简体   繁体   中英

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. 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:

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? 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. 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#?

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. but windows and web are different .. so beware when you start coding more frequently in both

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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