Link to home
Start Free TrialLog in
Avatar of jmpatton
jmpatton

asked on

c# Substring Help

Im checking the substring of an item and I keep getting and index out of range error.

if (memberSequenceList.Items[sequencePosition].StatusFlags.Substring(17).Equals("1"))
{

}

The StatusFlags substring in question looks like this '0000000000000100000000'

Can someone help me get around this or point out what I may be doing wrong?

Thanks
Avatar of mwochnick
mwochnick
Flag of United States of America image

how many items are in the memberSequenceList and how is the value sequencePosition set?
That's where your index out of bounds is coming from.
Avatar of jmpatton
jmpatton

ASKER

There are 26 members in memberSequenceList and the sequencePosition is 10.  

memberSequenceList.Items[10].StatusFlags = "00000000000001000000000000000000"

So thats why im so confused on the error.  Because im asking if (memberSequenceList.Items[10].StatusFlags.Substring(17).Equals("1")).

If its not equal to "1", I would think that it would just drop down to the next block of code.
ASKER CERTIFIED SOLUTION
Avatar of mwochnick
mwochnick
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of Fernando Soto
Hi jmpatton;

You state that StatusFlags is equal to this string "0000000000000100000000" and the command you are using is StatusFlags.Substring(17).Equals("1")). The SubString starting at index 17 of the string which is the 18th character in that string because strings are zero index based to the end of the string which makes Substring(17) equal to the string "00000" which will never equal a string of length of one character. Therefore the Substring method that you should use is this implementation SubString( StartIndex, Length ) therefore your statement should be this:

if (memberSequenceList.Items[sequencePosition].StatusFlags.Substring(17, 1).Equals("1"))
{

}

Open in new window


Hope that helps