我已经干了一段时间没有运气了。
我有这个delphi程序,我没有写,也没有原始的程序来测试。请注意以下评论,看看它应该做些什么:
// first parameter is an input string, and the others are returned contents
// parsed. Example: "Okay:C15" would be parsed as "Okay", "C", 15, 0
procedure TestingThis(const astring: string; var aname: string;
var atype: char; var alength: byte; var adecimals: byte);
var
ipos,jpos: integer;
aa: string;
begin
aname:='';
atype:='C';
alength:=1;
adecimals:=0;
aa:=astring;
ipos:=pos(':',aa);
if ipos > 1 then
begin
aname:=copy(aa,1,ipos-1);
aa:=copy(aa,ipos+1,length(aa)-ipos);
atype:=aa[1];
if atype = 'A' then exit;
if atype = 'B' then
begin
alength:=8;
exit;
end;
if atype = 'C' then
begin
alength:=strtoint(copy(aa,2,length(aa)-1));
exit;
end;
if atype = 'D' then
begin
jpos:=pos('.',aa);
if jpos < 1 then
begin
alength:=strtoint(copy(aa,2,length(aa)-1));
adecimals:=0;
end
else
begin
alength:=strtoint(copy(aa,2,jpos-2));
adecimals:=strtoint(copy(aa,jpos+1,length(aa)-jpos));
end;
exit;
end;
end;
end;这是我的C#版本:
public static void TestingThis(string astring)
{
int ipos;
int jpos;
string aa;
string aname = "";
char atype = 'C';
// def
byte alength = 1;
byte adecimals = 0;
aa = astring;
ipos = aa.IndexOf(':');
if (ipos > 0)
{
aname = aa.Substring(0,ipos);
aa = aa.Substring(ipos + 1, aa.Length - ipos - 1);
atype = aa[0];
if (atype == 'L')
{
return;
}
if (atype == 'D')
{
alength = 8;
}
if (atype == 'C')
{
if (Byte.TryParse(aa.Substring(1, aa.Length - 1), out alength)) //Get the last two elements of string and convert to type byte
{
return;
}
}
if (atype == 'N')
{
jpos = aa.IndexOf('.');
if (jpos < 0) // if '.' isn't found in string
{
if (byte.TryParse(aa.Substring(1, aa.Length - 1), out alength))
{
adecimals = 0;
return;
}
}
else
{
if ((byte.TryParse(aa.Substring(2, jpos - 2), out alength)) && (byte.TryParse(aa.Substring(jpos + 1 ,aa.Length - jpos), out adecimals)))
{
return;
}
}
return;
}
}
}我通过给它一个字符串来测试它,比如:
string test = "Okay:C15"
TestingThis(test)不过我很困惑。在Delphi代码中,只有一个参数是输入:astring,其余的应该是返回的值?这怎麽可能?我还没有看到任何关于从我所读取的内容中输入和输出的参数,var关键字意味着它们是通过引用传递的,这意味着我应该在c#版本中使用ref。函数本身应该只调用一次,而输入实际上是一个字符串。
编辑:将我的功能更改为:
public static void TestingThis(string astring, out string aname, out char atype, out byte alength, out byte adecimals)我这样叫它:
string test = "Okay:C15";
string aname;
char atype;
byte alength;
byte adecimals;
TestingThis(test, out aname, out atype, out alength, out adecimals);这是从德尔菲到C#的正确转换吗?
发布于 2016-02-19 18:53:35
如果您想在一个方法中返回多个值,例如,如果您调用了一个方法,并且希望返回某人的年龄,那么这个方法不是由ref返回的。当您只需要使用简单的out param时,为什么要使用ref关键字将年龄更改为返回值年龄。在C#输出参数上进行msdn谷歌搜索,您需要了解参考文献之间的明显区别。然后出去..。另外,如果您想使用作为全局变量,例如,您可以在您的方法中传递那些ref值。但是windows和web是不同的..。因此,当您开始更频繁地在两种情况下编码时,请小心。
https://stackoverflow.com/questions/35511021
复制相似问题