Delphi 到 C# 转换

问题描述 投票:0回答:2

我需要将用 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}
c# delphi
2个回答
5
投票

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 :

假设 QueryLcHistory 作为数据表:

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
   }
© www.soinside.com 2019 - 2024. All rights reserved.