Click here to Skip to main content
16,017,788 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
i have a method which takes a value from a database and pass the value to a string in a method argument. But it always result to 0. There is a table called account in my database with a column called [New Balance]. I have a query to retrieve a value from this column which produce an output of 500 and pass the value to string in a method argument so when i call the method i can pass the value from the string to a textbox.

What I have tried:

C#
public void AVinfo(string str)
        {
            
            decimal paValue;
            try
            {
                con = new SqlConnection(DAL.DBConn);
                con.Open();
                string ct = "SELECT [New Balance] From Account Where WithdrawalID = (SELECT Max(WithdrawalID) From Account)";
                cmd = new SqlCommand(ct);

                cmd.CommandType = CommandType.Text;
                cmd.CommandText = ct;
                cmd.Connection = con;

                DataTable dt = new DataTable();
                SqlDataAdapter da = new SqlDataAdapter();
                da.SelectCommand = cmd;
                da.Fill(dt);
                
                con.Close();
                string newval = dt.Rows[0]["New Balance"].ToString();
                str = newval.ToString();
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message, "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
            }

When i call this method i want to put the value of str in textbox.text
like
C#
AVinfo(textbox.text)
Posted
Updated 22-Feb-19 21:02pm
v2

That's because parameters are always passed by value, not reference. And if you think about it, that makes a lot of sense.
If you write a method like this:
C#
private void Double (int x)
   {
   x = x + x;
   }
You could call it like this:
C#
int i = 666;
Double(i);
And you might expect i to be 1332 after that. But if that worked, what would happen if you did this:
C#
Double(666);
Clearly, you don't want to try and change the value of constants!
So when you pass a value - from whatever source - to a method as a parameter, it is copied, and the copy is passed instead: this is called "passing by value" instead of "passing by reference" where the actual item is passed over.
Not you can call Double with a constant or a variable because it doesn't matter - the value outside the method will not be affected by any changes inside it - because it's a copy that is passed, not the "real thing".

You method has the same thing: your string is passed by value into the method - a copy is sent - and any changes are not passed back to the outside world, so the value you give to str is discarded when the method exits.

There are two ways to "fix" that: pass it by reference, or return it as a value.
To pass by reference, you do this:
public void AVinfo(ref string str)
And call it like this:
C#
string str = "";
AVinfo(ref str);
And now changes to str inside your method will affect the external variable.

Since you don't use the existing value, you could do that with an out parameter:
C#
public void AVinfo(out string str)
And call it like this:
C#
string str;
AVinfo(out str);

But the better way is to return the value instead of changing it.
C#
public string AVinfo()
   {
   ...
   return str;
   }

And call it like this:
C#
string str = AVinfo();
 
Share this answer
 
Comments
MaximusDebois 23-Feb-19 3:12am    
thanks man it worked.
OriginalGriff 23-Feb-19 3:26am    
You're welcome!
You probably want this:
textbox.text = AVinfo();

public string AVinfo()
{
  // ... your code
  return str;
}

Other ways are possible, pass a parameter by reference: https://www.dotnetperls.com/parameter[^]
 
Share this answer
 
v2

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900