none
Generic Use of Varying Data

    Question

  • Need a strategy to generically operate on different data sets with re-usable code.  My attempt follows:

            void useDifferentData( short chooseDataSet )
            {
                if ( chooseDataSet == 0 )
                    dataSet0 dataSet = new dataSet0();  //error: embedded statement cannot be a declaration or labeled statement
                if ( chooseDataSet == 1 )
                    dataSet1 dataSet = new dataSet1();  //error:  'dataSet' already defined
    
                //use chosen dataSet.number;
                //use chosen dataSet.arraySet;
                //use chosen dataSet.polygonSet;
            }
    
            public class dataSet0
            {
                public dataSet0()  //constructor
                {
                    short number = 1;
    
                    short[] array1 = { 3, 5, 7 };
                    short[] array2 = { 9, 11, 13 };
                    short[][] arraySet = { array1, array2 };
    
                    Polygon poly1 = new Polygon(); 
                    Polygon poly2 = new Polygon();
                    //define polygon geometries
                    poly1.Name = "abc";
                    poly2.Name = "def";
                    Polygon[] polygonSet = { poly1, poly2 };
                }
            }
    
            public class dataSet1
            {
                public dataSet1()
                {
                    short number = 10;
    
                    short[] array1 = { 30, 50, 70 };
                    short[] array2 = { 90, 110, 130 };
                    short[][] arraySet = { array1, array2 };
    
                    Polygon poly1 = new Polygon();
                    Polygon poly2 = new Polygon();
                    //define polygon geometries - different than in dataSet0
                    poly1.Name = "ghi";
                    poly2.Name = "jkl";
                    Polygon[] polygonSet = { poly1, poly2 };
                }
            }
    

    It may simply be a code organization/context problem...

    Since the different data sets are intended to be static, maybe structs would be better?

    Is there a simpler or better strategy to handle different data sets with the same code?

    Thanks for any suggestion...


    torpedo99

    Saturday, January 12, 2013 3:34 PM

Answers

  • I would stay with classes.  If you want to be able to bind the data you should use properties instead of public variables.

    If all your datasets are going to have the same signature but different data you should probably create an interface to use with your classes.

    • Proposed as answer by Aaron XueModerator Tuesday, January 15, 2013 8:34 AM
    • Marked as answer by Torpedo99 Wednesday, January 16, 2013 2:14 AM
    Saturday, January 12, 2013 3:59 PM

All replies

  • I would stay with classes.  If you want to be able to bind the data you should use properties instead of public variables.

    If all your datasets are going to have the same signature but different data you should probably create an interface to use with your classes.

    • Proposed as answer by Aaron XueModerator Tuesday, January 15, 2013 8:34 AM
    • Marked as answer by Torpedo99 Wednesday, January 16, 2013 2:14 AM
    Saturday, January 12, 2013 3:59 PM
  • Thanks Ken,

    What is the reason for the first error message in  my code?  I followed some sample code to retrieve data from the class but it's not working, how do I get the data from the class?  Is there an obvious context issue?  Should the class be declared in a separate class template file or does that matter?


    torpedo99

    Saturday, January 12, 2013 4:48 PM
  • You cannot change the type of an object by re declaring it.

    This code assumes both DataSet classes implement the IDataSet interface that you would have to create in your code.

    void useDifferentData( short chooseDataSet ) {

    IDataSet dataSet; if ( chooseDataSet == 0 ) dataSet = new dataSet0(); //error: embedded statement cannot be a declaration or labeled statement if ( chooseDataSet == 1 ) dataSet = new dataSet1(); //error: 'dataSet' already defined


    Saturday, January 12, 2013 5:13 PM
  • What would this IDataSet interface look like (more or less) for my code example?

    Alternatively, which documentation would be a good place to start to understand a class interface (I'm not having much luck finding anything)?

    Also, in your first response can you tell me what you mean by "same signature"?


    torpedo99

    Sunday, January 13, 2013 1:30 PM
  • The class examples you gave only create some variables in the constructor of the class.  So in your case you could use this as the interface

    interface IDataSet { }

    By same signature I mean both classes have to inherit from the same interface or class so you can place the 2 different type of classes in a variable

    Sunday, January 13, 2013 1:55 PM