Q: is there a C# equ. of the Delphis With x do?

  • Thread starter Thread starter Visual Systems AB \(Martin Arvidsson\)
  • Start date Start date
V

Visual Systems AB \(Martin Arvidsson\)

Hi!

I often come across situations where i have a major code string for
accessing objects, variables and so on

In delphi, you could enter:

With mainObj.ThisControl.ThisProperty do
begin
x := dsdsa;
end;

instead of mainObj.ThisControl.ThisProperty.x := dsdsa;

It it possbile to do this in c#?

Regards

Martin Arvidsson
 
The C# way is to use the following:

ThisPropertyType temp = mainObj.ThisControl.ThisProperty;

temp.x = whatever;
temp.y = something;
..
..
..

The with statement has been discussed many times before and has always
been declared as evil, because it can lead to hard to debug problems
with overlapping names

HTH,
Stefan
 
Note that the alternative suggested by Stefan has the additional benefit of
allowing you do use more than one shortcut at the same time. The 'With'
block (in VB or Delphi) allows only one shortcut at a time via 'With'. In
addition, one of the major drawbacks of 'With' is that intellisense does not
help you identify the object within the block - you have to examine the
header (ok if you have a short block, but otherwise not so good).

David Anton
www.tangiblesoftwaresolutions.com
Home of the Instant C# VB.NET to C# converter and the Instant VB C# to
VB.NET converter
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Back
Top