ajm113

Ok I am not to fancy with C#, but I do know a lot about PHP.

In this area I am trying to get the number of lines in a document then convert the number into a integer or a string. Then when I tell what random number it can go up too. It will use the random number in where what line it will read and then display on my RTB.

Sorry for the code. I am still working on it. I just started with this problem late at night and have been doing searching on the best way to do this and I think my way would work.

Code Snippet

private void LoadATip(object sender, System.EventArgs e)
{
string tipsFile = @"D3S_Base\tips.txt";
if (System.IO.File.Exists(tipsFile))
{

int counter = 0;
string line;
//Load the tips inside the box if it finds tips.txt
System.IO.StreamReader file = new System.IO.StreamReader(tipsFile);

while ((line = file.ReadLine()) != null)
{
counter++;
}
//Help More info needs to be entered!

}else{

System.Windows.Forms.MessageBox.Show("Can't Find tips.txt!", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
}
}


Thanks, Ajm113



Re: Visual C# Language Whats the best way to convert something into a string?

Peter Ritchie

the expected way is to use the type's ToString method. But, it depends on the type whether that creates anything useful for your purposes. For example. counter.ToString() will convert the integer to a string.






Re: Visual C# Language Whats the best way to convert something into a string?

Matthew Watson

If you want to convert Counter to a string, just do Counter.ToString()




Re: Visual C# Language Whats the best way to convert something into a string?

timvw

Btw, with string.Format you get something similar to php string generation/sprintf... (Make sure to lookup the format specificiers too)

string message = string.Format("There were {0} elements in the array.", counter);





Re: Visual C# Language Whats the best way to convert something into a string?

boban.s

You read the file lines, and in every line you probably have one tip. And you want to rundomly pick a row and show to the users. For showing tips, you will probably open some form, where he will see currently chosen tip, and will have two buttons (Next and Close) and if he clicks next you will rundomly pick next tip. I thing that creating form with that functionality is not that hard. I will write some source of rundomly pick a row:

StreamReader sr = new StreamReader(tipsFile);

string[] tips = sr.ReadToEnd().Split('\n');

Random rnd = new Random(DateTime.Now.Millisecond);

string tip = tips[rnd.Next(0, tips.Length - 1)]; //tip will here contain string content of rundomly picked row






Re: Visual C# Language Whats the best way to convert something into a string?

ajm113

Thanks that worked! Is thier something where I can tell it to ingore lines that have something like "#" at the biggining and maybe empty lines I.E Extra returns.





Re: Visual C# Language Whats the best way to convert something into a string?

Brendan Grant

To get you started, try adding something like this inside of your loop so that you only increment the counter if the condition has not been met... such as the line starting with a ¡°#¡± or the line being empty:

Code Snippet

if ( !(line.StartsWith("#") || line == "") )
{
counter++;
}






Re: Visual C# Language Whats the best way to convert something into a string?

ajm113

Thanks! I can't wait to get my program done! I still have a long way, but it will be worth it! Smile





Re: Visual C# Language Whats the best way to convert something into a string?

:)Eric Han

Brendan Grant wrote:

To get you started, try adding something like this inside of your loop so that you only increment the counter if the condition has not been met... such as the line starting with a ¡°#¡± or the line being empty:

Code Snippet

if ( !(line.StartsWith("#") || line == "") )
{
counter++;
}

if the line like this with some blank ahead:" #", does StartWith can still worked maybe need to call Trim()