Help with string.StartsWith() please

C

CCLeasing

I have a text file which consists of lines of data. Each line has units
of data seperated by semicolons, like so.

AAAA ; some text relating to AAAA ; some more text relating to AAAA
BBBB ; some text relating to BBBB ; some more text relating to BBBB

I have a form which consists of a combo box control, and text box
control.
combo box contol is called = comCrime
text box control is called = txtProtocol

I am trying to compare the value in a combo box with the contents of a
text file. I am trying to find the line in the text file that starts
with the text in the combo box. Once this line is found I would like to
populate the textbox control on my form with the text that lies between
the first and second semicolons of the line. I have tried the following
code but it isn't working. Can someone suggest a change please.

private void comCrime_TabIndexChanged(object sender, EventArgs e)
{
string[] lines = File.ReadAllLines(@"c:\protocol.txt");

foreach (string s in lines)
{
string[] items = s.Split(':');

if s.StartsWith(comCrime.Value) Then txtProtocol.Text =
s[1].ToString;

}
}
 
R

Rick Lones

CCLeasing said:
I have a text file which consists of lines of data. Each line has units
of data seperated by semicolons, like so.

AAAA ; some text relating to AAAA ; some more text relating to AAAA
BBBB ; some text relating to BBBB ; some more text relating to BBBB

I have a form which consists of a combo box control, and text box
control.
combo box contol is called = comCrime
text box control is called = txtProtocol

I am trying to compare the value in a combo box with the contents of a
text file. I am trying to find the line in the text file that starts
with the text in the combo box. Once this line is found I would like to
populate the textbox control on my form with the text that lies between
the first and second semicolons of the line. I have tried the following
code but it isn't working. Can someone suggest a change please.

private void comCrime_TabIndexChanged(object sender, EventArgs e)
{
string[] lines = File.ReadAllLines(@"c:\protocol.txt");

foreach (string s in lines)
{
string[] items = s.Split(':');

if s.StartsWith(comCrime.Value) Then txtProtocol.Text =
s[1].ToString;

}
}

Well, you say the separators are semicolons, but your sample code splits on
colons. Is it just a simple typo?

-rick-
 
D

Dan Bass

Gary,

If you know the text file is going to be really small then it's not much of
an issue, but rather than reading all the data into the string[] lines
variable, maybe open the file, then go through and read each line. This
method is arguably more efficient in time and on the resources.

Have you tried stepping through this code to see what the values are for the
variables?
Also, I'd put in checks around the array accessors to ensure that when you
go "items[1]...." that you actually have a valid non-null array with at
least two entries [0] and [1].

Finally, are you expecting the text to be case sensitive or not? your code
is case sensitive at the moment because StartsWith is case-sensitive...

Thanks.
Dan.
 
D

Dustin Campbell

I have a text file which consists of lines of data. Each line has
units of data seperated by semicolons, like so.

AAAA ; some text relating to AAAA ; some more text relating to AAAA
BBBB ; some text relating to BBBB ; some more text relating to BBBB

I have a form which consists of a combo box control, and text box
control.
combo box contol is called = comCrime
text box control is called = txtProtocol
I am trying to compare the value in a combo box with the contents of a
text file. I am trying to find the line in the text file that starts
with the text in the combo box. Once this line is found I would like
to populate the textbox control on my form with the text that lies
between the first and second semicolons of the line. I have tried the
following code but it isn't working. Can someone suggest a change
please.

private void comCrime_TabIndexChanged(object sender, EventArgs e)
{
string[] lines = File.ReadAllLines(@"c:\protocol.txt");
foreach (string s in lines)
{
string[] items = s.Split(':');
if s.StartsWith(comCrime.Value) Then txtProtocol.Text
= s[1].ToString;

}
}

If you're using the TabIndexChanged event, it's unlikely that your code will
ever execute because that only fires when the TabIndex property is changed.
You might try the SelectedIndexChanged event.

Based, on your description, the code should look like this:

private void comCrime_SelectedIndexChanged(object sender, EventArgs e)
{
string[] lines = File.ReadAllLines(@"c:\protocol.txt");
if (lines.Length == 0)
return;

string lookUpValue = comCrime.Text;

foreach (string s in lines)
{
if (s.StartsWith(lookUpValue))
{
string[] items = s.Split(';');
if (items.Length > 1)
{
txtProtocol.Text = items[1];
break;
}
}
}
}

Best Regards,
Dustin Campbell
Developer Express Inc.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top