This is just an example i have to do a simillar thing on a much larger code , but my question is why doesn t this work? I solved my problem with a string method and return statements but why can t i use a void method with the result parameter?

private void button1_Click(object sender, EventArgs e)
        {
           Method(textBox1.Text);

        }
        public  void  Method(string c)
        {
        string a = "4";
        string b = "3";
        c = a + b;

        }

Recommended Answers

All 4 Replies

Sorry, I don't understand what you really want to do exacly.

So i have a void method where you have to concetenate "add 4 and 3" and the result is c=43
and i want the textbox to become the c . So upon clicking the button i want to get in my textbox 43

c is a local variable, it doesn't exist outside the method. What is happening is you are passing in a string (the textbox) then changing what c is referencing, not the value that it is referencing. This has no effect on the Textbox reference.

Returning a value is the prefered method, it's bad design otherwise :) If you really must do it this way, look up the 'ref' keyword on MSDN.

Try

    private void button1_Click(object sender, EventArgs e)
    {
        Method(textBox1);
    }
    public void Method(TextBox t)
    {
        string a = "4";
        string b = "3";
        t.Text = a + b;
    }

or

    private void button1_Click(object sender, EventArgs e)
    {
        Method();
    }
    public void Method()
    {
        string a = "4";
        string b = "3";
        textBox1.Text = a + b;
    }
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.