2

I'm reading a field On a table it only has 3 values ("",ESD,R&S)

I don't know exactly why, but when I read the R&S value, the print out label is R ("empty space") S this is the code I'm using:

char[] area = read1[8].ToString().ToCharArray();
// if array is less than one do nothing
if (area.Length > 1)
{
    //trying to use this to check if the second item of array is the "&" symbol (print this format data)
    if (area[1].ToString() == "&")
    {
        Arealbl.Text = area[0].ToString() + "\n" + "&" + "\n" + area[2].ToString();
    }
    //else print out this format data
    else
    {
        Arealbl.Text = area[0].ToString() + "\n" + area[1].ToString() + "\n" + area[2].ToString();
    }
}

I using this code because I haven't found an easy way to put a label on vertical.

3
  • 2
    Please put more effort into formatting your code in future questions. It's better for you to do it than to rely on others doing it for you, and it makes a huge difference to the readability of the question. Commented Aug 11, 2014 at 17:02
  • Just a side observation, if (area.Length > 1) should be if (area.length >= 3) otherwise a length 2 will throw an index out of range exception. Commented Aug 11, 2014 at 17:04
  • 1
    @AWinkle, thx for the observation made the modification on my code. Commented Aug 11, 2014 at 17:26

2 Answers 2

1

The & is a special char in MenuItems, Labels and Buttons, used to indicate that the next char should be underscored. When you manage to focus Arealbl and hit Alt you might see that.

Set

Arealbl.UseMnemonic = false;

somewhere. Like with the designer.

Sign up to request clarification or add additional context in comments.

1 Comment

Than you for the help it print out juts the way i wanted.
1

In addition to @Henk Holterman's answer, here are a few code review suggestions. You can access a string as an array, so there is no need to .ToString().ToCharArray(), just to .ToString() everything further down the method. Simplifying the concatenation to a string.Format can help improve readability and assuming you don't have to do this a large number of times (tens of thousands) it shouldn't impact performance.

string area = read1[8].ToString()
if(area.Length < 3) { return; } //exit early on error conditions.
// if array is less than one do nothing
Arealbl.UseMnemonic = false; //only add this if you cannot guarantee it will be set.
Arealbl.Text = string.Format("{0}\n{1}\n{2}", area[0], area[1], area[2]);

1 Comment

I tought i needed the "ToCharArray()" since Im using a char array, thank you for the code and suggestions @AWinkle.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.