Delphi转C#转换

时间:2016-05-03 11:45:44

标签: c# delphi

我需要将Delphi程序转换为C#,我遇到了问题' WITH' 我无法弄清楚这个的正确转换。任何帮助都会很棒。 我附上了我遇到问题的片段。 QueryLchistory是sql查询,我也删除了while循环中执行的语句。

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}

2 个答案:

答案 0 :(得分:4)

with唯一能做的就是在其范围的命名空间中提升其操作数 这意味着编译器将前缀QueryLcHistory.添加到此前缀有效的每个标识符 这种特殊处理只发生在with语句的begin-end块中,之后就像往常一样。

因为C#没有with语句,所以你必须首先在没有with语句的情况下在Delphi中创建工作代码,而不能将其翻译成C#代码。

要删除with,请按以下步骤操作。

  1. 删除,离开开始
    {with QueryLcHistory do} begin

  2. 使用with语句中的任何内容为每个标识符添加前缀。

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

  3. 编译

  4. 从编译器出错的所有标识符中删除QueryLcHistory

  5. 确保新代码的行为与旧代码的行为相同。

  6. 现在您已经拥有了易于转换为C#的简单代码。

    有邪恶
    您如何知道哪些陈述受with影响哪些陈述不受影响? 除非你已经记住了QueryLcHistory的完整界面(或其中包含的内容),否则你无法知道。
    没有with,范围是明确的并且在您的面前。使用with它是隐含的和阴险的。永远不要使用with,因为很难分辨哪些语句在with语句的范围内以及哪些语句与其他语句相关。

答案 1 :(得分:0)

在C#中,没有类似于with语句的内容,而在VB中则有。

您可以指定对象的属性:

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

但是你不能在obejct之后花一些时间,无论如何你可以创建自己的QueryLcHistory方法,或者你可以在任何适用它的方法之前重复QueryLcHistory:

将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
   }