Interesting situation

  • Thread starter Thread starter Tibby
  • Start date Start date
T

Tibby

One thing I'vew come to dislike is .NEt's lack of sensable error messages. On this one, I get an "Index out of bounds" Can't figure it out. Will give ya the setup...

Protected Structure Letter
Dim Series1 As String
Dim Series2 As String
Dim Series3 As String
End Structure

Dim StoreLetter As Letter

StoreLetter = New Letter()
StoreLetter.Series1 = 0

If StoreLetter.Series1.Length < 15 Then StoreLetter.Series1 = New String( "0", 0, _
CInt(15 - StoreLetter.Series1.Length)) & StoreLetter.Series1

The above line is where I get the Index out-of bounds. I know, long line wrapped LOL So, in the End, since my StoreLetter.Series1.Length=1, I need to tack 14 Zero's onto the beginning of it, then the value of StoreLetter.Series1 on the end to make a string with 15 Zero's in this case.

Thanks
Tibby
 
Hi Tibby,

|| StoreLetter.Series1 = New String( "0", 0, _
|| CInt(15 - StoreLetter.Series1.Length)) & StoreLetter.Series1

This effectively comes down to
S = New String ("0", 0, 14)

Now the String constructor which takes three arguments is expecting an
array of characters for the first argument, and the second and third are the
starting position within that array and the number of characters therefrom.

In formal syntax, therefore, the code is
S = New String (New Char() {"0"}, 0, 14)

Now you can see where the array comes from. The second parameter says
start at the first char. The second says take 14 chars starting at that point.

But the array doesn't go that far - and <that> is an 'Index out-of bounds'
error! :-)

You have to remember that "ABCDE" is shorthand for New Char() {"A", "B",
"D", "E"}. [And glad it is too!].

I have to agree that the error message could be much more informative,
though. It could tell you what the array is, what value you've given and what
the array limits are, for example. It could perhaps show you the particular
definition of the constructor that you're using.

But, hey, compiler technology has only been developing for around half a
century. Give them time!! ;-)

Regards,
Fergus

ps. For my college project I investigated adding 'human' (ie. useful) error
messages to a Pascal compiler. Adding such intelligence to a compiler can
easily increase the complexity by a factor of ten.
Do you think it's worth it? So do I. :-)
Do 'they'? Not so far. :-(
 
To make sure I'm understanding you, I cannot use this as a replacement for
the old
String("0",14) command of VB6?
Basically, it would be S=New String(New Char() {"A","B","C"},1,2) would make
S =
"AB"???

Well that's no good, urgghhhhhh, I was hoping they wouldn't mess with that
nifty little feature. SO what, a for..next loop to add these 14 Zero's onto
the front?

Tibby
 
Sorry, this is the entire Error message:
Index was out of range. Must be non-negative and less than the size of the
collection.

Not that that helps much LMAO
Tibby
 
Fergus,
Very intresting, I never take that long thinking processes.
But I find it always nice to see that it is so deep been investigated
And your explanation was of course very clear.
:-)
Cor
 
Tibby,
Before you will think it , I was making and sending my message long before I
saw your answer.
Cor
 
Hi Tibby,

|| urgghhhhhh

Lol.

|| S=New String(New Char() {"A","B","C"},1,2) would make
|| S = "AB"???

Almost. Be warned - indexing in .NET starts at 0 so S = "BC". But just to
keep you on your toes, the old string functions (Mid, et al) are stll
available but index from 1.

|| the old String("0",14) command of VB6

... you will be relieved to know, is still with us - repeat <char> <n>
times. The String constructor that you used is simply an alternate constructor
(for other purposes).

I should point out that "X" will fail in New String() if you use Option
Strict On - the required syntax being "X"c or "X"C to indicate a character.

|| for..next loop to add these 14 Zero's onto the front?

Now you know, you don't have to use a loop.

But you might like this one:
StoreLetter.Series1 = StoreLetter.Series1.LeftPad ("0"c, 15)

This makes StoreLetter.Series1 15 characters long, if it was less, padding
on the left with "0" as required.

Have a look at the String Class - there are some nice methods in it.

All the best, ;-)
Fergus
 
Back
Top