none
error messages about assigning value to string array in c# RRS feed

  • Question

  • Hi

         I am learning C#. i dont understand what i am doing wrong in the following code in line 25. i know there are other ways to assign value to str1 string array. but all of them require assigning values one by one to all the elements of array. i want a shortcut like line 25 so that we dont have to write each variable of str1 manually

      24) string[,] str1 = new string[2, 4] { { "11", "22", "zz", "ss" }, { "22", "033", "pp", "qq" } };
                    
      25)  str1 = { { "11", "2", "33", "44"}, { "22", "033", "a", "b"} };

    I am getting following error messages

    Severity Code Description Project File Line Suppression State
    Error CS1002 ; expected helloworld C:\Users\hp\Documents\Visual Studio 2015\Projects\helloworld\helloworld\Program.cs 25 Active
    Error CS1002 ; expected helloworld C:\Users\hp\Documents\Visual Studio 2015\Projects\helloworld\helloworld\Program.cs 25 Active
    Error CS1002 ; expected helloworld C:\Users\hp\Documents\Visual Studio 2015\Projects\helloworld\helloworld\Program.cs 25 Active
    Error CS1002 ; expected helloworld C:\Users\hp\Documents\Visual Studio 2015\Projects\helloworld\helloworld\Program.cs 25 Active
    Error CS1002 ; expected helloworld C:\Users\hp\Documents\Visual Studio 2015\Projects\helloworld\helloworld\Program.cs 25 Active
    Error CS1002 ; expected helloworld C:\Users\hp\Documents\Visual Studio 2015\Projects\helloworld\helloworld\Program.cs 25 Active
    Error CS1002 ; expected helloworld C:\Users\hp\Documents\Visual Studio 2015\Projects\helloworld\helloworld\Program.cs 25 Active
    Error CS1002 ; expected helloworld C:\Users\hp\Documents\Visual Studio 2015\Projects\helloworld\helloworld\Program.cs 25 Active
    Error CS1002 ; expected helloworld C:\Users\hp\Documents\Visual Studio 2015\Projects\helloworld\helloworld\Program.cs 25 Active
    Error CS1525 Invalid expression term '{' helloworld C:\Users\hp\Documents\Visual Studio 2015\Projects\helloworld\helloworld\Program.cs 25 Active

    Tuesday, March 31, 2020 11:25 AM

Answers

  • Here is a solution

    var str1 = new string[2, 4]
    {
        { "11", "22", "zz", "ss" }, 
        { "22", "033", "pp", "qq" }
    };
    
    str1 = new string[2, 4]
    {
        { "11", "2", "33", "44"}, 
        { "22", "033", "a", "b"}
    };

    If the intent was to change a element then index down to the element you want to change and assign a new value.

    For learning, iterate and find/change a value

    var testStringArray = new string[2, 4]
    {
        { "11", "22", "zz", "ss" },
        { "22", "033", "pp", "qq" }
    };
    
    testStringArray[0, 1] = "Karen";
    for (int rowIndex = 0; rowIndex < testStringArray.GetLength(0); rowIndex++)
    {
        for (int columnIndex = 0; columnIndex < testStringArray.GetLength(1); columnIndex++)
        {
    
            var item = testStringArray.GetValue(rowIndex, columnIndex).ToString();
            if (item == "zz")
            {
                testStringArray[rowIndex, columnIndex] = "ZZ";
            }
    
            Console.WriteLine($" Row: {rowIndex} column: {columnIndex} Value: {testStringArray.GetValue(rowIndex, columnIndex)}");
        }
        Console.WriteLine();
    }
    
    Console.WriteLine();


    Please remember to mark the replies as answers if they help and unmarked 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.

    NuGet BaseConnectionLibrary for database connections.

    StackOverFlow
    profile for Karen Payne on Stack Exchange



    Tuesday, March 31, 2020 1:07 PM
    Moderator
  • The curly braces followed by the element values is an array initialization expression. In C# you can only use the array initializer syntax when newing up an object. To subsequently change a value you need to either use the standard array index syntax (arr[10]) or create a new array using new and then the initializer expression works again. 

    If you need to change most of the elements in the array (and a for loop won't work) then creating a new array is the cleaner approach. If you just need to change a couple of values use array indexing.

    So in regards to what you want, you have to new up a new array to use the syntax you want.


    Michael Taylor http://www.michaeltaylorp3.net

    • Proposed as answer by Naomi NModerator Tuesday, March 31, 2020 3:05 PM
    • Marked as answer by Adil1972 Wednesday, April 1, 2020 9:02 AM
    Tuesday, March 31, 2020 1:44 PM
    Moderator

  •      I am learning C#. i dont understand what i am doing wrong in the following code in line 25. 

      24) string[,] str1 = new string[2, 4] { { "11", "22", "zz", "ss" }, { "22", "033", "pp", "qq" } };
                    
      25)  str1 = { { "11", "2", "33", "44"}, { "22", "033", "a", "b"} };


    You need to keep in mind the difference between initialization and assignment.
    The bracketed values are correct syntax for array *initialization*, but not for
    (post-creation) *assignment*.

    Initialization - as the term implies - is done at the time an object is
    declared/created. It is the provision of the *initial* value(s) for the object.

    When you use the assignment operator (=) with an already existing object you
    are doing an *assignment*, and the syntax used for array *initialization* is
    not valid.

    - Wayne

    • Proposed as answer by Naomi NModerator Tuesday, March 31, 2020 3:05 PM
    • Marked as answer by Adil1972 Wednesday, April 1, 2020 9:02 AM
    Tuesday, March 31, 2020 2:00 PM
  • Hi Adil1972,
    As WayneAKing said, you need to know the difference between initialization and assignment.
    Initialization of arrays of strings: Arrays can be initialized after the declaration. It is not necessary to declare and initialize at the same time using the new keyword. However, initializing an array after the declaration, it must be initialized with the new keyword. It can’t be initialized by only assigning values.
    Here is a code example:

    // Declaration of the array
    string[] str1, str2;
    // Initialization of array
    str1 = new string[2,4] { { "11", "2", "33", "44"}, { "22", "033", "a", "b"} };
    str2 = new string[,] {  { "11", "22", "zz", "ss" }, { "22", "033", "pp", "qq" }};

    Note: Initialization without giving size is not valid in C#, it will give compile time error.
    Best Regards,
    Daniel Zhang


    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.

    • Marked as answer by Adil1972 Wednesday, April 1, 2020 11:32 AM
    Wednesday, April 1, 2020 2:10 AM

All replies

  • Here is a solution

    var str1 = new string[2, 4]
    {
        { "11", "22", "zz", "ss" }, 
        { "22", "033", "pp", "qq" }
    };
    
    str1 = new string[2, 4]
    {
        { "11", "2", "33", "44"}, 
        { "22", "033", "a", "b"}
    };

    If the intent was to change a element then index down to the element you want to change and assign a new value.

    For learning, iterate and find/change a value

    var testStringArray = new string[2, 4]
    {
        { "11", "22", "zz", "ss" },
        { "22", "033", "pp", "qq" }
    };
    
    testStringArray[0, 1] = "Karen";
    for (int rowIndex = 0; rowIndex < testStringArray.GetLength(0); rowIndex++)
    {
        for (int columnIndex = 0; columnIndex < testStringArray.GetLength(1); columnIndex++)
        {
    
            var item = testStringArray.GetValue(rowIndex, columnIndex).ToString();
            if (item == "zz")
            {
                testStringArray[rowIndex, columnIndex] = "ZZ";
            }
    
            Console.WriteLine($" Row: {rowIndex} column: {columnIndex} Value: {testStringArray.GetValue(rowIndex, columnIndex)}");
        }
        Console.WriteLine();
    }
    
    Console.WriteLine();


    Please remember to mark the replies as answers if they help and unmarked 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.

    NuGet BaseConnectionLibrary for database connections.

    StackOverFlow
    profile for Karen Payne on Stack Exchange



    Tuesday, March 31, 2020 1:07 PM
    Moderator
  • The curly braces followed by the element values is an array initialization expression. In C# you can only use the array initializer syntax when newing up an object. To subsequently change a value you need to either use the standard array index syntax (arr[10]) or create a new array using new and then the initializer expression works again. 

    If you need to change most of the elements in the array (and a for loop won't work) then creating a new array is the cleaner approach. If you just need to change a couple of values use array indexing.

    So in regards to what you want, you have to new up a new array to use the syntax you want.


    Michael Taylor http://www.michaeltaylorp3.net

    • Proposed as answer by Naomi NModerator Tuesday, March 31, 2020 3:05 PM
    • Marked as answer by Adil1972 Wednesday, April 1, 2020 9:02 AM
    Tuesday, March 31, 2020 1:44 PM
    Moderator

  •      I am learning C#. i dont understand what i am doing wrong in the following code in line 25. 

      24) string[,] str1 = new string[2, 4] { { "11", "22", "zz", "ss" }, { "22", "033", "pp", "qq" } };
                    
      25)  str1 = { { "11", "2", "33", "44"}, { "22", "033", "a", "b"} };


    You need to keep in mind the difference between initialization and assignment.
    The bracketed values are correct syntax for array *initialization*, but not for
    (post-creation) *assignment*.

    Initialization - as the term implies - is done at the time an object is
    declared/created. It is the provision of the *initial* value(s) for the object.

    When you use the assignment operator (=) with an already existing object you
    are doing an *assignment*, and the syntax used for array *initialization* is
    not valid.

    - Wayne

    • Proposed as answer by Naomi NModerator Tuesday, March 31, 2020 3:05 PM
    • Marked as answer by Adil1972 Wednesday, April 1, 2020 9:02 AM
    Tuesday, March 31, 2020 2:00 PM
  • Thank you very much everybody.

    i will go through it first thing tomorrow morning, as i am going sleep now.

    Tuesday, March 31, 2020 2:58 PM
  • Hi Adil1972,
    As WayneAKing said, you need to know the difference between initialization and assignment.
    Initialization of arrays of strings: Arrays can be initialized after the declaration. It is not necessary to declare and initialize at the same time using the new keyword. However, initializing an array after the declaration, it must be initialized with the new keyword. It can’t be initialized by only assigning values.
    Here is a code example:

    // Declaration of the array
    string[] str1, str2;
    // Initialization of array
    str1 = new string[2,4] { { "11", "2", "33", "44"}, { "22", "033", "a", "b"} };
    str2 = new string[,] {  { "11", "22", "zz", "ss" }, { "22", "033", "pp", "qq" }};

    Note: Initialization without giving size is not valid in C#, it will give compile time error.
    Best Regards,
    Daniel Zhang


    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.

    • Marked as answer by Adil1972 Wednesday, April 1, 2020 11:32 AM
    Wednesday, April 1, 2020 2:10 AM