I have get an error when using a function from a class

user7275921

I have created a Hangman game. But all the code was in the GUI. I have been reading a lot of programming books and I want to use functions and really program object orientated instead of putting everything in the GUI. So I tried creating a function that generates a random word but it doesn't work. It throws an error: An unhandled exception of type 'System.StackOverflowException' occurred in mscorlib.dll.

class GetSecretWord
{
    public static string GetWord()
    {
        string unsortedwords = "master,bother,bucket,legend,method";
        List<string> sortedwords = unsortedwords.Split(',').ToList();

        int index = new Random().Next(sortedwords.Count);
        string secretWord = sortedwords[index];

        return GetWord();
    }
}

What am I doing wrong?

Also, in my Form code:

 private void btn_Play_Click(object sender, EventArgs e)
    {
        string theSecretWord = GetSecretWord.GetWord();
        MessageBox.Show(theSecretWord);
    }

I use a messagebox to test if it works but it doesn't...

John Koerner

Change your return to return secretWord

 public static string GetWord()
 {
    string unsortedwords = "master,bother,bucket,legend,method";
    List<string> sortedwords = unsortedwords.Split(',').ToList();

    int index = new Random().Next(sortedwords.Count);
    string secretWord = sortedwords[index];

    return secretWord;
}

Right now you are recursively calling the function which is causing the stack overflow exception.

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

When I declare a function inside a class I get name error

Why do I get SIGSEGV error when use class function from pointer?

I have an error when using returning data from state in redux

I get a SIGPIPE error when using the write() C function

I get "Cannot call a class as a function" error when I use a class component as a custom header for another component using react-navigation-stack

I have an error using OSMNX: OS error when using ox.graph_from_place

Why do we get error saying "train" and "class" have different lengths while using Knn function in R?

when using object variable from baseController in view i get an error

I have an error when using Firebase with RecyclearView

In Java I'm was doing an ArrayList with my class, but when I'm trying to use a function from side library, I get an error about Provided type

I'm trying to parse data from a webpage using the importXML function in excel, but the data doesn't have a class

Objective - c How to get Data from popToRootViewController class, I have Text field in that class I want that textfield text when screen is popped

how do i get a specific attribute from an inherited class when using an array from another class?

I have node.js application I am setting .env using dotenv, I get the following error when using port

C++: Undefined function error when I have defined the function

I have an error when trying to obtain certain statistics (stats) from the PokeAPI using Axios and Node.js

I have an error in retrieving data from database while using "AND" in "Case When"

I get an error when dialing as a function

When using range function to populate list why do I get list formatting error when printing with %d?

Why do I get an error message when using an observeEvent with this function that works fine when not wrapped in an observer?

Why do I get stack overflow error when I use this function for Merge Sort(using Linked Lists)?

how i can use toggle and function that remove the class from the previous element together only using JavaScript i have tried to Different ways?

why i get "no attirubute error" when i try to call python function from c++

Why when I trying to make a function that delete books from my library I get an Error?

Why do I get different results when using 'hasattr' function on the class and its object for the same attribute?

Why do I get the error "use of deleted function 'class : : class()"

Get methods from class when using a prototype

I have an error when I delete an image from folder

Trying to run app from function and i have an error when i trying to call some variable inside some functinon