views:

90

answers:

3

I have the following code:

   public static class ScraperMasterUriDetails
    {
        public static Dictionary<Guid, string> MasterUriDetails;

    }

However I've decided that I need to add an integer to the dictionary Dictionary<ScraperMasterUriDetails>, so I thought I'd add properties and a few parameters to the constructor.

But, you can't do that in C#. How do I implement what I'm trying to implement?

EDIT:

A more experienced member edited my post, so I'll leave it how it is. I don't care about using a dictionary (was just the right thing to use at the time)

Essentially I just want a list of three types of data in a structure manner, except I always want to refer to once instance of the class which stores the values, hence static. Now I've always done List of(T) like:

public class WebsiteTitles
{
    public string WebsiteId { get; set; }
    public string Keywords { get; set; }

    public WebsiteTitles(string websiteguid, string keywords)
    {
        WebsiteId = websiteguid;
        Keywords = keywords;
    }

    public WebsiteTitles()
    {
    }
}

And then done the following

           List<WebsiteTitles> _siteTitles = new List<WebsiteTitles>();
            _siteTitles.Add(new WebsiteTitles("blah", "keyword")); 

However in this scenario I want like something similar to the above, but static (don't want to be creating instances etc. I really appreciate all the suggestions, hence why I edited my post to provide more information.

As a note, I'll probably want to use LINQ to extract some records e.g. get record where guid == guid etc. That's about all I'll use it for.

+2  A: 

Singleton.

http://en.wikipedia.org/wiki/Singleton_pattern

Steven Sudit
@Steven Sudit, Ash: Don't do it! http://www.oreillynet.com/cs/user/view/cs_msg/23417. Also: http://sites.google.com/site/steveyegge2/singleton-considered-stupid.
Merlyn Morgan-Graham
@Merl: I'm not big on the design-patterns-for-every-occasion bandwagon, myself, but this seems like a pretty obvious case. Why wouldn't it be a good idea here?
Steven Sudit
+1, @Merlyn: singleton is good solution for the OP's needs.
Markos
@Steven Sudit, Markos: Read those articles. Singletons are overused, and there are arguments against using them, even when the more advanced features could prove useful. The simplest reason, for this particular case, is that a singleton is a static variable, and is usually implemented that way. For this case, slapping a silly name on a static variable is just obfuscating what he is trying to do.
Merlyn Morgan-Graham
@Ash: If you make it more clear exactly why you need parameters, and what you need them for (with some example sample pseudo code, even if it is broken), then we can give you a better idea of whether a singleton is the simplest implementation for your case.
Merlyn Morgan-Graham
Thanks for the replies! I've added additional information within my post. Singleton looks pretty good, but I'll wait and hear everyone out first :)
Ash
@Ash: The "WebsiteTitles" class should be named "Website". Each instance describes properties on one website.
Merlyn Morgan-Graham
@Merl: Thanks for the downvote. As it happens, this is a reasonable case for singletons, since you can pass whatever parameters you like to the constructor of that static instance, whereas you have no such flexibility for a static class. So, on the whole, I find myself disagreeing.
Steven Sudit
@Steven Sudit: No downvote given, even before you said that. I just don't think singletons are all they are cracked up to be.
Merlyn Morgan-Graham
Used singleton, everything has advantages and disadvantages, can't believe it was that easy to implement, using two classes.
Ash
@Merl: I'd really rather you *had* given me the downvote, because I'm just fine with *explained* votes. It's the mystery ones that just annoy me.
Steven Sudit
@Merl: In any case, I don't think we're at all far apart on this. Singletons in specific, and design patterns in general, are overrated.
Steven Sudit
@Ash: Glad you found a reasonable solution to your problem, because that's all that really matters.
Steven Sudit
+1  A: 

Why not just use the collection initializer syntax? http://msdn.microsoft.com/en-us/library/bb384062.aspx

public static class ScraperMasterUriDetails
{
    public static List<WebsiteTitles> MasterUriDetails = new List<WebsiteTitles>()
    {
        new WebsiteTitles()
        {
            WebsiteId = Guid.NewGuid().ToString(),
            Keywords = "programming, fish, popsicles, nihilism",
        },
    };
}

If you need to look up a website by ID, then by all means use a dictionary:

public static class ScraperMasterUriDetails
{
    public static Dictionary<Guid, string> MasterUriDetails = new Dictionary<Guid, string>()
    {
        { Guid.NewGuid(), "programming, fish, popsicles, nihilism" },
        { new Guid("abcdef" /* etc */), "ankles, sprocket, glucose, the moon" },
    };
}

Before Edit:

Please note that a dictionary isn't a list.

However, you can of course add values to a static member at any time. It doesn't have to be defined at compile time:

public class SomeOtherClass
{
    public void SomeNonStaticMethod(Guid key, string subUrl)
    {
        ScraperMasterUriDetails.MasterUriDetails[key] = "http://www.helpmeimstuckinsideasadnsserver.com/" + subUrl;
    }
}

If you need more information than this, please describe the structure you used to "add an int". If you are having problems defining that structure, please explain what you want your dictionary to do. You pass it a GUID, and get back what information?

Merlyn Morgan-Graham
Thanks for the reply, as mentioned above my posted was edited, and it reads that I really want to use a dictionary which isn't the case. I just want something similar to my edited example above, but in a static context.Many thanks for the reply.
Ash
Hi, thanks for the example again, I can't use initializers as the values I want to store are generated after processing within a class. Also the WebsiteTitle class was just an example of how I declare a custom list, I'm still only using ScraperMasterUriDetails and wanting to store a Guid, string and int.
Ash
@Ash: You can use a static constructor, or just have your related class (that has the .Add() code) modify the static variable directly. Static variable doesn't mean immutable, it just means that it isn't bound to a specific instance.
Merlyn Morgan-Graham
+1  A: 

A static constructor is called automatically, not by you. There is no way for it to know what parameters to pass in.

So, either

  1. Set the properties to values in the constructor
  2. Set them later with another method you add e.g. Initialize(int a, ...)
Lou Franco
+1. If you're simply constructing MasterUriDetails, use a static constructor. It makes it obvious what you are trying to do.
Merlyn Morgan-Graham