Advice on Data structure for storing mixed type elements

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • kwm123
    New Member
    • Mar 2010
    • 4

    Advice on Data structure for storing mixed type elements

    I am a newbie in programming and I need some advice on storing of multiple types of data together.

    My method takes in a String Array as parameter. This string array basically is an array of Chemical elements.

    Code:
     
    String[] elements = { Calcium, Potassium, Magnesium, Sodium}
    I iterate through each element of the array and call another method that takes in each chemical element as a parameter and calculates 4 values.

    Code:
    for (int i = 0; i < elements.Length; i++)
    {[INDENT]...some lines of code
    GetDerivedPropertyValues(Calcium);[/INDENT]}
    Now before I pass the next element as the function parameter, I need to store the 4 values calculated by the method for that corresponding element.

    For this reason, I think I may need an ArrayList ( I could be completely wrong though) which stores the first element as a string type i.e the Element Name, and the remaining 4 double values.

    Alternatively, I can also create a double array of size 4 to store the 4 calculated values and call each array by the element name

    for example,
    Code:
    double[] calcium = new double[4]
    I dont know how to programmaticall y name the array according to each element of the initial string array.

    Please help me and excuse my lack of knowledge if this is a well documented scenario but I simply didn't knew where to look.

    Thanks.
  • tlhintoq
    Recognized Expert Specialist
    • Mar 2008
    • 3532

    #2
    I think I would make a class that contains everything together.


    Code:
    class ChemElem
    {
       public string name;
       public string value1;
       public string value2;
       public string value3;
    
       public Calculate()
       {
           // Do work here
       }
    }
    Now you can can make a new ChemElem.
    Set the name
    Call the Calculate() method which will populate the values.
    Then send the ChemElem around your program as needed, and it will have the name and values together.

    Comment

    • kwm123
      New Member
      • Mar 2010
      • 4

      #3
      Hi tlhintoq , Thanks for your response. I indeed call the calculate method exposed from another class as you have suggested but the thing is I need to store all the values together in a datastructure such as an array or a list as my further calculations require operations such as comparision and other mathematical interactions of related values of different chemical elements´.

      So any more suggestions and advices for storing together strings and doubles of each chemical element?

      Comment

      • tlhintoq
        Recognized Expert Specialist
        • Mar 2008
        • 3532

        #4
        What was wrong with the suggestion of using a class?

        Comment

        • kwm123
          New Member
          • Mar 2010
          • 4

          #5
          I think using class is an elegant solution for object oriented approach of calculating multiple values and code reuse. But how about storing those values together for not one but multiple calculations.

          For Example, I have a method exposed by a class that takes in the element name and the temperature and calculates multiple temperature dependent properties. For example, when I call a function exposed by a class,
          Code:
           GetDerivedProperties(Titanium, 1800)
          I get 4 resulting double values:
          1. Partial pressure
          2. Partial Molar Volume
          3. Partial Density
          4. Partial Viscosity

          Now, what do you think is the best way to store these 4 values of different elements together? For instance, if I call 7 times, the GetDerivedPrope rties(element, temp) function for 7 different elements and store the values of each element?

          Comment

          Working...