none
.resx file (Key,Collection<values>)

    Question

  • Is it possible to use .resx file to produce (key, and collection of values) for example
    (key, List<Class1>)
    where Class1 is

    public class Class1
    {
     public Class1(string aID, string aFirstName, string aLastName)
         {
      this._ID = aID;
                    this._firstName = aFirstName;
                    //....
     }
            private string ID;
            public string ID
            {
      get{return _ID;}
            }
            //.....
    }

    //Add this .resx to in a Collection, then
    //Read out through foreach() ?

    Friday, April 10, 2009 3:08 PM

Answers

  • Hello,

    Based on your description, I think you want to create the node that with the type of (String key, String value).   You can refer to the following code snippet to create the Resource.resx file:

    ResXResourceWriter writer = new ResXResourceWriter(@"\..\Resources.resx");

     

    Class1 c1 = new Class1() { ID = "1", FirstName = "Lingzhi", LastName = "Sun" };

    writer.AddResource("Key1", c1.ToString());

    writer.Close();

    ===========================

    public class Class1

    {

        public string ID { get; set; }

        public string FirstName { get; set; }

        public string LastName { get; set; }

     

        public override string ToString()

        {

            return string.Format("{0}, {1}, {2}",

                ID, FirstName, LastName);

        }

    }

     

    In the Resource.Designer.cs file, you can add this method to retrieve the corresponding value:

    internal static string Key1

    {

        get

        {

            object obj = ResourceManager.GetObject("Key1", resourceCulture);

            return ((string)(obj));

        }

    }

     

    Best Regards,
    Lingzhi


    Please remember to mark the replies as answers if they help and unmark them if they provide no help.
    Welcome to the All-In-One Code Framework! If you have any feedback, please tell us.
    Wednesday, April 15, 2009 2:56 AM

All replies

  • Hello,

    It is feasible.  You can use ResXResourceWriter to create the .resx file.  Please remember that the target class inside the generic list should be marked as [Serializable], because these objects need to be serialized when they are saved into the .resx file.   For detail, please see the following code snippet:

    ResXResourceWriter writer = new ResXResourceWriter("MyTest.resx");

    List<MyClass> list1 = new List<MyClass>() { new MyClass() { ID = 1, Name = "1" }};

    List<MyClass> list2 = new List<MyClass>() { new MyClass() { ID = 2, Name = "2" } };

    writer.AddResource("1", list1);

    writer.AddResource("2", list2);

    writer.Close();

     

    [Serializable]

    public class MyClass

    {

        public int ID { get; set; }

        public string Name { get; set; }

    }

     

    For how to retrieve the value in the .resx files, you can see this C# General FAQ:
    http://social.msdn.microsoft.com/Forums/en-US/csharpgeneral/thread/5f66ea10-85be-4cc9-9e52-5affbcbfee7f

     

    Best Regards,
    Lingzhi


    Please remember to mark the replies as answers if they help and unmark them if they provide no help.
    Welcome to the All-In-One Code Framework! If you have any feedback, please tell us.
    Tuesday, April 14, 2009 9:48 AM
  • Hi Lingzhi,
    thanks for your answer, but the main objective for me is to have a Key with mulitple Values associated with resources.resx
    for example
    Key as String1 Values as value1, value2, value3 
    (Key1, Values(aID, aFirstName, aLastName))

    so in another words,
    once I create a resource file of string type,
    from designer I will enter 
    the Key, then its Values
    "myKey1" , "value1, value2, value3"

    Tuesday, April 14, 2009 3:17 PM
  • Hello,

    Based on your description, I think you want to create the node that with the type of (String key, String value).   You can refer to the following code snippet to create the Resource.resx file:

    ResXResourceWriter writer = new ResXResourceWriter(@"\..\Resources.resx");

     

    Class1 c1 = new Class1() { ID = "1", FirstName = "Lingzhi", LastName = "Sun" };

    writer.AddResource("Key1", c1.ToString());

    writer.Close();

    ===========================

    public class Class1

    {

        public string ID { get; set; }

        public string FirstName { get; set; }

        public string LastName { get; set; }

     

        public override string ToString()

        {

            return string.Format("{0}, {1}, {2}",

                ID, FirstName, LastName);

        }

    }

     

    In the Resource.Designer.cs file, you can add this method to retrieve the corresponding value:

    internal static string Key1

    {

        get

        {

            object obj = ResourceManager.GetObject("Key1", resourceCulture);

            return ((string)(obj));

        }

    }

     

    Best Regards,
    Lingzhi


    Please remember to mark the replies as answers if they help and unmark them if they provide no help.
    Welcome to the All-In-One Code Framework! If you have any feedback, please tell us.
    Wednesday, April 15, 2009 2:56 AM