Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

C# With block? 1

Status
Not open for further replies.

demohr

Programmer
Apr 19, 2006
5
US
Hello All,

Does anyone know the C# equivalent of the VB "With...End With" block? (If there is one).

Thanks,

demohr
 
Like John says. If you have a long dotted string of methods/properties off a variable, you can create another variable to point to the last of them, which becomes much easier to use.

Chip H.


____________________________________________________________________
If you want to get the best response to a question, please read FAQ222-2244 first
 
It still seems like a great shame for this construct to have been left out of C#. True, it doesn't make for better apps; just simpler code.

Code:
with object1
   .name = "the name";
   .top = 123;
   .left = 100;
   .someotherparam = "aaa";
end with

is trully cleaner and easier to read than ...

Code:
object1.name = "the name";
object1.top = 123;
object1.left = 100;
object1.someotherparam = "aaa";

... don't you think?
 
Not really, but thats just my viewpoint :)

Unless your working with a very long name (for instance, with many periods like the above commentary) I can't see how adding two lines of code and removing the variable name from the line is supposed to make it more readable. Technically you could use another variable to really make it shorter:
Code:
ObjectType o1 = object1;
   o1.name = "the name";
   o1.top = 123;
   o1.left = 100;
   o1.someotherparam = "aaa";

But that seems a little less readable to me and means that the next person to look at your coe is going to try to figure out just why you were doing that.

 
With the advent of intellisense, the need for the with block has diminished. You can now be even more lazy and never completely type the object name.

[red]"... isn't sanity really just a one trick pony anyway?! I mean, all you get is one trick, rational thinking, but when you are good and crazy, oooh, oooh, oooh, the sky is the limit!" - The Tick[/red]
 
The
[tt]WITH
BEGIN
...
...
END[/tt]

in PASCAL was originally used for performance / optimisation purposes, by enabling the compiler to use a base register with indexing rather than have to relocate the data each time it was referenced.

Additionally, with PASCAL and Delphi, but not VB, WITH blocks can be nested.


Hope this helps.



[vampire][bat]
 
I also heard somewhere that with endd with should make the coed faster. If they meant compile faster or run faster I don't know. I could look it up but I 'm to lazy.

Christiaan Baes
Belgium

"My new site" - Me
 
AFAIK, there are two separate situations here, one of which may speed up your code, and the other definitely will.
perhaps someone who can poke about in the bytecode could confirm this.

1. properties

Code:
obj1.Property.Property.Property.Property1 = value1;
obj1.Property.Property.Property.Property2 = value2;

object obj = obj1.Property.Property.Property;
obj.Property1 = value1;
obj.Property2 = value2;

in this case, i seem to remember that the compiler has got a lot smarter and does this for you. it may not, however, so the second way may be faster.

2. methods

Code:
obj1.Property.Method().Property.Property1 = value1;
obj1.Property.Method().Property.Property2 = value2;

object obj = obj1.Property.Method();
obj.Property.Property1 = value1;
obj.Property.Property2 = value2;

since the method will be evaluated each time, the second way will pretty much always be faster.
where this becomes a necessity is with foreach loops.
i've always been told that, since Method() is evaluated on each iteration, the following code is a very bad thing:

Code:
foreach ( object obj in obj1.ArrayMethod() )
{
  // code here
}

and should be replaced by:

Code:
Array obj2 = obj1.ArrayMethod();
foreach ( object obj in obj2 )
{
  // code here
}

i wonder, though, if the compiler hasn't been made smarter yet and turns the first code into the second for you. anyone know?


mr s. <;)

 
As mentioned by earthandfire previously, Delphi makes use of 'With' statements.

However, there is one very big disadvantage when using this in Delphi and that is with debugging. When you hit a breakpoint you cannot simply hovver over the variable using the mouse to show the current values as this does not work when the variables are abbreviated using 'With' statements.

Perhaps there would have been a similar problem with C# and hence not developed?

 
aprobe

I'm pretty sure that if that were the case (i.e. not adding WITH support to C# because of the mouse hover issue), MS would have managed to get around it somehow. They did an incredible job with intellisense, so this would not have been a major problem.

I actually find it surprising that they left it out. C# is by far the friendliest and easiest to use language I've had to deal with to date (after LOGO, of course :) ), due both to the language itself and the editor, and adding the WITH to it would have been an extra cherry. I can't figure out what their rationale was.
 
That's what I hear. A buddy of mine and I are constantly comparing the two products and joking about how incredibly similar they are.

I wonder if higher up the chain both MS and SUN actually belong to the same guy. One day he pulls a brilliant maneouver and decides to market the exact same tool under two different labels just so he could get people up in arms about one or the other, generate publicity and controversy, and in the meantime, generate more revenue?

Good thing I'm not a conspiracy theorist ... [smile]
 
Hello all,

First of all with statements do optimize code in delphi, as it prevents recurring pointer references.
Second of all, there is NO drawback when it comes to debugging . Select the entire object that the with statement is being operated on, press Alt+F5 which will activate the Object inspector for runtime (which should always be used instead of the "hover") and the object inspecter shows the values of each property as the object is manipulated at runtime.
Third of all - the with statement was and still is essential - C# is just using existing logics to substitute for it, giving the developer more work.
 
Third of all - the with statement was and still is essential - C# is just using existing logics to substitute for it, giving the developer more work.
I humbly disagree. A nice to have for GUI controls or classes that you have not built.
If you have that many assignments that you need the With..End With construct I would look at why the client has so much responsibility. Time to rework your CRC cards.
Encapsulate the passive data structure/record making it an object where you can put the method.
Marty
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top