0

I have this data and I need to make an object out of it so I can easily read/write to this object.

enter image description here

I tried to make a dictionary and fill it with hashtables but it is very hard to maintain and I don't even know how to modify the data inside the hashtable inside that dictionary.

this is my bad practice:

Dictionary<string,Hashtable> DICTFormsControls = new Dictionary<string,Hashtable>();

 DICTFormsControls[parentfrm] = new Hashtable();
 DICTFormsControls[parentfrm][controlid] = new FormControl(controlnm,parentfrm,controlid,controlpermission);

offcourse the "FormControl" is just a simple class to hold the control proberties.

I need to make it so I can get the data easily and modify it just as easy.

medo ampir
  • 1,850
  • 7
  • 33
  • 57
  • 1
    Just a note: If the internal Hashtable will always contain FormControls and is indexed by the same type, why not make that a Dictionary also? – Sami Kuhmonen Jul 18 '15 at 18:52
  • 2
    If your key is a combination of a string and an integer, as it appears from your example, you're likely better off implementing that structure as a class that implements hashing (or using a `Tuple`) and using *that* as the key to your dictionary rather than trying to manage a multi-dimensional data structure. As it stands, though, it's not clear what you're unhappy about in your example code, or what "easy" means, exactly. – Preston Guillot Jul 18 '15 at 19:00
  • @Preston Guillot , how can I modify "controlpermission" that is a proberty in a class inside a hashtable that is inside the dictionary? in example please – medo ampir Jul 18 '15 at 19:13
  • @medoampir It's starting to sound like your real problem is that you're using the non-generic `HashTable` instead of a generic colleciton, e.g. `HashSet` or `Dictionary`. In your case, using `HashTable`, you'd need to cast the element of the collection before you could access properties of it. e.g. `((SomeClass)someHashTable[someKey]).SomeProperty = someValue`; – Preston Guillot Jul 18 '15 at 22:05

1 Answers1

0

A dictionary already contains a hash table for the key. Try something like this

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace ConsoleApplication1
{
    class Program
    {
        static void Main(string[] args)
        {
            DICTFormsControls dictFormcontrols = new DICTFormsControls();
            dictFormcontrols.Add(controlnm,parentfrm,controlid,controlpermission)

        }
    }
    public class DICTFormsControls
    {
        public static Dictionary<int, DICTFormsControls> dict = new Dictionary<int, DICTFormsControls>();
        public string controlnm { get; set;}
        public string parentfrm { get; set;}
        public int controlid { get; set;}
        public bool controlpermission {get;set;}

        public void Add(string controln, string parentfrm, int controlid, bool controlpermission)
        {
            dict.Add(controlid, new DICTFormsControls() { controlnm = controlnm, parentfrm = parentfrm, controlid = controlid, controlpermission = controlpermission });
        }
    }
}
​
jdweng
  • 33,250
  • 2
  • 15
  • 20