簡體   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