【发布时间】:2016-02-19 17:16:07
【问题描述】:
我已经有一段时间没有运气了。
我有这个不是我编写的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,其余的应该是返回值?这怎么可能?我没有看到任何关于一个参数输入和 4 输出的信息从我读到的,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);
这是从 Delphi 到 C# 的正确转换吗?
【问题讨论】:
-
var是 C# 中的一个关键字,implicitly vs explicitly告诉您变量类型是什么.. 如果方法签名具有out参数,您也可以从方法返回尽可能多的值例如var myVar = new DataTable这与明确表示DataTable myVar = new DataTable()相同,并且要在 C# 中通过 ref 传递,您仍然需要在方法签名中声明ref关键字以及在C# MSDN var上进行谷歌搜索如果您希望从您发布的 C# 方法中返回一个值,那么您需要将 void 更改为实际的数据类型 -
它只是
out看看这个优秀的帖子/解释 - stackoverflow.com/questions/1516876/when-to-use-ref-vs-out 我也知道 Delphi 在我 22 年的编码中的 17 年中对它进行了非常好的编码 -
这是正确的,delphi 你不必初始化参数,但 C# .net 你做的尤其是那些你将用作输出参数的..
-
如果你想在一个方法中返回多个不是 ref 的值,例如如果你调用一个方法并想返回某人的年龄..你为什么要将年龄更改为返回值年龄当您需要做的就是使用简单的输出参数时,使用 ref 关键字 .. 对 C# 输出参数进行 msdn google 搜索,您需要了解 ref.. 和 out.. 之间的明显区别。如果您想使用例如,作为全局变量,您将在方法中传递这些 ref 值。但是 windows 和 web 是不同的.. 所以当你开始更频繁地编码时要小心。
-
您仍然需要初始化输出参数,例如执行以下操作 `string aname = string.Empty;` 或者您可以使用(默认)关键字,但我更喜欢 string.Empty 与 @987654338 @总是初始化整数=0,对象=null,字符串=string.Empty;或一些字符串值。 char =
' 'decimals = 0.0 而不是 0 等等......你得到了漂移
标签: c# delphi parameters delphi-7