HellOoo :

my questoin is like this , if I have a function

void hello(string hh){

hh=textbox1.text
}

if I want to take the value of hh from the function 'hello' , to use it in another function , it impossible to make this

void main()
{

hello(hh);
string x=hello(hh); <-wrong
}

so how can I take the value of hh from the function

Thanks

Dani AI

Generated

Several valid ways to get the value of hh out of a method were suggested in this thread. 's return-value suggestion is the simplest and least error-prone. 's ref approach works but, as pointed out, it introduces side effects that can make code harder to reason about. Below are a couple of practical alternatives, a UI-thread caution, and a correction to 's GC remarks.

A clear, explicit pattern is the "Try" + out form when you need to indicate success/failure without throwing exceptions:

string text;
if (TryGetTextFromUi(out text))
{
    // use text
}

bool TryGetTextFromUi(out string value)
{
    if (string.IsNullOrWhiteSpace(textBox1.Text))
    {
        value = null;
        return false;
    }
    value = textBox1.Text;
    return true;
}

For simple flows, a direct return is fine (as suggested). If the value represents object state, expose it via a property or set a private field. Important UI note: read control values only on the UI thread; from a background thread use Invoke/BeginInvoke to avoid cross-thread exceptions:

string ReadTextSafe()
{
    if (textBox1.InvokeRequired)
        return (string)textBox1.Invoke(new Func<string>(() => textBox1.Text));
    return textBox1.Text;
}

On security: 's comment about the garbage collector enabling unauthorized access is incorrect. The GC manages memory; it does not expose data to attackers. Dispose/IDisposable applies to unmanaged resources, not plain string objects. If handling secrets, avoid long-lived immutable strings—use a char[] and clear it when done:

char[] pwd = GetPasswordChars();
// use pwd
Array.Clear(pwd, 0, pwd.Length);

Recommended Answers

All 4 Replies

You can set the variable for passing it into the first method, then use the ref keyword in the parameter field of the method to reference the parameter as the original variable, rather than creating a new copy. Here's an example:

string hh = "Bob";
Hello(hh);
string x = hh;
Console.WriteLine(x);


// ...

private void Hello(ref string hh)
{
	hh = textBox1.Text;
}

The output will be whatever textBox1.Text's text was.

http://msdn.microsoft.com/en-us/library/14akc2c7%28VS.71%29.aspx

hello is returning a void result i would just change that to a string return.

string hh = "";
hh = hello();

string hello()
{
    return textBox1.text;
}

You can set the variable for passing it into the first method, then use the ref keyword in the parameter field of the method to reference the parameter as the original variable, rather than creating a new copy. Here's an example:

string hh = "Bob";
Hello(hh);
string x = hh;
Console.WriteLine(x);


// ...

private void Hello(ref string hh)
{
	hh = textBox1.Text;
}

The output will be whatever textBox1.Text's text was.

http://msdn.microsoft.com/en-us/library/14akc2c7%28VS.71%29.aspx

One thing to be aware of is that passing an object reference can have side effects (in this case, you are using the side effects to get what you want). What this means is that objects/values outside the function can be changed and you might not realize it. Divin757 has what I'd consider a better solution, as there are no side effects from the method.

commented: its not too bad with value types but interesting things can happen with reference types +2

Memory allocation in C# generates some security related problems such as unauthorised access to the data being passed by reference. This is because garbage collector performs the task of deallocating the memory assigned on heap for such variable periodically. In the meantime, some unauthorised access can occur by third party invaders. Data stored on the heap : content of reference-type objects or anything structured inside a reference-type object. An explicit call to Object.Dispose() method by user can be done to avoid this.

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.