2016-05-03 163 views
1

我需要將Delphi程序轉換爲C#,我有'WITH'問題 我找不出適當的轉換。任何幫助都會很棒。 我附上了我有麻煩的片段。 QueryLchistory是sql查詢,我也刪除了while循環內執行的語句。德爾福到C#轉換

with QueryLcHistory do begin 
First; 
RemainingMargin := 0; 

    while (not Eof) and Another test Case 
     do begin 
     //Statements #1 
     Next; 
    end; 

    while (not Eof) and Another test Case2 
     do begin 
    // Statements #2 
     Next; 
    end; 
end; {with} 
+1

隨着不是一個循環。沒有C#等價於。如果你不明白什麼意思閱讀文檔。請在這裏請求之前這樣做。 –

回答

4

with所做的唯一的事情是在其範圍的名稱空間中提升其操作數。
這意味着編譯器將前綴QueryLcHistory.添加到前綴有效的每個標識符。
這個特殊處理只發生在with語句的begin-end塊中,之後它就像往常一樣。

由於C#沒有with語句,因此您必須首先在Delphi中創建工作代碼,而不是先聲明with聲明,然後才能將其轉換爲C#代碼。

要刪除with請按照下列步驟操作。

  1. 與刪除,離開開始
    {with QueryLcHistory do} begin

  2. 前綴的每一個標識無論是在with語句。

    QueryLcHistory.First;
    QueryLcHistory.RemainingMargin := 0; //etc

  3. 編譯

  4. 刪除從那裏編譯器提供了一個錯誤的所有標識符QueryLcHistory

  5. 確保新代碼的行爲與舊代碼的行爲相同。

現在你已經有了應該很容易轉換成C#的簡單代碼。

隨着邪惡
你怎麼知道哪些語句由with影響的,哪些不是?
那麼除非你記住了QueryLcHistory的完整界面(或者與之相關的任何東西),你不知道。
沒有with範圍是明確的,在你的臉上。與with它是隱含的和陰險的。切勿使用with,因爲很難判斷哪些語句在with語句的範圍內,哪些語句與其他語句相關。

0

在C#中沒有類似於with語句的東西,而在VB中有。

您可以分配這樣一個對象的屬性:

StringBuilder sb = new StringBuilder() 
    .Append("foo") 
    .Append("bar") 
    .Append("zap"); 

,但你不能把一個,而對象後,反正你可以創建自己的QueryLcHistory的方法,或者你可以簡單地之前的任何重複QueryLcHistory適用於它的方法:

Assumning QueryLcHistory作爲一個DataTable:

int RemainingMargin = 0; 
DataRow row; 

    IEnumerator e = QueryLcHistory.rows.GetEnumerator(); 

    while (e.MoveNext() && Another test Case) { 
     row = e.Current 
     // Statements #1 
    } 

    while (e.MoveNext() && Another test Case 2) { 
     row = e.Current 
     // Statements #2 
    }