简体   繁体   English

无法将delphi过程转换为C#

[英]Trouble converting delphi procedure to C#

I've been at this for a bit with no luck. 我来这已经有一点运气了。

I have this delphi procedure which I did not write and don't have the original program to test. 我有这个Delphi程序,我没有编写,也没有要测试的原始程序。 Please take note of the comment to see what it's supposed to do: 请记下评论以查看其应执行的操作:

// 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;

Here's my C# version: 这是我的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;
                }
            }    
        }

I've tested it by giving it a string like: 我通过给它一个字符串来测试它:

string test = "Okay:C15"
TestingThis(test)

I'm confused though. 我很困惑。 In the Delphi code, only one parameter is an input: astring , and the rest are supposedly returned values? 在Delphi代码中,只有一个参数是输入: astring ,其余的应该是返回值? How is this possible? 这怎么可能? I haven't seen anything about one parameter going in and 4 going out From what I read, the var keyword means they are passed by reference, which means I should use ref in the c# version. 我没有看到关于传入一个参数和传出4个参数的任何信息。从我读到的内容来看, var关键字表示它们是通过引用传递的,这意味着我应该在c#版本中使用ref The function itself is supposedly only called once, and the input is in fact a single string. 该函数本身仅被调用一次,而输入实际上是单个字符串。

Edit: Changed my function to this: 编辑:更改我的功能到此:

public static void TestingThis(string astring, out string aname, out char atype, out byte alength, out byte adecimals)

And I call it like this: 我这样称呼它:

    string test = "Okay:C15";
    string aname;
    char atype;
    byte alength;
    byte adecimals;
    TestingThis(test, out aname, out atype, out alength, out adecimals);

Is this correct conversion from Delphi to C#? 这是从Delphi到C#的正确转换吗?

If you are wanting to return multiple values in a method that are not by ref for example if you call a method and want to return someone's age.. why would you change the age to the return values age by using the ref key word when all you need to do is use a simple out param.. do a msdn google search on C# out parameters you need to understand the clear difference between ref.. and out.. also if you want to use as a global variable for example you would pass those ref values in your method. 如果要在方法中返回多个值,而不是通过ref来返回值,例如,如果您调用一个方法并想返回某人的年龄,那么为什么在所有情况下都使用ref关键字将年龄更改为返回值age您需要做的是使用简单的out参数。.在ms#google搜索C#out参数上,您需要了解ref。和out ..之间的明显区别。例如,如果您想用作全局变量,则在您的方法中传递这些ref值。 but windows and web are different .. so beware when you start coding more frequently in both 但是Windows和Web是不同的..所以要当心,当您开始更频繁地在两者中进行编码时

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

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