array out of range - problem with data types (?)

  • Thread starter Thread starter ian
  • Start date Start date
I

ian

Hello,



lease can someone give me some advice regarding my program code (simplified
below).



I have an array which is going out of range. I think it's a data type
problem, something which I'm quite new to, (and I suspect the solution is
really simple !)



The program should loop trough the digits in an employee code (empCode).

And then for each of those digits, it should assign a binary code
representation for that digit.

Then it goes on to work with the binary code (this part is fine).



The logic is fine and it works when I explicitly set the employee digit
(empCodeDigit) value to a specific value (1-5) which the program the use to
get to relevent binary code representation for that digit.

I think the problem lies somewhere with the whole "empCode" being a "string"
, and that when I set the empCodeDigit to its 'i'th (Nth) digit (position),
it does not like it (because it is assigning this part of a string to an
"integer", (which it needs to be because that "integer" is then going to be
used as the counter for the "binaryCodes" array)




Thanks for any suggestions.

Ian.





string[] binaryCodes = new string[5]
{"00110","10001","01001","11000","00101"}



string empCode = "12543"; //believe this should be string because could
contain alphanumerics)

//(Will have a test here to check for
alphanumerics and use some other logic)





for (int i = 0; i <= 4; i++)

{

int empCodeDigit = empCode; //thought this would work (but
does not work)

//(should assign a single digit
(1,2,5,4,3) from the empCode)



// int empCodeDigit = 1; // this works (for testing, have
used this simple test

// to explicitly
set the array counter to

// an integer
value)



// empCodeDigit = empCode[1]; // --does not work




//--------------------------------------------------------------------------
---------



// empCodeDigit = empCode[1].ToInt(); // --does not work



// empCodeDigit = byte. empCode[1]; // --does not work



// empCodeDigit = char. empCode[1]; // --does not work



// empCodeDigit = (int) empCode[1]; // --does not work



// empCodeDigit = int (empCode[1]); // --does not work




//--------------------------------------------------------------------------
---------



//(set BinaryString equal to the values in the array, according to the
digit number from the empCode)

string BinaryString = binaryCodes[empCodeDigit]; <----- this array is
going out of range



For (........

{

//(loop through binary code bits)
 
Is this C# or C++?
Try this (C#):

int empCodeDigit = Convert.ToInt32(empCode.)

/claes
 
ian said:
Please can someone give me some advice regarding my program code (simplified
below).

Sure - although I don't think this is really the best newsgroup for
this question.

The answer is (as you suggested) very simple - but in two parts:

1) You're treating each *character* in empCode as if it were the
integer you were after. In fact, it's Unicode character. The values
you'll be getting are 49, 50, 53, 52, 51: '1', '2', '5', '3', '2'.
To transform them into 1, 2, 5, 3, 2 you could just subtract '0' if
you're confident they'll be in the range '0'-'9'.

2) You're then using binaryCodes[foo] where foo is in the range 1-5.
However, the valid indexes of binaryCodes are 0-4, because arrays are
0-based.

Those should give you enough information to be going on with - if
you're still having trouble, please post a short but complete program
which demonstrates the problem. See
http://www.poxox.com/~skeet/csharp/complete.html for what I mean by
that.
 
C#

(attempted to make some better formatted program code below)

ian.

Claes Bergefall said:
Is this C# or C++?
Try this (C#):
int empCodeDigit = Convert.ToInt32(empCode.)

Hello,



please can someone give me some advice regarding my program code

(simplified below).
I have an array which is going out of range. I think it's a data
type problem, something which I'm quite new to, (and I suspect the
solution is really simple !)
The program should loop trough the digits in an employee code
(empCode).

And then for each of those digits, it should assign a binary code
representation for that digit.

Then it goes on to work with the binary code (this part is fine).
The logic is fine and it works when I explicitly set the employee
digit (empCodeDigit) value to a specific value (1-5) which the program the use
to get to relevent binary code representation for that digit.

I think the problem lies somewhere with the whole "empCode" being a
"string", and that when I set the empCodeDigit to its 'i'th (Nth) digit
(position), it does not like it (because it is assigning this part of a string
to an "integer", (which it needs to be because that "integer" is
then going to be used as the counter for the "binaryCodes" array)

Thanks for any suggestions.
Ian.


-------------------------------------------------------------------------------------


string[] binaryCodes = new string[5]
{"00110","10001","01001","11000","00101"}

string empCode = "12543"; //believe this should be string because
// could contain alphanumerics)
//(Will have a test here to check for
// alphanumerics and use some other logic)
//--------------------------------------------------------------------
for (int i = 0; i <= 4; i++)
{
int empCodeDigit = empCode; //thought this would work (but does not work)
//(should assign a single digit (1,2,5,4,3) from the empCode)

// int empCodeDigit = 1; // this works (for testing, have used this simple test
// to explicitly set the array counter to an integer value)
// empCodeDigit = empCode[1]; // --does not work
//--------------------------------------------------------------------
// empCodeDigit = empCode[1].ToInt(); // --does not work
// empCodeDigit = byte. empCode[1]; // --does not work
// empCodeDigit = char. empCode[1]; // --does not work
// empCodeDigit = (int) empCode[1]; // --does not work
// empCodeDigit = int (empCode[1]); // --does not work
//--------------------------------------------------------------------
//(set BinaryString equal to the values in the array, according
//to the digit number from the empCode)
string BinaryString = binaryCodes[empCodeDigit]; <----- this array


//is going out of range
 
Thanks very much Jon,
sorry for posting my problem in the wrong place.
(By the way, I couldn't access www.poxox.com)

ian.

Jon Skeet said:
ian said:
Please can someone give me some advice regarding my program code (simplified
below).

Sure - although I don't think this is really the best newsgroup for
this question.

The answer is (as you suggested) very simple - but in two parts:

1) You're treating each *character* in empCode as if it were the
integer you were after. In fact, it's Unicode character. The values
you'll be getting are 49, 50, 53, 52, 51: '1', '2', '5', '3', '2'.
To transform them into 1, 2, 5, 3, 2 you could just subtract '0' if
you're confident they'll be in the range '0'-'9'.

2) You're then using binaryCodes[foo] where foo is in the range 1-5.
However, the valid indexes of binaryCodes are 0-4, because arrays are
0-based.

Those should give you enough information to be going on with - if
you're still having trouble, please post a short but complete program
which demonstrates the problem. See
http://www.poxox.com/~skeet/csharp/complete.html for what I mean by
that.
 
Back
Top