将结果返回给调用方法

时间:2015-04-30 14:47:12

标签: c# variables methods parameters

将参数传递给方法对我来说没有问题,但是返回一个调用方法,我从来没有做过,也不知道怎么做。例如,我想从我的GetStartOfQuery方法返回soq。如何将这些数据恢复到我的BuildAQuery方法中?

public static string soq = string.Empty;

public static void BuildAQuery(string databaseName)
{
  dao.Database dd;
  dao.DBEngine db = new dao.DBEngine();
  var qd1 = new dao.QueryDef(); 
  GetStartOfQuery(databaseName);
  dd = db.OpenDatabase(SetPath(databaseName));
  qd1.Name = String.Format("qry_PersonalInformation");
  qd1.SQL = String.Format(startOfQuery + "location", "empID");
  dd.QueryDefs.Append(qd1);
}

public static void GetStartOfQuery(string databaseName)
{
  if (VBS.Left(databaseName, 2) == "AC")
  {
    soq = "Select hiredate, terminationdate, employeename, ";
  }
  else 
  {
    soq = "Select hiredate, employeename, timeoffaccrued, timeoffused, ";
  }
}   

2 个答案:

答案 0 :(得分:4)

假设你需要的只是一个字符串:

ExecutionEnvironment.execute()

会做的伎俩

答案 1 :(得分:0)

你应该更好地使用这样的函数:

public static void BuildAQuery(string databaseName)
{
    dao.Database dd;
    dao.DBEngine db = new dao.DBEngine();
    var qd1 = new dao.QueryDef(); 
    soq = GetStartOfQuery(databaseName);
    dd = db.OpenDatabase(SetPath(databaseName));
    qd1.Name = String.Format("qry_PersonalInformation");
    qd1.SQL = String.Format(startOfQuery + "location", "empID");
    dd.QueryDefs.Append(qd1);
}


public static string GetStartOfQuery(string databaseName)
{
  if (VBS.Left(databaseName, 2) == "AC")
  {
    soq = "Select hiredate, terminationdate, employeename, ";
  }
  else 
  {
    soq = "Select hiredate, employeename, timeoffaccrued, timeoffused, ";
  }
  return soq;   
}  

因为void不返回任何内容而你想要返回一个值。如果您不想在string函数中返回它,可以根据需要将var作为outref传递。像:

public static void BuildAQuery(string databaseName)
{
    dao.Database dd;
    dao.DBEngine db = new dao.DBEngine();
    var qd1 = new dao.QueryDef(); 
    string soq = string.Empty.
    GetStartOfQuery(databaseName, soq);
    dd = db.OpenDatabase(SetPath(databaseName));
    qd1.Name = String.Format("qry_PersonalInformation");
    qd1.SQL = String.Format(startOfQuery + "location", "empID");
    dd.QueryDefs.Append(qd1);
}


public static void GetStartOfQuery(string databaseName, out string soq)
{
  if (VBS.Left(databaseName, 2) == "AC")
  {
    soq = "Select hiredate, terminationdate, employeename, ";
  }
  else 
  {
    soq = "Select hiredate, employeename, timeoffaccrued, timeoffused, ";
  }  
}