tags:

views:

28

answers:

3

If using the following in an if statement I get an error:

If trg.Name.Substring(4, 6).ToUpper <> ("ABCDEF") Then

I get the error: "Index and length must refer to a location within the string. Parameter name: length"

I assume this is because the string (trg.name) is too small for the 4, 6 substring. What would be the correct method of working around this problem?

Thanks, madlan.

VB.net Studio 2008.

A: 

You should likely be checking that the length of trg.Name is at least (4+6) characters long.

TreDubZedd
trg.name can be any length. I'm looking for any that fit 123_ABCDEF***The only constant is 4, 6 (ABCDEF) which I need to look for.
madlan
+2  A: 
If (trg.Name.IndexOf("ABCDEF", StringComparison.OrdinalIgnoreCase) <> 4) Then
LukeH
+1 from me. This is the way I would recommend.
Stefan
A: 

Instr returns the index of where the searched string is first found. So If you could do this:

If InStr(trg.Name, "ABCDEF", CompareMethod.Text) - 1 <> 4 Then

With InStr you dont have to check lenght of trg.Name.

Stefan
LukeH:s example is better (using the string-objects .IndexOf), but I leave my answer as it is for variation and information.
Stefan