-2

I am attempting to make a method that's used to search for a word in a paragraph that asks the user for the word but I get the error "Argument 2 must be passed with the 'ref' keyword." when trying to call my GetString method from my input.cs. I'm trying to associate the word "find" for the parameter but I'm having a hard time trying to figure this out and would appreciate any help.

Below is my SearchWord method in Program.cs.

        public static void SearchWord(Dictionary<string, int> word)
        {
            string find = "";
            string choice = Input.GetString("\nWhat word are you searching for? ", find);
            if (word.ContainsKey(choice))
            {
                PrintKeyValueBar(choice, word[choice]);
                string[] sentences = GetSpeech().Split(new char[] {'.',' ',',','!','?',':','\n','\t','\r'});
                foreach (string sentence in sentences)
                {
                    if (sentence.Contains(choice))
                    {
                        Console.WriteLine(sentence);
                    }
                }
            }
            else
            {
                Console.WriteLine($"{choice} is not found.");
            }
        }

Thank you in advance for the assistance.

EDIT. Adding the GetString method in Input.cs

public static void GetString(string prompt, ref string value)
        {
            while (true)
            {
                value = GetInput(prompt);
                if (ValidString(prompt))
                {
                    break;
                }
                Console.WriteLine("That's not right");
            }
        }

Class instructions state "Use GetString to get the word from the user!" but when trying to do so I get the error.

I have tried string ref find = "";, ref find = ""; ref string find "";

Zuhan
  • 1
  • 4
  • Welcome to Stack Overflow. Please take the [tour] to learn how Stack Overflow works and read [ask] on how to improve the quality of your question. Then [edit] your question to include the full source code you have and the whole error message you get. – Progman Nov 06 '22 at 18:02
  • Hi Zuhan. Please post your input.cs here, especially in the GetString method. – raah Nov 06 '22 at 18:08
  • You might want to look at other questions like https://stackoverflow.com/questions/1524076/why-i-need-to-use-ref-keyword-in-both-declaration-and-call. You have to call your method with the `key` keyword, as clearly stated in the error message. – Progman Nov 06 '22 at 18:29
  • I'm not sure what you mean by a "key" keyword. I've looked at that question and I still do not understand what needs to be done to fix it. I'm completely new to programming and still learning. Also I added ref before find and it STILL doesn't work. – Zuhan Nov 06 '22 at 18:40
  • @Zuhan Sorry, meant `ref` keyword, not `key` (can't edit the comment anymore). – Progman Nov 06 '22 at 18:46
  • @Zuhan Your method has a return type of `void`. You can't assign any return value to a variable, so `string choice = Input.GetString(...` doesn't work. Remove the `string choice` part and work with the `find` variable instead. – Progman Nov 06 '22 at 18:47
  • Ok, the class instructions state "Use GetString to get the word from the user!" That's what I'm attempting to do and why I'm trying to find help :( – Zuhan Nov 06 '22 at 18:53

1 Answers1

-1

ref is a keyword in C Sharp to pass a value-type varibale like a ref-type variable to a method call. That means, you are able to change the value of your variable inside the method.

So try Input.GetString("\nWhat word are you searching for? ", ref find);.

Howevery, I don't see the purpose of ref in your code sniped.

Maybe you can post your GetString Method as well!

raah
  • 71
  • 2
  • 6
  • The reason the ref is in the GetString method is because the class I am in has it as required, the parameters in the GetString method is "string prompt, ref string value". When I changed the parameter to ref find I now get a "Cannot implicitly convert type 'void' to 'string'" error for the Input.GetString method. I added my GetString to my post. – Zuhan Nov 06 '22 at 18:25
  • 1
    Both a value-type and a reference-type will usually be passed by value in C#. That means that if the receiving method assigns a new value to the parameter, then that change will not be reflected outside of the method, for example with the caller. You can use the `ref` keyword with either a value-type or a reference-type, and it means the parameter is passed "by reference", so the same storage location is used by the receiving method. – Jeppe Stig Nielsen Nov 06 '22 at 18:32