tags:

views:

178

answers:

6

Hello guys, I have a question regarding this code. I'm a beginner and enjoy learning C#. But then I'm on the topic now called Array which is quite difficult and I need your help. I would like to understand the code. What I don't understand here is what does the part 1, 2, 3, 4 and 5 here mean?
I don't understand what is the function of "const" and "byte" here? I would appreciate your explanation? Thanks & regards;-)

1)

const byte numbers = 5;
byte[] myNumbers = new byte[numbers];
byte additionalNumbers;
Random coincidenceNumbers = new Random();

2)

string yourPassword;
Console.WriteLine("Please enter your password:");
yourPassword = Console.ReadLine();

if (yourPassword != "helloWorld")
{ 
    Console.WriteLine("\nWrong password\n");
    return;
}
else 
{
    Console.WriteLine();
    Console.WriteLine("Welcome to my world!");

    for (int i=0; i < myNumbers.Length; ++i)
    { 
        myNumbers[i]=(byte)(coincidenceNumbers.Next(1,50));
    }
}

3)

additionalNumbers=(byte) (coincidenceNumbers.Next(1,50));

4)

Array.Sort(myNumbers);
Console.WriteLine("\nThe Number is:\n");

5)

foreach (byte elem in myNumbers)
{
    Console.WriteLine("\t" + elem);
    Console.WriteLine();
    Console.WriteLine("Additional Number is: " + additionalNumbers);
}
+1  A: 

You need to read some basic education material on arrays, try MSDN Arrays Tutorial for example.

Paul Ruane
thanks for the link Paul
tintincute
A: 

byte is an integer type that stores values 0..255

const means that the value of variable "numbers" will never change (so it is constant).

Rashack
A byte is an UNSIGNED 8 bit integer type, that can store 0 to 255 (0xff)
Oliver
+1  A: 

const is a reserver word meaning that "variable" will not vary, instead, its value will not change, if you try to change

const byte numbers = 5;
numbers = 6; // will fail

byte is a type for storing small numbers

Then,

byte[] myNumbers = new byte[numbers];

creates an array of numbers (5) positions. You can for example, assign values to any position within the array, like this:

myNumbers[0] = 4;  // position 1
myNumbers[1] = 45;  // position 2
myNumbers[2] = 25;  // position 3
myNumbers[3] = 0;  // position 4
myNumbers[4] = 12;  // position 5
myNumbers[5] = 3;  // will fail, array just have 5 positions

[Edit]

additionalNumbers=(byte) (coincidenceNumbers.Next(1,50));

Here, coincidenceNumbers is a Random object, so it will generate random numbers. Its "Next" function will generate an integer number. It receives 2 parameters: minimum value and maximum value. So, here it will generate a random number between 1 and 50.

Integer is very big compared with byte, so there is a "casting"... the integer number will be converted to byte. If integer number less than 255, no problem, in other case you will loss precision

If you try to do this

int x = 500;
byte y = (byte) x;
Console.WriteLine(y); // 244, precision lost
Jhonny D. Cano -Leftware-
hello Jhonny, thanks for explaining. It's very informative but this one, I don't understand:additionalNumbers=(byte) (coincidenceNumbers.Next(1,50));Array.Sort(myNumbers);
tintincute
I edited my answer, check it out
Jhonny D. Cano -Leftware-
A: 
  1. Creates a constant variable which holds a byte, with the value 5.
  2. Makes place for a text string and calls the variable yourPassword.
  3. Loops through each byte in myNumbers.
  4. Sorts myNumbers.
  5. Loops through each byte in myNumbers again (this time it's sorted), calling each byte "elem".

You really should read some beginners tutorial on programming (in general). This has very little to do with the actual Array class.

EDIT: Aaaand there you changed your question...

Deniz Dogan
thanks skorpan. I will
tintincute
A: 

I'm guessing that the 1*, 2*, 3*, 4*, and 5* are notations pointing something out to you in the example you're looking at.

const is a modifier that marks the variable as constant (i.e. it will never change)

byte is a data type that will store 1 byte (8 bits) of data.

Edit Well, now I feel stupid...the question wasn't formatted quite the same when I initially read it. Feel free to ignore my answer...

dustyburwell
sorry about that maybe I wasn't clear with my design here. the 1 I meant was this:const byte numbers = 5;byte[] myNumbers = new byte[numbers];byte additionalNumbers;Random coincidenceNumbers = new Random();I would like to understand how the code, flows.no problem;-)
tintincute
A: 

From MSDN:

const:

The const keyword is used to modify a declaration of a field or local variable. It specifies that the value of the field or the local variable is constant, which means it cannot be modified.

So for example:

const byte numbers = 5;

You are declaring a constant variable of type 'byte' named numbers with value of 5.

Later on, you will not be able to change the variable numbers like:

numbers = 6; // this is not allowed because it was declared as const

I hope this clarifies your first part of the question.

I suggest going to MSDN and lookup up the terms for the other parts of your question. Try getting into a habit of using this resource for future reference.

Floetic