Jump to content


Check out our Community Blogs

Register and join over 40,000 other developers!


Recent Topics

Recent Status Updates

View All Updates

Photo
- - - - -

C# Tutorial: Arrays

array

  • Please log in to reply
6 replies to this topic

#1 CommittedC0der

CommittedC0der

    CC Leader

  • Expert Member
  • PipPipPipPipPipPipPip
  • 1065 posts
  • Programming Language:C#
  • Learning:C#, JavaScript, PL/SQL

Posted 15 February 2010 - 09:04 AM

NOTICE:
AFTER THE CURRENT CONTEST THIS TUTORIAL WILL BE COMPLETELY REDONE. :)

Hello, to all.

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:
int[] Numbers;
Then assign how many values it will hold like this:
Numbers = new int[9];
Now we have an array that will hold nine values, we can set the values like this:
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;
Important note: an array starts at 0 not 1 so be sure to remember that because if you assigning your array values like this:
Numbers[[COLOR=red]1[/COLOR]] = 1;
Numbers[[COLOR=red]2[/COLOR]] = 2;
Numbers[[COLOR=red]3[/COLOR]] = 3;
Numbers[[COLOR=red]4[/COLOR]] = 4;
Numbers[[COLOR=red]5[/COLOR]] = 5;
Numbers[[COLOR=red]6[/COLOR]] = 6;
Numbers[[COLOR=red]7[/COLOR]] = 7;
Numbers[[COLOR=red]8[/COLOR]] = 8;
Numbers[[COLOR=red]9[/COLOR]] = 9;
Your going to get an error.

We can also make an array like this:
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.

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.

[COLOR=red]string[/COLOR][] lowletter;
            lowletter = new [COLOR=red]string[/COLOR][26];
            lowletter[0] = [COLOR=red]"[/COLOR]a[COLOR=red]"[/COLOR];// 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";
For a double we just change it to:
[COLOR=red]double[/COLOR][] Numbers;

Numbers = new [COLOR=red]double[/COLOR][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;
OK now that we now how to declare an array lets print it out in a text box.

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:
  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];

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:
for (int i = 0; i != (lowcase.Length); i++)
            {

                textBox1.Text += (lowcase[i]);

            }
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.

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.:)

Edited by CommittedC0der, 12 August 2011 - 11:38 AM.
Added A Little More Info.

  • 1
There IS a war going on for your mind.
Nothing of eternal significance happens besides prayer.

#2 TriggerHappy

TriggerHappy

    CC Regular

  • Just Joined
  • PipPipPip
  • 30 posts

Posted 15 February 2010 - 03:56 PM

You know, you can write arrays like this;


string[] alpha = { "a", "b", "c" };


It's much easier than manually setting them all like you are doing.

EDIT: Nevermind, I didn't see the part where you said that..
  • 0

#3 CommittedC0der

CommittedC0der

    CC Leader

  • Expert Member
  • PipPipPipPipPipPipPip
  • 1065 posts
  • Programming Language:C#
  • Learning:C#, JavaScript, PL/SQL

Posted 15 February 2010 - 04:00 PM

Yes.

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.


But I tryed to keep it as easy to understand for the reader as I could so I used the other way.;)

EDIT OH yup ;)
  • 0
There IS a war going on for your mind.
Nothing of eternal significance happens besides prayer.

#4 Phoebe04

Phoebe04

    CC Lurker

  • Just Joined
  • Pip
  • 1 posts

Posted 29 March 2010 - 05:14 PM

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?

Edited by Phoebe04, 29 March 2010 - 05:25 PM.
oops, I think I hv put this iquestion in the wrong place

  • 0

#5 CommittedC0der

CommittedC0der

    CC Leader

  • Expert Member
  • PipPipPipPipPipPipPip
  • 1065 posts
  • Programming Language:C#
  • Learning:C#, JavaScript, PL/SQL

Posted 29 March 2010 - 05:53 PM

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?

Im not sure I understand, your trying to get the ASCII value from each letter in ech textbox then compare them?
  • 0
There IS a war going on for your mind.
Nothing of eternal significance happens besides prayer.

#6 James.H

James.H

    CC Devotee

  • Just Joined
  • PipPipPipPipPipPip
  • 569 posts

Posted 30 March 2010 - 01:44 PM

+ rep, enjoyed your easy to follow tut.
  • 0

#7 CommittedC0der

CommittedC0der

    CC Leader

  • Expert Member
  • PipPipPipPipPipPipPip
  • 1065 posts
  • Programming Language:C#
  • Learning:C#, JavaScript, PL/SQL

Posted 30 March 2010 - 02:14 PM

+ rep, enjoyed your easy to follow tut.

thx :)
  • 0
There IS a war going on for your mind.
Nothing of eternal significance happens besides prayer.





Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download