none
Creating a dynamic array in C# RRS feed

  • Question

  • Hi there. Sorry to say my English is not that good. I hope to write it carefully.

    I need a var that looks like this:

    var price_changes_sizes1 = new { size = new { XL = "5", L = "1", M = "-3", S = "-10%" } };
    var price_changes_sizes2 = new { size = new { XS = "1", S = "2", M = "3%" } };
    var price_changes_sizes3 = new { size = new { M = "5", S = "5%" } };
    var price_changes_sizes4 = new { size = new { 2XL = "10", 3XL = "15%" } };

    The problem with this its static. I need to fill in the Sizes dynamically.

    The sizes (array) in the size must be created dynamically. Image from the array below:

    var price_changes_sizes1 = new { size = new { XL = "5", L = "1", M = "-3", S = "-10%" } };
    Array:
    XL "5"
    L "1"
    M "-3"
    S "-10%"

    var price_changes_sizes2 = new { size = new { XS = "1", S = "2", M = "3%" } };
    Array:
    XS "1"
    S "2"
    M "3%"

    ETC.

    It must be in this order (no other way). How can I create an Array in this order in C#

    Many thanx

    Wednesday, January 11, 2017 10:17 AM

All replies

  • Hi,

    I would recommend you to declare a new object named Size with the corresponding properties. You can then do something like this:

            public class Size
            {
                public string XL { get; set; }
                public string L { get; set; }
                public string M { get; set; }
                public string S { get; set; }
            }
            public class OtherClass
            {
                private void MyMethod()
                {
                    var price_changes_sizes1 = new
                    {
                        Size1 = new Size { XL = "5", L = "1", M = "-3", S = "-10%" },
                        Size2 = new Size { XL = "5", L = "1", M = "-3", S = "-10%" }
                    };
                }
            }

    Wednesday, January 11, 2017 12:13 PM
  • Hi AsusT9, thank you for the response. Only I think this is not a solution because Size (in Class) can be like 300 - 400 different sizes. And Sizes value do change all the time. So Size here must be created on a Dynamically way.

    Also I want to add there is a size that is 2XL, 3XL etc:
            public class Size
            {
                public string 2XL { get; set; }
                public string L { get; set; }
                public string M { get; set; }
                public string S { get; set; }
            }
    2XL wont work here.

    • Edited by TheGamer2 Wednesday, January 11, 2017 12:21 PM
    Wednesday, January 11, 2017 12:17 PM
  • Don't believe this is possible without anonymous types e.g.

    Dictionary<int, object> dict = new Dictionary<int, object>();
    dict.Add(1, new { Text = "Azure", Count = 13, Link = "C#" });
    dict.Add(2, new { Count = 13, Text = "HTML", Link = "jQuery" });
    dict.Add(3, new { Counter = 1, Text = "Apples", Link = "Fruit" });
    foreach (KeyValuePair<int, object> entry in dict)
    {
        Console.WriteLine($"{entry.Key} - {string.Join(",",entry.Value)}");
    }


    Please remember to mark the replies as answers if they help and unmark them if they provide no help, this will help others who are looking for solutions to the same or similar problem. Contact via my Twitter (Karen Payne) or Facebook (Karen Payne) via my MSDN profile but will not answer coding question on either.
    VB Forums - moderator
    profile for Karen Payne on Stack Exchange, a network of free, community-driven Q&A sites

    Wednesday, January 11, 2017 12:27 PM
    Moderator

  • Hi #Karen thanks for responding. value of the:

    var price_changes_sizes1 = new { size = new { XL = "5", L = "1", M = "-3", S = "-10%" } };

    Is a object that is send to a webservice wich functionality i cant change. But the above var price_changes_size1 is the only way that the webservice accepts the object on a right way.

    Is there a way to creating a dynamically anonymous types?
    Wednesday, January 11, 2017 12:35 PM
  • There way I see it, and from writing web services data property order does not matter so long as the web service knows the properties being sent. Perhaps you need to re-think how you are passing data if the service is yours, if not discuss this if possible with the author of the service.

    Please remember to mark the replies as answers if they help and unmark them if they provide no help, this will help others who are looking for solutions to the same or similar problem. Contact via my Twitter (Karen Payne) or Facebook (Karen Payne) via my MSDN profile but will not answer coding question on either.
    VB Forums - moderator
    profile for Karen Payne on Stack Exchange, a network of free, community-driven Q&A sites

    Wednesday, January 11, 2017 12:45 PM
    Moderator
  • I did but i cant find the modurator. This is the link of the functionality:

    http://www.bubblecode.net/en/2012/04/20/magento-api-associate-simple-products-to-configurable-or-grouped-product/

    Wednesday, January 11, 2017 12:58 PM
  • Hello,

    I looked at the page provided, can't try it but from what I see there is nothing that indicates you need to do this as you sure in the initial post. Look at the post #10 and the reply along with #24.


    Please remember to mark the replies as answers if they help and unmark them if they provide no help, this will help others who are looking for solutions to the same or similar problem. Contact via my Twitter (Karen Payne) or Facebook (Karen Payne) via my MSDN profile but will not answer coding question on either.
    VB Forums - moderator
    profile for Karen Payne on Stack Exchange, a network of free, community-driven Q&A sites


    Wednesday, January 11, 2017 1:35 PM
    Moderator
  • Karen I did. But the website remains for weeks now at waiting for modurator. I can´t pass this associative array Dynamically in C#. Now for weeks :S Going crazy.

    The only C# code that works for the associative array is just like i mention before:

    var price_changes_sizes = new { color = new { Red = "2" }, size = new { Large = "1", Medium = "-3" } };

    But the above i can't create it Dynamically in C#


    Webservice PHP Like on the website:

    ‘price_changes’ => array(
    ‘color’ => array(
    ‘Red’ => ‘2’,
    ),
    ‘size’ => array(
    ‘Large’ => ‘+1′,
    ‘Medium’ => ‘-3′,
    ),
    ),

    Wednesday, January 11, 2017 1:59 PM
  • Also want to add that that this does not work:

    "var price_changes_sizes = new { color = new { Red = "2" }, size = new { 2XL = "1", 4XL = "30" } };

    Wednesday, January 11, 2017 3:48 PM
  • If you want something "like a array, but with dynamic size", that is called a List<T>.

    As with all generics, you can stack them. So you could make a List<List<T>> (or a list of List<T>'s).

    However the overall format is confusing:
    Why do you have 4 price_changes_sizes variables, instead of one array?
    Why do you store clearly numerical data as strings? Or why do you store strings like "2" and "-10%" in the same array to begin with?
    Why is M once -3, once 3% and once 5?

    Go a few steps back and tell us what your problem is. You might be a on totally wrong track to solve it.


    Remember to mark helpfull answers as helpfull and close threads by marking answers.

    Wednesday, January 11, 2017 5:41 PM
  • As I mentioned before. I am calling a webservice (extra function) for Magento. The Price price_changes value on the webservice is a associative array:

    ‘price_changes’ => array(‘color’ => array(‘Red’ => ‘2’,), ‘size’ => array(‘Large’ => ‘+1′, ‘Medium’ => ‘-3′,),),

    I try everything for this. price_changes is a associative array that needs to be called from the C# project. The only that worked for this was the:

    var price_changes_sizes = new { color = new { Red = "2" }, size = new { Large = "1", Medium = "-3" } };

    The price_changes object in C# can be a object or a string[]. So I need a way to setup this array or list and that the PHP Magento Service sees a associative array.

    Is this clear?

    Wednesday, January 11, 2017 6:30 PM
  • So no one knows how to pass a C# object so that the php webservice can actually read it as a  associative array:

    The only thing that works for (php webservice):
    ‘price_changes’ => array(‘color’ => array(‘Red’ => ‘2’,), ‘size’ => array(‘Large’ => ‘+1′, ‘Medium’ => ‘-3′,),),

    Is (C# object):
    var price_changes_sizes = new { color = new { Red = "2" }, size = new { Large = "1", Medium = "-3" } };

    So there is no other way?

    Thursday, January 12, 2017 8:11 AM
  • Hi TheGamer2,

    Maybe you could use JSON data.

    I make a simple example.

    Here is the json according to your code.

    {"size":
            [{"XL" : "5",  "L" : "1",M:"-3","S":"-10%"} ,
             { "XS" : "1", "S" : "2", "M" : "3%"} ,
             { "M" : "5", S : "5%"},
             { "2XL" : "10", "3XL" : "15%"}
             ]
    }
     


    You could copy it and then click Edit>Paste Special>Paste JSON As Classes. It will generate the classes according to your json.

    Here is the class due to json.

       public class Rootobject
        {
            public Size[] size { get; set; }
        }
    
        public class Size
        {
            public string XL { get; set; }
            public string L { get; set; }
            public string M { get; set; }
            public string S { get; set; }
            public string XS { get; set; }
            public string _2XL { get; set; }
            public string _3XL { get; set; }
        }


    You could add the information you want and it will generate classes directly.

    And then you could the following code to get the information.

     static void Main(string[] args)
            {
                string JSON = @"{'size':
                                       [ 
                                        { 'XL' : 5,  'L' : 1, 'M': -3, 'S':'-10%'} ,
                                        { 'XS' :1, 'S' : 2, 'M' : '3%'} ,
                                        { 'M' : 5, 'S': '5%'},
                                        {'_2XL' : 10, '_3XL' : '15%'}
                                       ]
                               }";
                Rootobject obj = JsonConvert.DeserializeObject<Rootobject>(JSON);
                foreach (var item in obj.size)
                {             
                    Console.WriteLine("L:{0},M:{1},S:{2},XL:{3},XS:{4},2XL:{5},3XL:{6}",item.L,item.M,item.S,item.XL,item.XS,item._2XL,item._3XL);
                }
               
                Console.ReadKey();
            }

    Here is the output.


    For more details, please refer to the Serializing and Deserializing JSON.

    I hope this would be helpful.

    Best Regards,

    Wendy


    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    Wednesday, January 18, 2017 7:18 AM
    Moderator