Reputation: 146
I need to convert a program written in the Delphi Programming Language to C# and I have a problem with with
.
I cannot figure out the proper conversion for this. Any help would be great.
I have attached the snippet where I am having trouble with.
QueryLchistory is the sql query and also I have removed the statements executed within the while loops.
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}
Upvotes: 3
Views: 1616
Reputation: 76537
The only thing that with
does is promote its operand in the namespace of its scope.
That means the compiler prefixes QueryLcHistory.
to every identifier where this prefixing is valid.
This special handling takes place only within the begin-end block of the with statement, after that it is business as usual.
Because C# does not have a with
statement you'll have to create working code in Delphi without the with
statement first, which can than be translated into C# code.
In order to remove with
follow these steps.
remove with, leave the begin
{with QueryLcHistory do} begin
prefix every single identifier with whatever was in the with statement.
QueryLcHistory.First;
QueryLcHistory.RemainingMargin := 0; //etc
Compile
Remove QueryLcHistory
from all identifiers where the compiler gives an error.
Ensure that the new code behaves in the same way the old code does.
Now you've got straightforward code that should be easy to translate into C#.
With is evil
How do you know which statements are affected by the with
and which are not?
Well unless you've memorized the full interface of QueryLcHistory (or whatever is in the with) you can't know.
Without the with
the scope is explicit and in your face. With with
it is implicit and insidious. Never use with
because it is hard to tell which statements are in scope of the with statement and which statements relate to something else.
Upvotes: 6
Reputation: 1002
In C# there isn't something similar to a with statement, while in VB there is .
You can assign properties of an object like that:
StringBuilder sb = new StringBuilder()
.Append("foo")
.Append("bar")
.Append("zap");
but you can't put a while after the obejct, anyway you could create your own method of QueryLcHistory or you could simply repeat QueryLcHistory before any method that applies to it:
Assumning QueryLcHistory as a 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
}
Upvotes: 0