NOTICE:Hello, to all.
AFTER THE CURRENT CONTEST THIS TUTORIAL WILL BE COMPLETELY REDONE.
Today I'm going to show you how to use arrays in C#.
OK so to get started we need to know, what is an array? Well the best way I can explain it is: An array is a variable that can hold multiple values without the need to assign multiple variables. So for instance we could turn these three variables: 'number1', 'number2', 'number3' into one variable(an array) 'numbers' and just access the value we want from it.
To use an array we declare it like this:
Then assign how many values it will hold like this:Code:int[] Numbers;
Now we have an array that will hold nine values, we can set the values like this:Code:Numbers = new int[9];
Important note: an array starts at 0 not 1 so be sure to remember that because if you assigning your array values like this:Code:Numbers[0] = 1; Numbers[1] = 2; Numbers[2] = 3; Numbers[3] = 4; Numbers[4] = 5; Numbers[5] = 6; Numbers[6] = 7; Numbers[7] = 8; Numbers[8] = 9;
Your going to get an error.Code:Numbers[1] = 1; Numbers[2] = 2; Numbers[3] = 3; Numbers[4] = 4; Numbers[5] = 5; Numbers[6] = 6; Numbers[7] = 7; Numbers[8] = 8; Numbers[9] = 9;
We can also make an array like this:
So what that did was make a new int array named Numbers with "[6]" values and assigned the values in one line of code here "{9,1,3,7,8,4}" but for now we will use the first way as it is easy to understand.Code:int[] Numbers = new int[6]{9,1,3,7,8,4};
OK so if your wandering how to make a string or double array we can make it the same as the int array just change the names.
For a double we just change it to:Code:string[] lowletter; lowletter = new string[26]; lowletter[0] = "a";// don't forget the [ " ] at the beginning and end of you string. lowletter[1] = "b"; lowletter[2] = "c"; lowletter[3] = "d"; lowletter[4] = "e"; lowletter[5] = "f"; lowletter[6] = "g"; lowletter[7] = "h"; lowletter[8] = "i"; lowletter[9] = "j"; lowletter[10] = "k"; lowletter[11] = "l"; lowletter[12] = "m"; lowletter[13] = "n"; lowletter[14] = "o"; lowletter[15] = "p"; lowletter[16] = "q"; lowletter[17] = "r"; lowletter[18] = "s"; lowletter[19] = "t"; lowletter[20] = "u"; lowletter[21] = "v"; lowletter[22] = "w"; lowletter[23] = "x"; lowletter[24] = "y"; lowletter[25] = "z";
OK now that we now how to declare an array lets print it out in a text box.Code:double[] Numbers; Numbers = new double[6]; Numbers[0] = 1.9; Numbers[1] = 1.7; Numbers[2] = 1.2; Numbers[3] = 3.3; Numbers[4] = 3.7; Numbers[5] = 7.9;
So if you haven't already, create a new windows form app from File->New Project->Windows Form Application, and drag a button and text box to the form.
OK so now double click the button and put in this code:
Now this will print out the values of the array, but you probably don't want to say " textBox1.Text += lowcase[0];" over and over again so we can replace it with a loop like this:Code:string[] lowcase; lowcase = new string[10]; lowcase[0] = "a"; lowcase[1] = "b"; lowcase[2] = "c"; lowcase[3] = "d"; lowcase[4] = "e"; lowcase[5] = "f"; lowcase[6] = "g"; lowcase[7] = "h"; lowcase[8] = "i"; lowcase[9] = "j"; textBox1.Text += lowcase[0]; textBox1.Text += lowcase[1]; textBox1.Text += lowcase[2]; textBox1.Text += lowcase[3]; textBox1.Text += lowcase[4]; textBox1.Text += lowcase[5]; textBox1.Text += lowcase[6]; textBox1.Text += lowcase[7]; textBox1.Text += lowcase[8]; textBox1.Text += lowcase[9];
Which runs a loop and prints the array values until "i" is equal to "lowcase.Length" which is how many values there are in the array which in this case would be 10.Code:for (int i = 0; i != (lowcase.Length); i++) { textBox1.Text += (lowcase[i]); }
OK so now you know how to declare an array set its values and print it in a loop, I hope you understood every thing clearly if you have any questions/problems tell me.
As always +rep and comments welcome.![]()
Last edited by CommittedC0der; 08-12-2011 at 12:38 PM. Reason: Added A Little More Info.
Knowledge: Intermediate C#, Beginner AS3, HTML, CSS, Binary, Hex, Octal.
Science is only an educated theory, which we cannot disprove.
You know, you can write arrays like this;
It's much easier than manually setting them all like you are doing.Code:string[] alpha = { "a", "b", "c" };
EDIT: Nevermind, I didn't see the part where you said that..
Yes.
But I tryed to keep it as easy to understand for the reader as I could so I used the other way.We can also make an array like this:
Code:
int[] Numbers = new int[6]{9,1,3,7,8,4};
So what that did was make a new int array named Numbers with "[6]" values and assigned the values in one line of code here "{9,1,3,7,8,4}" but for now we will use the first way as it is easy to understand.
EDIT OH yup![]()
Knowledge: Intermediate C#, Beginner AS3, HTML, CSS, Binary, Hex, Octal.
Science is only an educated theory, which we cannot disprove.
Hello, I have an assignement to write a for loop to sum characters (based on their ascii value). I have two text boxes where the user enters their name and somene else, and then the compter subtracts textBox2 from Box 1 to tell the user if they are compatbile or not. As far as I can get with this is:
for (int i = 0; i < myName.Length; i++)
{ total1 = (total1 + charValue); }
for (int i = 0; i < theirName.Length; i++)
{ total2 = (total2 + charValue2); }
label2.Text = Convert.ToString((total1 % 100) - (total2 % 100))
However, all this returns is the number of characters in textBox1 - the number of characters in textBo2. Any suggestions?
Last edited by Phoebe04; 03-29-2010 at 06:25 PM. Reason: oops, I think I hv put this iquestion in the wrong place
Im not sure I understand, your trying to get the ASCII value from each letter in ech textbox then compare them?Hello, I have an assignement to write a for loop to sum characters (based on their ascii value). I have two text boxes where the user enters their name and somene else, and then the compter subtracts textBox2 from Box 1 to tell the user if they are compatbile or not. As far as I can get with this is:
for (int i = 0; i < myName.Length; i++)
{ total1 = (total1 + charValue); }
for (int i = 0; i < theirName.Length; i++)
{ total2 = (total2 + charValue2); }
label2.Text = Convert.ToString((total1 % 100) - (total2 % 100))
However, all this returns is the number of characters in textBox1 - the number of characters in textBo2. Any suggestions?
Knowledge: Intermediate C#, Beginner AS3, HTML, CSS, Binary, Hex, Octal.
Science is only an educated theory, which we cannot disprove.
thx+ rep, enjoyed your easy to follow tut.![]()
Knowledge: Intermediate C#, Beginner AS3, HTML, CSS, Binary, Hex, Octal.
Science is only an educated theory, which we cannot disprove.
There are currently 1 users browsing this thread. (0 members and 1 guests)
Bookmarks