C# convert a long to string

Here my problem is:

I have this code:

static long CountLinesInFile(string f)
{
    long count = 0;
    using (StreamReader r = new StreamReader(f))
    {
        string line;
        while ((line = r.ReadLine()) != null)
        {
            count++;
        }
    }
    return count;
}

Which counts the lines of a text file. The problem I have is that when I'm trying this:

textBox1.Text = CountLinesInFile("test.txt");

I'm getting an error:

Error   1   Cannot implicitly convert type 'long' to 'string'

It seems legit, but how am I supposed to convert it to string? In Java its a simple toString()

Can someone give me a solution?

Answers


Use the ToString() method like this:

textBox1.Text = CountLinesInFile("test.txt").ToString();

In Java its a simply .ToString

And in C#, its simply .ToString().

Happy learning.


just write

textBox1.Text =(CountLinesInFile("test.txt")).ToString(); 

MSDN: Object.ToString Method - Returns a string that represents the current object.


try this textBox1.Text = CountLinesInFile("test.txt").ToString();


Need Your Help

Xcode iPhone "digest is missing"

ios iphone xcode development-environment organizer

"The digest is missing" in xcode organizer for iPhone 4S.

Working with Arrow Keys in a WPF Textbox (C#)

wpf textbox keypress arrow-keys

Is there any way to catch the Up/Down arrow keys in a WPF TextBox (System.Windows.Controls.Textbox) and allow them to alter the text? I've read about overiding the ProcessCmdKey method for a Windows