Rob Sobers
Rob Sobers

Reputation: 21135

What type of OO data structure should I use for this string data?

Let's say my program is always passed a string of characters that represents a data table in Wiki syntax. Something like:

  {||Client:||ABC|-|Contact:||Joe Smith|-|Current revision:||1.0||}

I don't want each one of my specific Wiki table objects (e.g., ClientContactTable, CustomerOrderTable, etc.) to know how to parse out | and - and }. So I'd like to write 1 chunk of code that parses the string into some "middle" tier data object that I can pass to the constructor of each specific Wiki table object.

My question is: what should that middle tier object be?

Upvotes: 0

Views: 214

Answers (4)

comingstorm
comingstorm

Reputation: 26117

As others have said, if you want the first name to act as a key, use an associative map (string->string). That should be the cleanest way; it means your parsing code can include a lot of the input checking and error handling that would otherwise be duplicated for each object class.

If you absolutely must include multiple entries with the same name (as in, multiple Contact's for the same Client), you can fall back to a list of (string,string) pairs.

Upvotes: 0

Joel Coehoorn
Joel Coehoorn

Reputation: 415880

You want to implement System.Runtime.IFormatter to create a generic WikiTable serialization formatter. To help you out with this, you can inherit from the System.Runtime.Formatter base class.

Once you've done that, you would use it like this:

Stream msg = GetWikiClientContactTableMessage();
WikiTableFormatter wtf = new WikiTableFormatter(); // nice name ;)
ClientContactTable result = (ClientContactTable)wtf.Deserialize(msg);

Additionally, this code will probably be a little easier to write if you do it more like the XmlSerializer and make the caller pass the expected output type to the constructor. Then you can use reflection to get the properties you're expecting if you need to.

Upvotes: 3

user54650
user54650

Reputation: 4426

I would use a Dictionary or Hash table.

http://msdn.microsoft.com/en-us/library/xfhwa508.aspx

In case the msdn docs are hard to read (always were for me), I'll give a quick overview. Basically, the dictionary takes an object (a string?) as a key, and another object for data.

Dictionary<string,string> d = new Dictionary<string,string>();
d["key1"] = "value";
d["anotherkey"] = "anothervalue";

Upvotes: 0

Jonas K&#246;lker
Jonas K&#246;lker

Reputation: 7837

I'd go with a map from strings to strings.

It's not particularly OO, but it'll do the job, unless there's something about the job I don't know.

Upvotes: 0

Related Questions