我正在使用以下代码获得预期值,这些代码工作正常。
List<int> member_spill = new List<int>();
string query1 = "select xyz from spill_donation where reg=148";
using (SqlConnection con = new SqlConnection(CS)) {
con.Open();
SqlCommand cmd = new SqlCommand(query1, con);
using (SqlDataReader reader = cmd.ExecuteReader()) {
while (reader.Read()) {
if (!reader.IsDBNull(0)) {
member_spill.Add(Convert.ToInt32(reader["xyz"]));
}
}
}
foreach (int empty_spill in member_spill) {
long parentid;
Boolean isTrue = false;
int spilled_id = empty_spill;
query="select a.reflection1,a.reflection2 from table_1 a, table_2 b where a.reg = b.xyz and b.xyz="+spilled_id;
DataTable dtspillresult = objDUT.GetDataTable(query);
if (dtspillresult.Rows.Count!=0) {
if(Convert.ToInt32(dtspillresult.Rows[0]["reflection1"]) == 0 && Convert.ToInt32(dtspillresult.Rows[0]["reflection2"]) == 0) {
isTrue=true;
}
if(Convert.ToInt32(dtspillresult.Rows[0]["reflection1"]) == 1 && Convert.ToInt32(dtspillresult.Rows[0]["reflection2"]) == 0) {
isTrue=true;
}
if(Convert.ToInt32(dtspillresult.Rows[0]["reflection1"]) == 0 && Convert.ToInt32(dtspillresult.Rows[0]["reflection2"]) == 1) {
isTrue=true;
}
if (isTrue==true) {
parentid = Convert.ToInt64(dtspillresult.Rows[0]["reg"]);
Label10.Text = parentid.ToString();
break;
}
}
}
}
当我试图使它成为一个返回值的方法时,它显示错误。我是c#.net的新手,所以请忽略我的错误。谢谢你提出解决问题的建议。
private long parentid(long sponsorid) {
long parent_id;
List<int> member_spill = new List<int>();
string query1 = "select xyz from spill_donation where reg=148";
using (SqlConnection con = new SqlConnection(CS)) {
con.Open();
SqlCommand cmd = new SqlCommand(query1, con);
using (SqlDataReader reader = cmd.ExecuteReader()) {
while (reader.Read()) {
if (!reader.IsDBNull(0)) {
member_spill.Add(Convert.ToInt32( reader["xyz"] ));
}
}
}
foreach (int empty_spill in member_spill) {
Boolean isTrue = false;
int spilled_id = empty_spill;
query="select a.reflection1,a.reflection2 from table_1 a, table_2 b where a.reg = b.xyz and b.xyz="+spilled_id;
DataTable dtspillresult = objDUT.GetDataTable(query);
if (dtspillresult.Rows.Count!=0) {
if(Convert.ToInt32(dtspillresult.Rows[0]["reflection1"]) == 0 && Convert.ToInt32(dtspillresult.Rows[0]["reflection2"]) == 0) {
isTrue=true;
}
if(Convert.ToInt32(dtspillresult.Rows[0]["reflection1"]) == 1 && Convert.ToInt32(dtspillresult.Rows[0]["reflection2"]) == 0) {
isTrue=true;
}
if(Convert.ToInt32(dtspillresult.Rows[0]["reflection1"]) == 0 && Convert.ToInt32(dtspillresult.Rows[0]["reflection2"]) == 1) {
isTrue=true;
}
if (isTrue==true) {
parentid = Convert.ToInt64(dtspillresult.Rows[0]["reg"]);
break;
}
}
}
}
return parent_id;
}
当我试图使它成为一个返回值的方法时,它显示错误。我是c#.net的新手,所以请忽略我的错误。谢谢你提出解决问题的建议。
答案 0 :(得分:1)
在该方法中,parent_id
不保证具有当前编写方法的值。唯一一次为parent_id
分配值的时间取决于某些条件。
要解决编译错误,请指定默认值:
private long parentid(long sponsorid)
{
long parent_id = 0; // or some other value.
// other code that conditionally assigns the value.
return parent_id;
}
此方案中的典型模式,如果方法中不保证存在该值,则使用out参数和方法的bool返回值。
long parentId;
if(TryGetParentId(sponsorid, out parentId)
{
// parentId will now have a value
// do work when there is a parentId
}
该方法将看起来像:
private bool TryGetParentId(long sponsorid, out long parentid)
{
// perform work to get the parentid
foreach(x in y)
{
parentid = 99; // actual value would be assigned with your logic
return true;
}
// no value was found. assign a default value to the
// out param of parentid; required for compiler.
parentid = 0;
return false;
}