Creating a Class with a variable number of fields

  • Thread starter Thread starter thml300
  • Start date Start date
T

thml300

Guest
I am working in a project that has a certain amount of possible data inputs. These data inputs have incremental amount of information.

An example, for clarity: Inputs can be blocks of data called A, B and C. Block A has got 2 fields - F1 and F2. Block B has got 3 fields - F1, F2 and F3. Block C has got 4 fields - F1, F2, F3 and F4. Fields with equal names represent the same information, so you can say that type C contains type B and so on.

My problem seems like an inheritance problem at first. So, I'm currently using a class to each type of data block, A, B and C, and then inheriting the previous object (C : B, B : A). But the thing is, if I have a lot of these objects I end up with a lot of classes. When i'm creating these data blocks at runtime, I'm always using the constructor for the specific class for that data type. The thing is, each constructor has a different amount of arguments, since the number of fields varies. So i was wondering if I could use some kind of overload in a single class. But if I use a single class with all 4 fields, I end up with unneeded data in almost all data blocks, and my class methods would have to also differentiate between data types somehow.

I was wondering if there is a more polished way to solve my problem. Is there a way to create a single class that will only contain the information needed? I searched around for a bit and found out about dynamic objects, but was unable to wrap my head around how it could (or should) be used. I'm not a very experienced programmer, so I'm pretty sure I'm missing something trivial here...

Thank you in advance.


THML-MSDN FORUM

Continue reading...
 
Back
Top