0

I'm trying to convert string collection type to dictionary that contain various types of keys and values(the value will be a struct most of the time. each string in the string collection will contain the key in the first element and the rest of the string will contain the struct elements(The Values of the Dictionary), of course that the string will contain the right type of each element in the struct we just need to decode the struct elements' type... i tried to do so but i ran into dead end and no possibility... i tried to google it but my case is pretty hard because it suppose to match to a lot of cases in my program... this is what i tried to do

     private static void LoadDictionaryFromString<TKey,TValue>(ref IDictionary<TKey,TValue> argetDict,StringCollection SourceString)
    {
        TargetDict.Clear();

        foreach(string strData in SourceString)
        {
            string[] strElements = strData.Split(';');
            int m = 1;
            // i must initialize 
            object Key = new object();  // when i try =>   TKey Key = new TKey() it's not working
            object Value = new object();   // the same like previous line...
            foreach (var field in typeof(TValue).GetFields())
            {
                // I can't work this out.... i stuck here
                // in this line i'm getting an exception -> out of index...
                TypeDescriptor.GetProperties(TargetDict.Values)[m].SetValue(Value , strElements[m]);
            }
            TypeDescriptor.GetProperties(TargetDict.Keys)[0].SetValue(Key, strElements[0]);

            TargetDict.Add((TKey)Key,(TValue)Value);
        }
    }

I hope some one can help me with this because I struggled with this a lot.

Thanks

I'm adding a sample for the input of the string collection, the string collection has been saved before from the same dictionary, so let's say the current dictionary contains the key and value like this: , the key is the OrderID the sample struct contain the fields:

      struct sampleStruct
      {
          string StockName;
          int Quantity;
          int StockType;
          enum_Status StockStatus;       // the declaration: enum enum_Status { Accepted, Cancel, Executed }
          double Price;
          DateTime TradeTime;
      }

now let's say we have two records that contain the values:

OrderID = 155 (for the key part) and for the struct : Apple , 200, 1, Accepted, 250, 12/05/2013 10:00:00
OrderID = 156 (for the key part) and for the struct : IBM, 105, 1, Executed, 200, 12/05/2013 12:34:10

so the saved string collection will be name StringCollection strCollection;

"155;Apple;200;1;Accepted;250;12/05/2013 10:00:00
156;IBM;105;1;Executed;200;12/05/2013 12:34:10"

now, after a while i have a saved string collection from the kind above and i know that it contains the specified struct above so i run the function like this:

    IDictionary<int, sampleStruct> DictToTarget=new IDictionary<int, sampleStruct>();

    // the Dictionary will be cleared and will contain the data as saved in the string collection
    // the stringCollection will contain the data above
    LoadDictionaryFromString<int, sampleStruct>(ref DictToTarget, strCollection);

I hope that's enough data to help me. Thanks again.

I4V
  • 34,891
  • 6
  • 67
  • 79
TVC
  • 452
  • 1
  • 6
  • 12
  • if you can post small sample of the stringcollection data, we can try it and let you know. – Saravanan May 12 '13 at 11:54
  • What is the input you passed to the function? what I'm suspecting is the input string doesn't have ; – Buddha May 12 '13 at 11:54
  • Are those keys from predefined types? Can you do something like switch statement on those keys and instatiate the right struct? – Shahar G. May 12 '13 at 11:58
  • Why don't you use a known serializer (*XmlSerializer, JavaScriptSerializer, SoapFormatter, BinaryFormatter, DataContractSeralizer, DataContractJsonSerializer, Json.Net, Protobuf etc.*) and try to create a custom one? – I4V May 12 '13 at 21:08
  • Well, I do it in the way i explained before because I save the data in the easiest way on c# , to the properies.settings. and i can't actually save Dictionary, the only accepted way is stringCollection. if you have another way to do that i'll be glad to hear.... Thanks – TVC May 12 '13 at 21:42
  • @user2374745 As I said, read about serialization. After a quick search, for ex, http://stackoverflow.com/a/16353877/2345956 – I4V May 12 '13 at 21:47
  • @user2374745 It seems a classical **X-Y problem** : *You want to do X, and you think Y is the best/easiest way of doing so. Instead of asking about X, you ask about Y.* – I4V May 12 '13 at 22:05
  • The main idea was to manage all the saved data in one place, in the settings the the c# suggest, so if i'm using the serialization, can i save it to the same setting? or manage another file to save other data? – TVC May 13 '13 at 09:00
  • Nobody knows the answer? – TVC May 13 '13 at 20:52

0 Answers0