c#类中的Void方法
本文关键字:Void 方法 | 更新日期: 2023-09-27 18:21:21
我在为字符串调用方法void时遇到问题。
我创建了一个类:
public string SendData(string val1){ return val1; }
public void RecoverBirth()
{
clsConnectionClass conn = new clsConnectionClass();
SqlCommand cmd = new SqlCommand();
SqlDataReader dr;
cmd.CommandText = "select * from birthday";
dr = cmd.ExecuteReader();
if (dr.HasRows)
{
while (dr.Read())
{
SendData(dr[1].ToString() + " " + dr[2].ToString() + "'r'n");
}
}
}
}
然后我尝试从类中检索数据
private void button_Click(object sender, EventArgs e)
{
var _test = new Test();
string Result = _test.RecoverBirth();
}
出现此错误:
无法将方法"void"转换为"string"
我需要RecoverBirth方法是Void而不是"字符串",因为它有很多不同的循环,并且不是所有的东西都返回一些东西,如果我将其声明为字符串,我必须使用很多返回null。
有(更好的)方法吗?
public class Test
{
public string StringValue { get; set;}
public void DoSome()
{
while(some code here)
{
//something here
//set StringValue to what you like
this.StringValue = "this is now the text";
}
}
}
然后
private void button_Click(object sender, EventArgs e)
{
var test = new Test();
test.DoSome();
var result = test.StringValue;
}
C#
语言中的void
关键字表示方法不返回任何内容。
您应该使用return
关键字,这意味着一个方法只返回一个值。应该在方法的签名处指定方法的返回值的类型。例如:
public string MethodA(bool isItReally)
在上面的代码中,返回值的类型是string
。
因此,您应该这样更正方法:您的更新代码:
class Test
{
public string RecoverBirth()
{
clsConnectionClass conn = new clsConnectionClass();
SqlCommand cmd = new SqlCommand();
SqlDataReader dr;
cmd.CommandText = "select * from birthday";
dr = cmd.ExecuteReader();
string colValue;
if (dr.HasRows)
{
while (dr.Read())
{
colValue= dr[1].ToString() + " " + dr[2].ToString() + "'r'n");
}
}
return colValue;
}
}
无法将void
方法的值分配给string
变量void
方法不返回任何内容。
您最好从基本。。。编程伙计,这是非常basic
的东西。。
class Test
{
public string s1(string val) { return val; }
public string DoSome()
{
while(some code here)
{
s1(result1 + result2);
}
//Must return string to get as string
return someVariableOfString;
}
}
如果你严格希望你的方法是void
返回类型,那么通过引用out
关键字将参数传递给方法,在这种情况下,方法可以直接更改引用值,这样你就可以在"调用"代码中获得新值。
您可以在MSDN 中了解有关out
的更多信息
public void DoSome(out string result)
{
//Do whatever stuff you want and assign the new value(string in your case) to the parameter
result = "My new string after some processing.";
}
呼叫:
string result;
DoSomething(out result); //After successful execution of this line `result` will have newly assigned string value from within the function
希望这能帮助你。。
忽略所有关于这样的设计有意义的恼人疑虑。。。您可以使用一个参数来发出任何必要的输出。例如:
public void DoSomething(Action<string> someAction = null)
{
// When it's time to emit a result, just do
if (someAction != null) someAction("Hello world!");
}
然后你可以称之为
var sb = new StringBuilder();
DoSomething(s => sb.AppendLine(s));
如果你从一个不关心"返回值"的地方调用,你可以省略这个参数:
DoSomething();
但是说真的,如果你必须做这样的事情,你的设计需要大量的工作。此外,return null
没有任何问题——很难理解为什么这会成为代码中的一个问题。只要用return null;
之类的东西替换掉所有的return;
。但是,如果你关心区别的话,你的方法似乎做了太多的事情。
如果需要方法返回void
,可以使用out
关键字将结果作为引用变量传递,如下所示:
class Test
{
public string SendData(string val1){ return val1; }
public void RecoverBirth(out string result)
{
clsConnectionClass conn = new clsConnectionClass();
SqlCommand cmd = new SqlCommand();
SqlDataReader dr;
cmd.CommandText = "select * from birthday";
dr = cmd.ExecuteReader();
if (dr.HasRows)
{
while (dr.Read())
{
result += SendData(dr[1].ToString() + " " + dr[2].ToString() + "'r'n");
}
}
}
}
你的电话:
private void button_Click(object sender, EventArgs e)
{
var _test = new Test();
string result = null;
_test.RecoverBirth(out result);
}