What type of object for this?

  • Thread starter Thread starter Brett Romero
  • Start date Start date
B

Brett Romero

I need an object similar to a struct that allows me to write something
like this:

public struct TheFields
{
public static string Hand = "Hand";
public static string PersonID = "PersonID";
}

and reference the fields by name such as TheFields.Hand and get the
value "Hand". The above does all of this. However, I need the
ability to loop blindly through these fields (strings) and get their
values. This construct also needs to be passed in as a parameter to
an object, which will then loop through and get the string values.

I don't think an arraylist will work because I can say myarray.Hand.
I'd have to know what the index of hand is and even then, intellisense
will not do myarray[2].Hand. I need the intellisense to popout all of
the fields within this construct so I can go to the one I want.

Any ideas?

Thanks,
Brett
 
Brett,

I would use a class, and then have static fields as you do.

Additionally, I would have a type constructor which would initialize a
read-only dictionary which you expose, which would use reflection and cycle
through the fields exposed on the type. It would populate with name/values,
if you need that.

Either that, or I would pass the type around.

Hope this helps.
 
Thanks Nicholas. That's working smoothly.

The next thing is I'd like is to some how black box this. The class
that a developer must create, "TheFields" or something similar, allows
for errors and loose formatting. The parsing class that is accepting
TheFields along with the field list inside of TheFields, is blackboxed
but needs the format/structure outlined above to hold. Since
developers are creating TheFields class, they may decide to start
tweaking their implementation or just get it wrong. If that doesn't
happen, the may copy/paste their code wrong, which causes TheFields
class not to work or an invalid format to be thrown into the parsing
class. Basically, I'd like a factory for TheFields. Since there will
be many unique (unique fields I mean) implementations of TheFields (Ex:
TheFieldsPerson, TheFieldsSupervisor, TheFieldsNewHire, TheFieldsCEO,
etc) with their own restricted field list, I need to avoid these manual
implementations from being scattered everywhere. Any ideas?

Brett
 
Brett,

I think that you are going about this the wrong way. I think that for
something like this, you should have an attribute which you can set your
values on, which take a key and a value, and then you pass the type to your
methods that are looking to use it (or get the type from somewhere else).

Then, you use reflection to get the attributes which contain the field
info.
 
One thing that can be done to abstract some of this implementation is
to have the developer write their new Field class and implement the
static string fields as public. That's simple enough. Then they just
inherit from a class that gathers up their fields and basically does
all of the leg work. They will also need to call the base class
constructor.

Still open on better techinques and thoughts.

Thanks,
Brett
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Back
Top