Jump to content

Baffling failure

- - - - -

This topic has been archived. This means that you cannot reply to this topic.
2 replies to this topic

#1
shikaningen

shikaningen

    Newbie

  • Members
  • Pip
  • 1 posts
How could it ever be that the following code should result in an IndexOutOfRange exception? I ask because that's what's happening.

[HIGHLIGHT="C#"]
number = such and such;
if(number > 9)
{
string blah = number.ToString();
byte whatever = byte.Parse(blah [ (blah.Length - 1) ] + "");
}
[/HIGHLIGHT]

I have even tried 'byte.Parse(blah[0] + "");' and even that causes an IdexOutOfRange exception to be thrown. I have tried breaking at the point of failure and analyzing the various relevant variable values and they all check out; Blah.Length = 3 and Blah = "422" when it fails.

Is this an example of heap corruption or some such? How should I go about fixing this?

I thank you for your time.

#2
Guest_Kaabi_*

Guest_Kaabi_*
  • Guests
I would be interested to see what is wrong with this code, as it looks fine.

#3
xpnctoc

xpnctoc

    Newbie

  • Members
  • Pip
  • 9 posts
That's messed up. Maybe you've got some bad memory or something. Your code looks fine, and I typed it into a test program using the value 422 myself. Did you ever find out what was going on?