如何将值传递给方法中的字符串 [英] How to pass a value to a string in a method

查看:115
本文介绍了如何将值传递给方法中的字符串的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个方法,它从数据库中获取值并将值传递给方法参数中的字符串。但它总是导致0.在我的数据库中有一个名为account的表,其中包含一个名为[New Balance]的列。我有一个查询从这个列中检索一个产生500的输出值并将值传递给方法参数中的字符串,所以当我调用方法时,我可以将字符串中的值传递给文本框。



我尝试了什么:



  public   void  AVinfo( string  str)
{

decimal paValue;
尝试
{
con = new SqlConnection(DAL.DBConn );
con.Open();
string ct = SELECT [New Balance] ]来自帐户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, 错误,MessageBoxButtons.OK,MessageBoxIcon.Error);
}



当我调用这个方法时我想把str的值放在textbox.text中

就像

 AVinfo(textbox.text)

解决方案

这是因为参数总是按值传递,不是参考。如果你考虑一下,这很有意义。

如果你写这样的方法:

  private   void   Double  int  x)
{
x = x + x;
}

您可以这样称呼:

  int  i =  666 ; 
Double (i);

之后你可能会认为我是1332。但是如果这样做了,如果你这样做会发生什么:

  Double  666 ); 

显然,你不想尝试改变常量的值!

所以当你传递一个值时 - 从任何来源 - 作为参数的方法,它被复制,而传递副本:这被称为通过值传递而不是通过引用传递实际项目被传递。

不是你可以用常量或变量调用Double,因为它没关系 - 方法之外的值不会受到其中任何变化的影响 - 因为它是传递的副本,而不是真实的东西 。



你的方法有同样的事情:你的字符串通过值传递给方法 - 发送一个副本 - 并且任何更改都不会传递回外部世界,因此当方法退出时,您给 str 的值将被丢弃。



有t我可以修复:通过引用传递它,或者将其作为值返回。

要通过引用传递,请执行以下操作:

 public void AVinfo(ref string str)

并将其称为:

  string  str =  ; 
AVinfo( ref str);

现在改变你方法中的str会影响外部变量。



由于您没有使用现有值,您可以使用out参数执行此操作:

  public   void  AVinfo( out  字符串 str)

并按如下方式调用:

  string  str; 
AVinfo( out str);



但更好的方法是返回值而不是改变它。

  public   string  AVinfo()
{
...
return str;
}



并按如下方式调用:

  string  str = AVinfo(); 


你可能想要这个:

 textbox.text = AVinfo(); 

公共字符串AVinfo()
{
// ...你的代码
return str;
}



其他方式可行,通过引用传递参数: https ://www.dotnetperls.com/parameter [ ^ ]


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:

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

AVinfo(textbox.text)

解决方案

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:

private void Double (int x)
   {
   x = x + x;
   }

You could call it like this:

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:

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:

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:

public void AVinfo(out string str)

And call it like this:

string str;
AVinfo(out str);


But the better way is to return the value instead of changing it.

public string AVinfo()
   {
   ...
   return str;
   }


And call it like this:

string str = AVinfo();


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[^]


这篇关于如何将值传递给方法中的字符串的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆