1

I have the following class:

public class Content {
    public int Key { get; set; }
    public int Order { get; set; }
    public string Title { get; set; }
}

I have the following function that returns a content type code depending on an id.

protected string getType(string id) {
    switch(id.Substring(2, 2)) {
        case "00": return ("14");
        case "1F": return ("11");
        case "04": return ("10");
        case "05": return ("09");
        default: return ("99");
    }
}

Although the id is not part of the content class the class and function are always used together.

Is there some way I could cleanly fit this function into my class? I was thinking of an enum or something fixed however my knowledge of C# isn't really good enough for me to know how I could do this. I hope someone can give me and example.

Update:

I like the following suggestion:

public static readonly Dictionary<String, String> IdToType = 
    new Dictionary<string, string>
    {
        {"00", "14"},
        {"1F", "11"},
        {"04", "10"},
        {"05", "09"},
        //etc.
    };

but I don't know how I could fit this into my class. Is there anyone out there who could show me? What I would like to be able to do is write something like this:

Content.getType("00")

With data stored in the dictionary as suggested.

Alan2
  • 23,493
  • 79
  • 256
  • 450
  • How is the `Content` class always used with the method? Is the return value of the method stored in `Content`? – Adam Houldsworth May 30 '12 at 13:37
  • I'm not sure what you're getting at. If the `Content` class bears no relationship to the `id` and return value of `getType`, then what do you hope to gain? Obviously you could just throw it into your `Content` class as a static method, but it's not clear what you're trying to achieve. – Kirk Woll May 30 '12 at 13:37
  • 1
    What is the ID identifying? The magic numbers in this code are opaque. – Jeff Watkins May 30 '12 at 13:38
  • is getType supposed to be returning the key for a Content reference? How are the Content objects stored? – Kevin DiTraglia May 30 '12 at 13:42
  • The only connection between the class and the function is that they both apply to content data. Actually the function is used to look at part of the key and based on the part of the key it returns a type. However there are screens that also need to be able to get the type from the function based on the id. – Alan2 May 30 '12 at 13:43
  • @Gemma So the return of this function is stored inside `Content`? – Adam Houldsworth May 30 '12 at 13:43
  • No it's not stored inside Content. I am just thinking to put all the functionality in my class because it's only needed when a person wants to know about the content type. – Alan2 May 30 '12 at 14:02

5 Answers5

5

This may not be what you're looking for, but I'd use a string to string dictionary.

If you want to make it a public static member of your class, that may be what you're after.

ex:

public static readonly Dictionary<String, String> IdToType = 
    new Dictionary<string, string>
    {
        {"00", "14"},
        {"1F", "11"},
        {"04", "10"},
        {"05", "09"},
        //etc.
    };
FishBasketGordo
  • 22,904
  • 4
  • 58
  • 91
DLH
  • 146
  • 6
  • How can I add this to my class? I was looking to keep it with the class as the function is only ever used to find the content type given an input id. – Alan2 May 30 '12 at 13:49
  • +1 I like the Dictionary pattern as opposed to the switch statement. You just have to make sure the appropriate default behavior is implemented if a key is not found. – FishBasketGordo May 30 '12 at 13:49
  • @Gemma - You can incorporate this into the method in my solution. You would just replace the switch statement with a Dictionary lookup. Check out the method `TryGetValue` on MSDN: http://msdn.microsoft.com/en-us/library/bb347013.aspx – FishBasketGordo May 30 '12 at 13:51
  • @DLH - I'm sorry but I don't quite understand. Can you add an example of how I could replace this into my function. – Alan2 May 30 '12 at 13:59
  • You wouldn't put it into your function. You would just use the dictionary instead of the function. – DLH May 30 '12 at 14:12
  • @DLH - Thanks but how do I call it and how would I put this into my class? Sorry to be asking this but I really don't know how to use it. – Alan2 May 30 '12 at 14:26
2

Is something like this what you're looking for?

public class Content
{
    public int Key { get; set; }
    public int Order { get; set; }
    public string Title { get; set; }

    public static string getType(string id)
    {
        switch (id.Substring(2, 2))
        {
            case "00": return ("14");
            case "1F": return ("11");
            case "04": return ("10");
            case "05": return ("09");
            default: return ("99");
        }
    }
}

The method could be called like: Content.getType("00").


Aside: Method names by convention in C# should be Pascal-cased, so your method name should be GetType. As you might have found out, there is already a method on System.Object called GetType, so you probably want to come up with a more descriptive name.

FishBasketGordo
  • 22,904
  • 4
  • 58
  • 91
  • It may be worth noting that it would normally be called `GetType` and the final return statement is unreachable. – Jon Skeet May 30 '12 at 13:41
  • I think this looks good but is there a better way to code this getting of data. I just need something that will return a type given an input id. Also for this to work will I have to have an instance of the class? Can I do this in some static way? – Alan2 May 30 '12 at 13:44
  • @JonSkeet - I couldn't agree with you more. – FishBasketGordo May 30 '12 at 13:44
  • @Jon - Good point about the naming and return not being reachable. Do you think there is a better way to do this other than use a case and a function? – Alan2 May 30 '12 at 13:45
  • 1
    @Gemma - This is the static way. `Content` is the class, not an instance. – FishBasketGordo May 30 '12 at 13:46
1

It seems you need to combine the enum with an extension method...

e.g.

static string Default = "99";
public static readonly Dictionary<string, string> Cache = new Dictionary<string,string>(){
    {"00", "14"},
    {"1F", "11"},
    {"04", "10"},
    {"05", "09"},
    //etc
}
public static getType(Content this){
    if(Cache.ContainsKey(this.typeId)) return Cache[this.typeId];
    else return Default;
}
//Add other types as needed

Or see this post for an example of the Pattern of TypeSafe Enum Pattern : C# String enums

Community
  • 1
  • 1
Jay
  • 3,276
  • 1
  • 28
  • 38
  • Jay. I'm sorry but I don't understand about extension methods. What if I just put the dictionary part in my class file? How could I call it ? – Alan2 May 30 '12 at 14:39
  • Not understanding is no reason to not use it... See MSDN.. http://msdn.microsoft.com/en-us/library/bb383977.aspx You would call it like `MyNameSpace.Cache.TryGetValue("someKey", ref myContent);` or `MyNamespace.Cache["MyString"]`; – Jay May 30 '12 at 14:50
  • Additionally I thought of a way without the Dictionary... see my other answer. – Jay May 30 '12 at 15:16
1

Gemma,

Just to explain @DLH 's answer:

public class Content 
{
 public int Key { get; set; }
 public int Order { get; set; }
 public string Title { get; set; }

 public static readonly Dictionary<String, String> getType = 
       new Dictionary<string, string>
 {
     {"00", "14"},
     {"1F", "11"},
     {"04", "10"},
    {"05", "09"},
    //etc.
 };
}

Will then allow you to do this:

string value = Content.getType["00"];
Blachshma
  • 17,097
  • 4
  • 58
  • 72
  • Sorry but how can I call getType as there is no getType function? – Alan2 May 30 '12 at 14:49
  • @Gemma - Updated the code, changed the dictionary's name to getType. You can now do Content.getType["00"] and it will return "14" – Blachshma May 30 '12 at 14:51
  • Thanks for your help. I wanted to give the credit to the other user but I just needed what you suggested. – Alan2 May 30 '12 at 14:53
1

You can define a class which is Comparable to string...

You can then define an enum with the values you want for the content.

You can then add operator overloading for dealing with string's, int's or long's etc.

You will then add a operator overload for the enum type.

With this approach you will not need the Dictionary and you really don't even need the enum as you would declare const readonly Properties in the Content class such as public static readonly Type0 = "00";

This is actually less than using the Type Safe Enum Pattern although it is similar and it gives you the benefit of being able to declare real constants.

Jay
  • 3,276
  • 1
  • 28
  • 38