newbie Arrays

  • Thread starter Thread starter MP
  • Start date Start date
M

MP

Hi
I have twp questions about arrays in c#

1) I have made following initialization:
// Create and initialize the names array
string[,] mediaClasses =
{
{"TYPE1","SubType1", "1", "10", "100", "2*2", "1000", "2", "223"},
{"TYPE2","SubType2", "1", "10", "500", "2*4", "1000", "4", "555"},
{"TYPE3","SubType3","0","0","0","0","0","0","0"},
{"TYPE4","SubType4 ","0","0","0","0","0","0","0"}
};

It is all string. I would like to know is possible to made some elements as
strings some as integer?


2) How I can add new line into array runtime?

I have tried following:
int NewRow;
int numberOfRows = mediaClasses.GetLength(0);

NewRow = numberOfRows;

mediaClasses.SetValue(txbType.Text, NewRow,0);
mediaClasses.SetValue(txbSubType.Text, NewRow,1);
mediaClasses.SetValue(txbProductID.Text, NewRow,2);

but did not works :(

Like I told you I am new in c# and I will appreciate some short example if
is possible.

Thanks in advance.
 
MP said:
Hi
I have twp questions about arrays in c#

1) I have made following initialization:
// Create and initialize the names array
string[,] mediaClasses =
{
{"TYPE1","SubType1", "1", "10", "100", "2*2", "1000", "2", "223"},
{"TYPE2","SubType2", "1", "10", "500", "2*4", "1000", "4", "555"},
{"TYPE3","SubType3","0","0","0","0","0","0","0"},
{"TYPE4","SubType4 ","0","0","0","0","0","0","0"}
};

It is all string. I would like to know is possible to made some elements as
strings some as integer?

Hi MP,

One solution is to use type object instead of string. i.e. object[,]
mediaClasses = ...

That will not give you any type safety. Another option is to create a new
class with the types you want and make a single dimension array of the new
class type.

public class MediaItem
{
string m_type;
string m_subType;
int m_val1;
int m_val2;
int m_val3;
string m_expression;
int m_val4;
int m_val5;
int m_val6;

public MediaItem (
string type,
string subType,
int val1,
int val2,
int val3,
string expression,
int val4,
int val5,
int val6)
{
m_type = type;
m_subType = subType;
m_val1 = val1;
m_val2 = val2;
m_val3 = val3;
m_expression = expression;
m_val4 = val4;
m_val5 = val5;
m_val6 = val6;
}
}

public class MediaTest
{
public MediaTest()
{
// Create and initialize the names array
MediaItem[] mediaClasses =
{
new MediaItem("TYPE1","SubType1", 1, 10, 100, "2*2", 1000, 2, 223),
new MediaItem("TYPE2","SubType2", 1, 10, 500, "2*4", 1000, 4, 555),
new MediaItem("TYPE3","SubType3", 0, 0, 0, "0", 0, 0, 0),
new MediaItem("TYPE4","SubType4", 0, 0, 0, "0", 0, 0, 0)
};
}
}
2) How I can add new line into array runtime?

I have tried following:
int NewRow;
int numberOfRows = mediaClasses.GetLength(0);

NewRow = numberOfRows;

mediaClasses.SetValue(txbType.Text, NewRow,0);
mediaClasses.SetValue(txbSubType.Text, NewRow,1);
mediaClasses.SetValue(txbProductID.Text, NewRow,2);

but did not works :(

You didn't print the error, so I don't know for sure. However, I do know
that you are trying to read off the end of the array, which will throw an
IndexOutOfRangeException at runtime. I'd fix it like this:

NewRow = numberOfRows - 1;

Remember, C# arrays are 0-based by default.

Joe
 
Hi,

it is not very good to do such things because at runtime you must know which
type the element has at which index this makes your code unstable. I think
you could put classes in you array. If you want to elements while runtime
you need to take e.g Arraylist from System.Collections.

--
Mit freundlichen Gruessen - Regards

Ralph Gerbig
(e-mail address removed)
www.ralphgerbig.de.vu
 
I am trying to add new row. Minus one (- 1) is only teoretically, I have
tried without -1 and if look trough debbuger it is good.
I need two dimensions and I am not sure is ArrayList good solution as Ralph
suggest?

If you wish I can send you my short example and you can see details.

Thanks,


Ralph Gerbig said:
Hi,

it is not very good to do such things because at runtime you must know which
type the element has at which index this makes your code unstable. I think
you could put classes in you array. If you want to elements while runtime
you need to take e.g Arraylist from System.Collections.

--
Mit freundlichen Gruessen - Regards

Ralph Gerbig
(e-mail address removed)
www.ralphgerbig.de.vu
MP said:
Hi
I have twp questions about arrays in c#

1) I have made following initialization:
// Create and initialize the names array
string[,] mediaClasses =
{
{"TYPE1","SubType1", "1", "10", "100", "2*2", "1000", "2", "223"},
{"TYPE2","SubType2", "1", "10", "500", "2*4", "1000", "4", "555"},
{"TYPE3","SubType3","0","0","0","0","0","0","0"},
{"TYPE4","SubType4 ","0","0","0","0","0","0","0"}
};

It is all string. I would like to know is possible to made some elements as
strings some as integer?


2) How I can add new line into array runtime?

I have tried following:
int NewRow;
int numberOfRows = mediaClasses.GetLength(0);

NewRow = numberOfRows;

mediaClasses.SetValue(txbType.Text, NewRow,0);
mediaClasses.SetValue(txbSubType.Text, NewRow,1);
mediaClasses.SetValue(txbProductID.Text, NewRow,2);

but did not works :(

Like I told you I am new in c# and I will appreciate some short example if
is possible.

Thanks in advance.
 
MP said:
I am trying to add new row. Minus one (- 1) is only teoretically, I have
tried without -1 and if look trough debbuger it is good.
I need two dimensions and I am not sure is ArrayList good solution as Ralph
suggest?


If you are wanting to add a new row, an ArrayList is a good choice. C#
arrays are fixed size, so once they are declared, you are stuck with an
array of that size. However, with an ArrayList you can add as many items as
you want. i.e.

ArrayList mediaClasses = new ArrayList();

mediaClasses.Add(new MediaItem("TYPE1","SubType1", 1, 10, 100, "2*2", 1000,
2, 223));

If you must have two dimensions, then initialize your array to a size that
will hold everything you want to add. You could also add an array to your
ArrayList and simulate two dimensions.

// array defined as object because you expressed a desire to add different
types to the array
mediaClasses.Add(new object[] {"TYPE1","SubType1", "1", "10", "100", "2*2",
"1000", "2", "223"});

p.s. I didn't compile this, so please forgive syntax errors, if any.

Joe
 
Back
Top