vb to c# question

  • Thread starter Thread starter djc
  • Start date Start date
D

djc

in vb there is a With construct. It supposedly speeds things up. By caching,
I assume, it eliminates having to travel several levels in a hierarchy using
the dot operator. Is there a C# equivalent of this? A quick look at VS help
only brought up VB stuff for it.

With ObjectVar
.this
.that
.whatever()
End With
 
"djc" <[email protected]> a écrit dans le message de (e-mail address removed)...

| in vb there is a With construct. It supposedly speeds things up. By
caching,
| I assume, it eliminates having to travel several levels in a hierarchy
using
| the dot operator. Is there a C# equivalent of this? A quick look at VS
help
| only brought up VB stuff for it.
|
| With ObjectVar
| .this
| .that
| .whatever()
| End With

The only point behind the with construct is to save typing code.

If you have to do several things with a sub-property, then you can always
assign to a local variable and use that instead.

{
string shortcut = This.That.Other.Name;

shortcut.this;
shortcut.that;
shortcut.whatever();
}

Joanna
 
djc,

While there is no "with" statement, you can easily take what you would
put in the "with" statement and then assign it to a variable, then use the
variable, like so:

// Assume ObjectVar is of type MyClass.
MyClass ov = ObjectVar;

// Do the work.
ov.this;
ov.that;
ov.whatever();

In reality, that's all the with statement ever did.

Hope this helps.
 
"With" has no advantage that you can't get with an obscurely-shortened
temporary variable name (and the same disadvantage).
Also, the performance benefit only applies to VB6 (due to a
characteristic/flaw of the VB6 compiler).
--
David Anton
www.tangiblesoftwaresolutions.com
Instant C#: VB to C# converter
Instant VB: C# to VB converter
Instant C++: C#/VB to C++ converter
C# Code Metrics: Quick metrics for C#
 
"With" actually creates a unnamed, temporary variable on the stack. As such
it will also improve VB 2005 code performance. Careful use of With can
actually make your code easier to read and maintain.

Mike Ober.
 
Michael D. Ober said:
"With" actually creates a unnamed, temporary variable on the stack. As such
it will also improve VB 2005 code performance.

Surely it won't improve performance over having a named variable on the
stack, will it? By the time the code is running, they'd be equivalent.
Careful use of With can
actually make your code easier to read and maintain.

Easier to read and maintain than an explicitly named variable? I
suspect we'll continue to disagree.
 
Back
Top