Help with multidimensional array please

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • SquidgeyBall
    New Member
    • Apr 2010
    • 3

    Help with multidimensional array please

    I have created a multidimensiona l array as follows:

    string[,] myarray = {{"TestArray1Pa rt1", "TestArray1Part 2"},{"TestArray 2Part1", "TestArray2Part 2"}};

    Basically what I want to do is show a message box which will display the first two parts of the array (TestArray1Part 1 & TestArray1Part2 ), then loop to show the next parts.

    I am currently using a foreach statement to loop through the array but this just shows each item one by one.

    Do you know how to achieve this so both parts are shown on each loop of the foreach statement?
  • tlhintoq
    Recognized Expert Specialist
    • Mar 2008
    • 3532

    #2
    Combine the two parts into one string, then show it

    Code:
    string Message = Part1 + System.Environment.NewLine + Part2;
    string Caption = "Iteration: " + LoopCounter.ToString();
    MessageBox.Show(Message, Caption";

    Comment

    • SquidgeyBall
      New Member
      • Apr 2010
      • 3

      #3
      Originally posted by tlhintoq
      Combine the two parts into one string, then show it

      Code:
      string Message = Part1 + System.Environment.NewLine + Part2;
      string Caption = "Iteration: " + LoopCounter.ToString();
      MessageBox.Show(Message, Caption";
      Thank you for the help, I am just a bit lost on how to display both of the parts below:

      string[,] myarray = {{"TestArray1Part 1", "TestArray1Part 2"},{"TestArray2P art1", "TestArray2Part 2"}};

      In the same foreach statement. At present I am using the following statement:

      foreach (string bothparts in myarray)
      {
      MessageBox.Show (bothparts);
      }

      Comment

      • tlhintoq
        Recognized Expert Specialist
        • Mar 2008
        • 3532

        #4
        You aren't going to do it with a ForEach (well, you could but personally I think it's sloppy)
        You're going to need to do it with counter in your loop
        for (int Counter = 0)
        So you can get your part1 and part2 individually and add them together.
        MSDN has a tutorial on such things.

        Comment

        • SquidgeyBall
          New Member
          • Apr 2010
          • 3

          #5
          Originally posted by tlhintoq
          You aren't going to do it with a ForEach (well, you could but personally I think it's sloppy)
          You're going to need to do it with counter in your loop
          for (int Counter = 0)
          So you can get your part1 and part2 individually and add them together.
          MSDN has a tutorial on such things.
          http://msdn.microsoft.com/en-us/libr...53(VS.71).aspx
          Perfect, I will give this a go.

          Thanks for the help.

          Comment

          Working...