Monday, January 24, 2011

Structures in C#

Structures:

The second kind of the types of variables is the structure 'Struct' acronym for the word (structure)
Represent the structure of different parts of the data can be different for each part type. It enables us to define our species of special variables, depending on the structure ...
For example:
Suppose we wish to save the path for the site specific from the point where this path composed of direction and distance measured in miles and simplify things we'll impose that the trend is one of the four directions of knowledge within a census orientation
And that the mileage represented by type double
Now we can use separate converts to represent this path as in the following code:


orientation direction;
double distance;

There is nothing wrong in what we have done but it would be simpler if we used a single variable to store this information



The definition of structures:

Struct <type name>
{
<member Declaration>
}

Section <memberDeclaration>: include the declaration of the variables (called data members of this structure) with the same usual method of almost every statement of intention to take the following formula:

<accessibility> <type> <name>;
  : <name> Represents the name of any user name variable
   : <type> Represents the type of user
: <accessibility> Is an extent of access to the member from outside the structure


namespace s {
    class class1
    {
        enum orientation : byte
        {
            North = 1,
            South = 2,
            East = 3,
            West = 4
        }

        struct route
        {
            public orientation direction;
            public double distance;
        }

        static void main(string[] args)
        {
            route myRoute;
            int myDirection;
            Double myDistance;
            Console.WriteLine(@"1) North \ n 2) South \ n" + @"3) East \ n4) West");

            do
            {
                Console.WriteLine("select a direction:");
                myDirection = Convert.ToInt32(Console.ReadLine());
            } while ((myDirection < 1) || (myDirection > 4));

            Console.WriteLine("Input a distance: ");
            myDistance = Convert.ToDouble(Console.ReadLine());
            myRoute.direction = (orientation)myDirection;
            myRoute.distance = myDistance;
            Console.WriteLine("myRout specifies a direction of {0}" + "and a distance of {1} ", myRoute.direction, myRoute.distance);
        }
    }
}

No comments:

Post a Comment