locked
Loop Through array of Array RRS feed

  • Question

  • Not sure why I'm getting error:

    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    
    namespace Word_Search
    {
        class Program
        {
            static void Main(string[] args)
            {
                WordFinder WordFinder = new WordFinder();
    
                //Build Matrix
    
                //Across
                WordFinder.matrix[0, 0] = WordFinder.GenerateChar();
                WordFinder.matrix[0,1] = WordFinder.GenerateChar();
                WordFinder.matrix[0,2] = WordFinder.GenerateChar();
                WordFinder.matrix[0,3] = WordFinder.GenerateChar();
                WordFinder.matrix[0,4] = WordFinder.GenerateChar();
                WordFinder.matrix[0,5] = WordFinder.GenerateChar();
                WordFinder.matrix[0,6] = WordFinder.GenerateChar();
                WordFinder.matrix[0,7] = WordFinder.GenerateChar();
                WordFinder.matrix[0,8] = WordFinder.GenerateChar();
                WordFinder.matrix[0,9] = WordFinder.GenerateChar();
                WordFinder.matrix[0,10] = WordFinder.GenerateChar();
                WordFinder.matrix[0,11] = WordFinder.GenerateChar();
                WordFinder.matrix[0,12] = WordFinder.GenerateChar();
                WordFinder.matrix[0,13] = WordFinder.GenerateChar();
                WordFinder.matrix[0,14] = WordFinder.GenerateChar();
                WordFinder.matrix[0,15] = WordFinder.GenerateChar();
                WordFinder.matrix[0,16] = WordFinder.GenerateChar();
                WordFinder.matrix[0,17] = WordFinder.GenerateChar();
                WordFinder.matrix[0,18] = WordFinder.GenerateChar();
                WordFinder.matrix[0,19] = WordFinder.GenerateChar();
                WordFinder.matrix[0,20] = WordFinder.GenerateChar();
                WordFinder.matrix[0,21] = WordFinder.GenerateChar();
                WordFinder.matrix[0,22] = WordFinder.GenerateChar();
                WordFinder.matrix[0,23] = WordFinder.GenerateChar();
                WordFinder.matrix[0,24] = WordFinder.GenerateChar();
                WordFinder.matrix[0,25] = WordFinder.GenerateChar();
    
                //Display Matrix
                foreach (string s in WordFinder.matrix)
                {
                    Console.WriteLine(s);
                }
    
                //Wait
                Console.ReadLine();
            }
        }
    }
    

    Gives me a error on my Foreach statement:

    System.IndexOutOfRangeException
      HResult=0x80131508
      Message=Index was outside the bounds of the array.
      Source=Word_Search
      StackTrace:
       at Word_Search.Program.Main(String[] args) in C:\Users\Mholmes\source\repos\Word_Search\Word_Search\Program.cs:line 45

    Saturday, October 10, 2020 7:53 PM

All replies

  • Hello,

    Unless we have full source we can't help you as currently WordFinder is unknown to us.


    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

    Saturday, October 10, 2020 8:31 PM
  • missing class:

    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    
    namespace Word_Search
    {
        public class WordFinder
        {
            public String[,] matrix = new string[25,25];
    
            public string GenerateChar(string previous)
            {
                string value = string.Empty;
    
                do
                {
                    Random random = new Random();
                    // random lowercase letter
                    int a = random.Next(0, 26);
                    char ch = (char)('a' + a);
    
                    value = ch.ToString();
    
                } while (value == previous);
    
                return value;
            }
    
        }
    }
    

    Saturday, October 10, 2020 8:46 PM
  • The code you've presented shouldn't even compile.

    WordFinder.matrix[0,25] = WordFinder.GenerateChar();

    No overload for method 'GenerateChar' takes '0' arguments


    Before you can learn anything new you have to learn that there's stuff you don't know.

    Saturday, October 10, 2020 8:49 PM
  • Sorry updated code a little:

    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    
    namespace Word_Search
    {
        class Program
        {
            static void Main(string[] args)
            {
                WordFinder WordFinder = new WordFinder();
    
                //Build Matrix            
                WordFinder.matrix[0, 0] = WordFinder.GenerateChar("");
                WordFinder.matrix[0,1] = WordFinder.GenerateChar(WordFinder.matrix[0, 0].ToString());
                WordFinder.matrix[0,2] = WordFinder.GenerateChar(WordFinder.matrix[0, 1].ToString());
                WordFinder.matrix[0,3] = WordFinder.GenerateChar(WordFinder.matrix[0, 2].ToString());
                WordFinder.matrix[0,4] = WordFinder.GenerateChar(WordFinder.matrix[0, 3].ToString());
                WordFinder.matrix[0,5] = WordFinder.GenerateChar(WordFinder.matrix[0, 4].ToString());
                WordFinder.matrix[0,6] = WordFinder.GenerateChar(WordFinder.matrix[0, 5].ToString());
                WordFinder.matrix[0,7] = WordFinder.GenerateChar(WordFinder.matrix[0, 6].ToString());
                WordFinder.matrix[0,8] = WordFinder.GenerateChar(WordFinder.matrix[0, 7].ToString());
                WordFinder.matrix[0,9] = WordFinder.GenerateChar(WordFinder.matrix[0, 8].ToString());
                WordFinder.matrix[0,10] = WordFinder.GenerateChar(WordFinder.matrix[0, 9].ToString());
                WordFinder.matrix[0,11] = WordFinder.GenerateChar(WordFinder.matrix[0, 10].ToString());
                WordFinder.matrix[0,12] = WordFinder.GenerateChar(WordFinder.matrix[0, 11].ToString());
                WordFinder.matrix[0,13] = WordFinder.GenerateChar(WordFinder.matrix[0, 12].ToString());
                WordFinder.matrix[0,14] = WordFinder.GenerateChar(WordFinder.matrix[0, 13].ToString());
                WordFinder.matrix[0,15] = WordFinder.GenerateChar(WordFinder.matrix[0, 14].ToString());
                WordFinder.matrix[0,16] = WordFinder.GenerateChar(WordFinder.matrix[0, 15].ToString());
                WordFinder.matrix[0,17] = WordFinder.GenerateChar(WordFinder.matrix[0, 16].ToString());
                WordFinder.matrix[0,18] = WordFinder.GenerateChar(WordFinder.matrix[0, 17].ToString());
                WordFinder.matrix[0,19] = WordFinder.GenerateChar(WordFinder.matrix[0, 18].ToString());
                WordFinder.matrix[0,20] = WordFinder.GenerateChar(WordFinder.matrix[0, 19].ToString());
                WordFinder.matrix[0,21] = WordFinder.GenerateChar(WordFinder.matrix[0, 20].ToString());
                WordFinder.matrix[0,22] = WordFinder.GenerateChar(WordFinder.matrix[0, 21].ToString());
                WordFinder.matrix[0,23] = WordFinder.GenerateChar(WordFinder.matrix[0, 22].ToString());
                WordFinder.matrix[0,24] = WordFinder.GenerateChar(WordFinder.matrix[0, 23].ToString());
                WordFinder.matrix[0,25] = WordFinder.GenerateChar(WordFinder.matrix[0, 24].ToString());
    
                //Display Matrix
                foreach (string s in WordFinder.matrix)
                {
                    Console.WriteLine(s);
                }
    
                //Wait
                Console.ReadLine();
            }
        }
    }
    

    Saturday, October 10, 2020 8:56 PM
  • >public String[,] matrix = new string[25,25];

    >WordFinder.matrix[0,25] = WordFinder.GenerateChar(WordFinder.matrix[0, 24].ToString());

    If matrix is 25x25 then the highest index that can be used for either
    dimension is 24.

    - Wayne

    Sunday, October 11, 2020 2:59 AM
  • Hi,

    Has your issue been resolved?

    If so, please click on the "Mark as answer" option of the reply that solved your question, so that it will help other members to find the solution quickly if they face a similar issue.

    Best Regards,

    Timon


    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, October 21, 2020 8:00 AM