Sign in to follow this  
Sijmen

[.net] [.NET/C#] Better way of doing this?

Recommended Posts

Hi there, Currently I have this system which behaves like a miniature version of the Windows Registry, and somewhat easyer to use. (And for other purposes.) Now, with my Folder class, you can use this method:
Key key = folder.GetKey("mykey");
If the key 'mykey' does not exist in the folder, the returned key will be null. Now, if you'd like to have it made when it doesn't exist, it's possible to set the overloaded argument bool create to true:
Key key = folder.GetKey("mykey", true);
This is usefull to do this:
folder.GetKey("backColor", true).StringValue = "red";
Now, storing data is very easy. Just add the 'true' argument and set the value. However, getting data is still not nice:
Key key = folder.GetKey("backColor");
if (key == null)
	tehcolor = "red";
else
	tehcolor = key.StringValue;
To solve this, I've added an overload with a default argument. Now, the above code can be written like this:
tehcolor = folder.Getkey("backColor", true, "red");
Nice. -But- now I have got three overloads:
Key GetKey(string name);
Key GetKey(string name, bool create);
Key GetKey(string name, bool create, string defaultValue);
In fact, all these three methods do something different. It would be best to rename them, but it would be nice if the name would be too long (GetOrCreateKey!?). Are there any better ways (other than the overloads) to get this functionality? Thanks, Sijmen

Share this post


Link to post
Share on other sites
Maybe im wrong but isnt this the classic case of get/set/let?

Key GetKey(string name);
stays as is.

Key GetKey(string name, bool create);
becomes
Key SetKey(string name);

and
Key GetKey(string name, bool create, string defaultValue);
becomes
Key LetKey(string name, string defaultValue);

although you have
tehcolor = folder.Getkey("backColor", true, "red");
which is a tpye mismatch since tehcolor is type string and GetKey returns type Key

Alternative:

How about:

bool GetKey(out string tehcolor, string name string defaultValue)

where:
out string tehcolour will adjust the value of tehcolor in your example to null or a key, and

if default value = null then thats the same as setting create = false

it returns true if the key has existed before (ie didnt have to be created)
note: GetKey(out tehcolor, name, null) will return true if the key name exists
.
that doesnt use an overload!

hope that helps

Yratelev

Share this post


Link to post
Share on other sites
Okay, thanks for the replies.

@Yratelev

Yes, there's a type mismatch, but that was just an example. Currently I'm using a KeyValue structure to represent the value.

The out method is a nice idea too, I might want to do that.

@ernow

Yes, I've thought about an indexer, but that gives two problems.

  1. Besides keys, there are also subfolders. A subfoldar and a key can have the same name. Of course, I could implement a key and folder collection, but that seems like a bit of overkill.

  2. With indexers, it itsn't clear whether the item should be created or not if it doesn't exist.



@paulecoyote

That's not entirely the same. The nice thing about this is that it takes only a few lines, and the data on disk is quite compact. Also, this supports easy means of external storage (read: database backend).

Share this post


Link to post
Share on other sites
hm. in c# 2.0 you can have this piece of code:

int? Width = reg.Get("Width");
if(Width.HasValue) {
Console.WriteLine("Width is " + Width.ToString());
}



quite neat for such things, i'd guess.. :D


then again, yes.. xml all the way :D and the predefined config files, and preferences files, they rule... directly load it into variables and save it back, NEVER EVER touch any string :D

Share this post


Link to post
Share on other sites
Have you ever used ASP.Net? You are basically creating something similar to the Session object, so it might be useful to take a look at that.

What you want to do is use the indexer someone sujested. The indexer is a property and has its own get and set methods like any other property. If someone calls the "get" method by trying to retrieve a value it will not create the value if it is missing. If the user calls "set" by trying to set a value that does not exist it will be added.

Also add a folder.Contains(key) that returns whether or not a key is there. Then you implement your defulat values like this:

if (folder.Contains(key))
{
tehcolor=folder[key];
}
else
{
tehcolor = red;
}

I know that looks awfully wordy for what you want, so you might also like to use the "if?then:else" syntax, which would looke like this:

tehcolor=(folder.Contains(key)?folder[key]:red);

The ?: statement will return the value on the left of the colon if it is true, or the value on the right if it is false.

Share this post


Link to post
Share on other sites
Guest Anonymous Poster
You could also throw an exception if the key doesn't exist.

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this