开发者

Suggestions for creating an array of objects

I am looking at registry keys for a specific services. So I created a class so i could create my objects for the regKeys for the specific service.

There are multiple services that all have multiple reg keys for that specific service.

My class

class Component
{
    private String regkey;
    private String regpath;

    //regkey
    private String getRegkey(String name)
    { return name; }

    public string regkeyProp
    {
        get { return (regkey); }
        set { regkey = getRegkey(value); }
    }

    //regpath
    private String getRegPath(String name)
    { return name; }

    public string regPathProp
    {
        get { return (regpath); }
        set { regpath = getRegPath(value); }
    }
}

So now i have my class defined so I can create my objects. So one of my services is the sync service. So i created my new object called sync.

Component SYNC= new Component();

My other function scans thought he array and grabs every single regkey & path for the sync service.

When I am b开发者_JAVA百科uilding my Sync object, I am assigning my regkeyProperty.

Sync.regkeyProps = regkeyValue;

So the issue here is that there is many regkeys for one sync service and when it goes though the for loop though the array it only stores the last reg key each time.

I am just stumped how i can accomplish this. I would assume some sort of array?


I am guessing a bit of what it exactly is you are asking but i assume you want to keep a list of more than 1 Component objects. You could achieve this by doing something like this:

List<Component> syncItems = new List<Component>();

foreach (/* not sure how you loop over the items */)
{
  Component sync = new Component();
  sync.regKeyProp = ...
  syncItems.Add(sync);
}

Also as a side note i would suggest choosing a different name from Component as a Component is already the name of some concept that is being used a lot in .NET


Some form of IEnumerable is probably going to be your best bet. Depending on what you want to do once you have them all, you could use List<T> (pretty well my default) or any other concrete implementation of the IEnumberable interface.

Though, with what you've got for your "Component" class, you might (might- so make sure for yourself before you make changes) be better served with a Dictionary<string, List<string>> instead of creating a custom class to hold what amounts to a Key with a List of Values.

You'd get the name like this (to replace your getRegPath)

\\Assuming Components is a Dictionary as mentioned and "name" is the string you pass into your method
Components[name]

That will get you to the element in your dictionary defined by the Key "name" (in your case, the registry key) and you can the access the property like: Components[name].Value[0] or any other method of accessing a specific item in a List<T>

Again, YMMV, so make sure the Dictionary is really best for you before you make that change.


I can't say I completely understand, but here is a suggestion that may or may not help.

Sync.regkeyProps = regkeyValue;

You can use a List and add to the list, instead of using an array.

So you can do

Sync.regkeyProps.Add(regkeyValue) and it would be inserted into the array without worrying about indexes.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜