SHARE
TWEET

Untitled

a guest Sep 13th, 2017 76 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Text;
  4.  
  5.  
  6. namespace ArrayOfObjects
  7. {
  8.     class Program
  9.     {
  10.  
  11.         static void Main(string[] args)
  12.         {
  13.             Class1[] listing = new Class1[3]; // Alex: okay, the array of 3 instances of Class1 is declared. Everything's perfectly fine
  14.  
  15.  
  16.         // Alex: Juan. Looks great
  17.             Class1 a1 = new Class1();
  18.             a1.YourName = "Juan";
  19.             a1.YourAge = 26;
  20.             a1.YourCountry = "Argentina";
  21.  
  22.         // Alex: That would be me. Still fine
  23.             Class1 a2 = new Class1();
  24.             a2.YourName = "Alex";
  25.             a2.YourAge = 20;
  26.             a2.YourCountry = "Russia";
  27.  
  28.         // Alex: Our old pal Jon this was intended to be
  29.             Class1 a3 = new Class1();
  30.             a2.YourName = "Jon Skeet"; // Alex: uh-oh, should've used a3 here.
  31.             a2.YourAge = 40; // Alex: and here as well. That's how I ended up being 40. :D
  32.             a2.YourCountry = "USA";
  33.  
  34.  
  35.         /* Alex: I'll once again be boring and read a small lecture. :) While it is tempting to give variables short names
  36.         (a1, a2, a3 and the like), I think many guides do you a bad favor with giving you this habit. Variables with short, cryptic,
  37.         meaningless names are the main cause to mistakes like one above (I guess here that it was a mistake, and Jon Skeet should've
  38.         been assigned to a3 instead of a2). You really should try and give your variables more meaningful names: even though they're
  39.         longer to type, they make the code much more obvious. Consider the following: */
  40.  
  41.         Class1 juan = new Class1();
  42.             juan.YourName = "Juan";
  43.             juan.YourAge = 26;
  44.             juan.YourCountry = "Argentina";
  45.  
  46.             Class1 alex = new Class1();
  47.             alex.YourName = "Alex";
  48.             alex.YourAge = 20;
  49.             alex.YourCountry = "Russia";
  50.  
  51.             Class1 jonSkeet = new Class1();
  52.             jonSkeet.YourName = "Jon Skeet";
  53.             jonSkeet.YourAge = 40;
  54.             jonSkeet.YourCountry = "USA";
  55.  
  56.         /* Alex: Nothing has changed logic-wise, but the code got somewhat more understandable and much less error-prone. */
  57.  
  58.             // a1 and a2 are references to objects in memory
  59.  
  60.             listing[0] = a1;
  61.             listing[1] = a2;
  62.  
  63.        //Alex: dont' know what your intention was, but I'd put the a3 into the array too. It has room for 3 instances anyway
  64.        listing[2] = a3;
  65.            
  66.  
  67.  
  68.            
  69.  
  70.  
  71.             //listing[1].Print();
  72.  
  73.  
  74.         // Alex: this works so nicely. You'll love it even more when it'll come to inheritance/polymorphism (just teasing)
  75.             for (int i = 0; i < 2; i++)
  76.             {
  77.                 listing[i].Print();
  78.             }
  79.  
  80.  
  81.  
  82.          
  83.             Console.ReadLine();
  84.  
  85.  
  86.  
  87.         }
  88.  
  89.        
  90.     }
  91. }
  92.  
  93.  
  94.  
  95. using System;
  96. using System.Collections.Generic;
  97. using System.Text;
  98.  
  99. namespace ArrayOfObjects
  100. {
  101.     class Class1
  102.     {
  103.  
  104.         // We define our properties
  105.         public string YourName { get; set; }
  106.         public int YourAge { get; set;  }
  107.         public string YourCountry { get; set; }
  108.  
  109.  
  110.  
  111.         public void Print()
  112.     {
  113.  
  114.             //we are making use of string formatting syntax
  115.         Console.WriteLine("{0} with age  {1} from {2} ", YourName, YourAge, YourCountry ); // Alex: string formatting rocks!
  116.     }
  117.  
  118.        
  119.  
  120.     }
  121. }
RAW Paste Data
Top