rob*_*pas 0 .net c# arrays return-value
我有以下代码:
public object[] Dispatch(string arg)
{
int time;
int i = 0;
object[] array = new object[10];
if (int.Parse(arg) >= 0 && int.Parse(arg) <= 20)
{
array[i] = new ComputeParam(int.Parse(arg));
}
else
{
if (arg[0] == '/' && arg[1] == 't')
{
Options opt = new Options();
time = opt.Option(arg);
}
}
return array;
}
Run Code Online (Sandbox Code Playgroud)
我将参数传递给我的程序,ArgsParser
如果它们是数字则将它们放入数组中,或者如果参数类似,则设置延迟时间/t:=Max
.问题是我需要数组和时间,我不能返回两个值.我怎样才能解决这个问题?
您可以使用返回类,只需创建一个自定义对象:
public class DataRC {
public object[] aObj { get; set;}
public DateTime dtTime {get; set;}
}
Run Code Online (Sandbox Code Playgroud)
并改变你的功能,所以:
public class ArgsParser
{
public DataRC Dispatch(string arg)
{
DataRC dResult = new DataRC();
int time;
int i = 0;
dResult.aObj = new object[10];
if (int.Parse(arg) >= 0 && int.Parse(arg) <= 20)
{
dResult.aObj[i] = new ComputeParam(int.Parse(arg));
}
else
{
if (arg[0] == '/' && arg[1] == 't')
{
Options opt = new Options();
// Is this where you need the time?
dResult.dtTime = opt.Option(arg);
}
}
return dResult;
}
}
}
Run Code Online (Sandbox Code Playgroud)