C# - Selecting specific items in an array element with multiple values

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • Cy0nide
    New Member
    • Oct 2007
    • 1

    C# - Selecting specific items in an array element with multiple values

    Hello all,

    I'm new to C# and I'm hitting a brick wall on this.

    I've skimmed through the posts regarding arrays and their elements and was unable to find the information I was looking for, so hopefully I'm not blind and someone will be kind enough to help me out :)

    Using a standard 2D array I need to assign it a new class instance that contains several values. My setup needs to be like below, so dictionaries etc. won't help me much and they're a bit over my head right now anyway.

    object[] testArray = new object[2];

    testArray[0] = new className(100, 1.5, 3);
    testArray[1] = new anotherClass(20 0, 3.5, 4);

    I need to be able to go through each array index and pull out the third item in each element of that index. So I would need to pull out the 3 or the 4 for each of these two indexes.

    I've tried running through the Array methods, but haven't come up with anything so far that I could use. I'm not looking for anyone to code it for me, as that would be silly, but if you could provide a point in the right direction that would be great :)

    Thanks in advance!
  • sanYAua007
    New Member
    • Oct 2007
    • 6

    #2
    Originally posted by Cy0nide

    object[] testArray = new object[2];
    testArray[0] = new className(100, 1.5, 3);
    testArray[1] = new anotherClass(20 0, 3.5, 4);
    Hello CyOnide!

    1.If both of classes contain three values of the same type(looking at their constructors it seems so) you should use one class (f.e. object).
    Code:
    class object
    {
        public:
           object::object(int v1,double v2,int v3):val1(v1),val2(v2),val3(v3){} //constructor 
           int val1;
           double val2;
           int val3;   //class fields
    };
    2. If it is not so - classes className and anotherClass have to be derived from class object if you want to use them as testArray's array fields, because the array's elements type is object.
    Code:
    class className: public object 
    {
         public:
            className::className(int v1, double v2, int v3):object(v1,v2,v3); //constructor
            .....//other fields or methods
    };
    
    class anotherClass: public object
    {
    //------------------------//the same as in className
    };
    3. To reach third value of each class from array just use next code
    Code:
    testArray[i]->val3;
    Good luck!

    Comment

    • Shashi Sadasivan
      Recognized Expert Top Contributor
      • Aug 2007
      • 1435

      #3
      since the array is storing objects rahter than another set of arrays or any properly structured class, it will return an object type which you have to cast back to its original class.
      If you make all the classes that you are entering into this array inherit an Interface which will have a method of fetching the 3rd value, then you can avoid the speggeti code that you would otherwise be entering into.

      cheers and good luck

      Comment

      Working...