-4

I am working on my first Windows Form App and I need to pass a string from one function to another. My code is rather long at this point so I made a small example that is reproducing the same error.

All that I am trying to do is take the created string from Create() and pass it into Gets(). I have done this with C++ but I cannot seem to figure it out with C#.

private void Create()
{
    string a;
    a = "This is a test";
    Gets(a);
}

private void Gets(string b)
{
    MessageBox.Show(b);
}

private void button1_Click(object sender, EventArgs e)
{
    Gets();  //error CS1501: No overload for method 'Gets' takes 0 arguments
}
CDspace
  • 2,639
  • 18
  • 30
  • 36
daaaavidv
  • 29
  • 1
  • There is no code that calls "Create" function - not really clear what you trying to do. You may want to show version in C++ that demonstrates your goal. – Alexei Levenkov Mar 06 '17 at 00:30

5 Answers5

3

Try like this :

private void button1_Click(object sender, EventArgs e)
{
        Create();
}

You were trying to call the function Gets() , but it needs a parameter. If you call the function Create(), it will create the string and call Gets() with the created string , so now it can be displayed in the MessageBox.

Nico
  • 12,493
  • 5
  • 42
  • 62
Stanley S
  • 1,052
  • 13
  • 22
  • I know I can cut out the middle man sort to speak and just call the Create method. I simplified the example for this post. I am trying to learn how to pass strings from 1 method to another and then call the 2nd method when needed. Thank you for your input. – daaaavidv Mar 06 '17 at 01:41
1

Your stated error has nothing to do with passing a string by reference - as a C++ developer you should be able to understand the error.

You have one method called Gets that takes a string parameter. You then call it without a string, so the compiler rightfully complains that there is no overload for the method that takes no parameter.

Your solution is to either pass a string (or null) to the existing method, or create an overload that takes no parameters. Be aware that if you pass a null then your MessageBox call will fail, so you may want to substitute a default value.

slugster
  • 49,403
  • 14
  • 95
  • 145
1

Your error is stating it cannot find a method named Gets that takes 0 arguments.

This is the signature of your method:

private void Gets(string b)

That means to call it, you need to pass one argument of type string to it. Therefore, you need to do this when you are calling it:

Gets("hello or whatever you want");

You almost never, though there are times, need to pass an argument by ref. There are rare cases where you may need to, so perhaps refer this answer for details.

Community
  • 1
  • 1
CodingYoshi
  • 25,467
  • 4
  • 62
  • 64
1

I found a solution for what I am looking to do.

Thank you everyone for your input.

        public string Create()
    {
        string a;

        a = "This is a test";

        return a;

    }


    private void Gets()
    {

        string Value = Create();
        MessageBox.Show(Value);

    }



    private void button1_Click(object sender, EventArgs e)
    {

        Gets();
    }
daaaavidv
  • 29
  • 1
-3

It seems to me that you're trying to create some state in the Create method that you want to be able to use in the Gets method, however Gets is triggered from a button click. The answer, if that's the case, is to use a class-level variable to hold the state.

Try this:

private void Create()
{
    b = "This is a test";
}

private string b = null;
private void Gets()
{
    MessageBox.Show(b);
}

private void button1_Click(object sender, EventArgs e)
{
    Gets();
}
Enigmativity
  • 113,464
  • 11
  • 89
  • 172