Click here to Skip to main content
14,641,302 members
Rate this:
Please Sign up or sign in to vote.
See more:
I have a text box that takes two numbers separated by a comma. For eg: "05, 2004".
I have a two dimensional array called store[i][j] of integer type.
I would like to store 10 in ith position and 2010 in jth position.
such that, store[0][0] should be equal to "05, 2010"
So far, I've only been learning and dealing with one dimensionl array,
and have been using the following statement to read values from a textbox.

if (!int.TryParse(carmake.Text, out store[0]))
MessageBox.Show("type an integer ");

Any help would be much appreciated. Thanks
Updated 12-Sep-11 0:30am
Rate this:
Please Sign up or sign in to vote.

Solution 1

May be this will help you or you'll get some idea

string textBox = "45,78";
int[,] values = new int[1,2];
string[] textBoxSplit = textBox.Split(',');
values[0, 0] = Convert.ToInt32(textBoxSplit[0]);
values[0, 1] = Convert.ToInt32(textBoxSplit[1]);
steersteer 12-Sep-11 6:52am
Thanks for your reply, but I am afraid that Split(); method is not available to textbox control in WPF. I tried with "carmake.Split(','); but I get an error that the does not contain a definition for that method. Any thought about it ?
BobJanova 12-Sep-11 10:19am
Look carefully, textBox in this sample is a -string-.
Syed Salman Raza Zaidi 12-Sep-11 6:57am
Try this,use textbox value in a string and then spliting that string it will surely help you :)
Rate this:
Please Sign up or sign in to vote.

Solution 2

Two dimensional arrays are used to construct matrices. And at teach point in the array, you could still only save one item. Example:

[0][0] : "5, 2010" | [0][1] : "6, 2010"
[1][0] : "5, 2011" | [1][1] : "6, 2011"

What I think you would need to look at is either List<KeyValuePair<int, int>> or Tuple<int, int>.
steersteer 12-Sep-11 6:58am
Thanks. Very good example. Easy to understand for an amateur like me. Please see if you could answer my other question about controls in WPF.
KenBonny 12-Sep-11 7:14am
Sorry mate, never worked with WPF before.
BobJanova 12-Sep-11 10:18am
I updated this solution to display the type chevrons properly. Use HTML entities (&lt;/&gt;) to get them.
KenBonny 13-Sep-11 2:07am
Cheers. Didn't know I had to use those. It's been a looong time... since I've used those.

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