繁体   English   中英

无法将delphi过程转换为C#

[英]Trouble converting delphi procedure to C#

我来这已经有一点运气了。

我有这个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#的正确转换吗?

如果要在方法中返回多个值,而不是通过ref来返回值,例如,如果您调用一个方法并想返回某人的年龄,那么为什么在所有情况下都使用ref关键字将年龄更改为返回值age您需要做的是使用简单的out参数。.在ms#google搜索C#out参数上,您需要了解ref。和out ..之间的明显区别。例如,如果您想用作全局变量,则在您的方法中传递这些ref值。 但是Windows和Web是不同的..所以要当心,当您开始更频繁地在两者中进行编码时

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM