Click here to Skip to main content
14,573,479 members
Rate this:
Please Sign up or sign in to vote.
See more:
Hello Good Friends, :laugh:
I need your help again. Please help me.

I am using C#NET2008 to develop application using Two Dimension Array to store String text and INT data

These are the error messages:
Error #1 Index was outside the bounds of the array.
Error #2 Cannot implicitly convert type 'int' to 'string'

private string[ , ] strColmArray = new string[9, 1];

private void FLoadColmArray()
   {
       strColmArray[0, 0] = "Order ID";
       strColmArray[0, 1] =  "Ord487";  <--- Error #1

       strColmArray[1, 0]  = "Product ID";
       strColmArray[1,1]   =  750.45;  <- Error #2
    }
Posted
Updated 1-Jan-11 19:56pm
v2
Rate this:
Please Sign up or sign in to vote.

Solution 1

strColmArray[0, 1] =  "Ord487";  <--- Error #1


Your array is small, that's why you get the first error.
Try

private string[ , ] strColmArray = new string[9, 2]


The second error - is because you can't use a string array to store integers, try:
strColmArray[1,1]   =  "750.45";  - Error #2


As a side note, don't use a two dimensional array for this kind of things.
You can use a DataTable or a List Of Strongly typed objects to do these kind of things.
   
Comments
Manfred Rudolf Bihy 2-Jan-11 4:54am
   
I think OP has trouble grasping the concept of zero based indexes so I pointed that out to him in my answer. Since you're absolutely correct I gave you 5 for your good answer.
You have even shown him the correct solution which he has obviously not tried out.
Shani Natav 2-Jan-11 4:59am
   
Thanks! :)
Manfred Rudolf Bihy 2-Jan-11 4:59am
   
[Moved to a comment on OP's behalf]
My Array is not small as per my declaration
private string[ , ] strColmArray = new string[9, 1];
Using DataTable is no problem but I have to obey instructions from my IT Senior Team Leader whose requirement and specfication requested the use of ARRAY. So, I have no choice.
Rate this:
Please Sign up or sign in to vote.

Solution 3

Your array is too small! The nubmers are 9 and 1 giving you 9 slots in the first array dimension and 1 slot in the second array dimension.
Indexes are zero based so:

private string[ , ] strColmArray = new string[9, 1];
private void FLoadColmArray()
   {
       strColmArray[0, 0] = "Order ID";
       strColmArray[0, 1] =  "Ord487";  //This is the second slot in the second array but you declared only one slot

       strColmArray[1, 0]  = "Product ID";
       strColmArray[1,1]   =  750.45;  //should be "750.45" as there is no implicit cast from that to a string it will have to be done explicitely or expressed as a string in the first place
    }


I think you need to read some on arrays and zero based indexes.

Best Regards,

Manfred
   
v3
Rate this:
Please Sign up or sign in to vote.

Solution 4

If your IT Senior Team Leader has requested an Array, then give him an Array.

It cannot, however, be a string Array since one of the pieces of data is an integer, unless you convert to and from a string each time you want to read or store that data.

You could use an Array of type Object

private object[ , ] objColmArray = new object[9, 1];


which will allow you to store any data type but has the disadvantage that to make use of the data you will have to convert/cast the data when reading it.

My preference, if it has to be an Array, would be to use a one dimensional array of a custom class

internal class MyClass
{
  public string Name {get; set;}
  public int Value {get; set;}
}

private MyClass[] mcColmArray = new MyClass[100];


My real preference would be to have a chat with your IT Senior Team Leader to find out why they want you to use an Array when it is not the best option.
   
Rate this:
Please Sign up or sign in to vote.

Solution 6

Hi Henry Minute,
As requested by you to speak to my IT Senior Team Leader, I have printed your request and show it to JaneDurban my IT Senior Team Leader.

She mentioned that the request of using 2 Dimensional Array by me is to develop the working logic so share it with other developers who are fresh Graduates from University 8 months ago who do not have the practical experiences.

She said the idea of 2 dimensional Array is to create a storage events of Column Name and column width for EXCEL 2003 Spreadsheet and WORD 2003 Table that will reduced the codings.
   
Comments
   
TeeLeong, you're not supposed to post such text in the form reserved to answer your question. Please, remove it and put in comment to the answer of Henry Minute.
Rate this:
Please Sign up or sign in to vote.

Solution 2

[Moved to a comment]:

This was supposed to a comment OP should have written to an answer.
   
v2
Comments
Manfred Rudolf Bihy 2-Jan-11 4:49am
   
Answer was moved to a comment.
Rate this:
Please Sign up or sign in to vote.

Solution 5

You have some correct answers here.

Also, don't forget jagged array (in some document Microsoft recommended to prefer those over regular fixed-rank arrays, because they can contain uneven-length array in one or more indexes, buy I think, this depends on application):

string[][] array = new string[3][];
int length = array.Length; //==3; first index is outer
for (int index = 0; index < length; index++)
    array[index] = new string[index * 2 + 1]; //for example
//...
array[2][0] = "some value";


I've put this sample just to let you see which index is initialized first and which one allows for variable-length initialization.
   

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)




CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100