"With" equivalent in C#

  • Thread starter Thread starter Paul Brownjohn
  • Start date Start date
P

Paul Brownjohn

Hello

I am new both to this group and to C# programming having spent the last 15
years or so writing C and VB.

You will forgive I hope if I am asking the totally obvious but it is a
question that I cannot find an answer to in the doc:

Is there an equivalent in C# to the "With Me" syntax in VB; specifically
some means of getting a down-down that enumerates the objects available on a
form instead of having to remember them or having to go look what's there?

Thanks

Paul BJ
 
Unfortunately, no I don't believe there is. In the old days of VB, with
could provide significant performance enhancements every time you cut out a
'.' which other than readability, was it's primary allure. However, from
what I've been told, that's no longer the case. Anyway, I agree it's a nice
construct, but I don't believe there's a C# equivalent.
 
No, there is no C# counterpart. And, according to C# dev blogs, it's not in
the plan for the foreseeable future either.
The equivalent code to "With
OriginalObjectRef.Property1.Property2.Indexer[5]" is to create a temp
reference:

MyClassType ref = OriginalObjectRef.Property1.Property2.Indexer[5];
ref.Prop1 = 100;
ref.Prop2 = "some text";
ref.Method1();

That keeps you from having to type the entire chain of members every time,
and is basically what VB comiles in the background.

If all you are worried about is the quick drop-down intellisense, just type
"this."
Yes, it's not as convenient as using With, but it's what you have.

-Rob Teixeira [MVP]
 
Or, if you want to save keystrokes and are in to cheap hacks that will
probably be unreadable to others, just declare a refernece and name it _ or
something.

{ // with
MyClassType _ = OriginalObjectRef.Property1.Property2.Indexer[5];
_.Prop1 = 100;
_.Prop2 = "some text";
_.Method1();
} // end with


Not that this is any less readable than the With construct, in my opinion.

*sheepish grin*

--Matthew W. Jackson

Rob Teixeira said:
No, there is no C# counterpart. And, according to C# dev blogs, it's not in
the plan for the foreseeable future either.
The equivalent code to "With
OriginalObjectRef.Property1.Property2.Indexer[5]" is to create a temp
reference:

MyClassType ref = OriginalObjectRef.Property1.Property2.Indexer[5];
ref.Prop1 = 100;
ref.Prop2 = "some text";
ref.Method1();

That keeps you from having to type the entire chain of members every time,
and is basically what VB comiles in the background.

If all you are worried about is the quick drop-down intellisense, just type
"this."
Yes, it's not as convenient as using With, but it's what you have.

-Rob Teixeira [MVP]

William Ryan said:
Unfortunately, no I don't believe there is. In the old days of VB, with
could provide significant performance enhancements every time you cut
out
a
'.' which other than readability, was it's primary allure. However, from
what I've been told, that's no longer the case. Anyway, I agree it's a nice
construct, but I don't believe there's a C# equivalent.
last
available
 
Paul Brownjohn said:
I am new both to this group and to C# programming having spent the last 15
years or so writing C and VB.

You will forgive I hope if I am asking the totally obvious but it is a
question that I cannot find an answer to in the doc:

Is there an equivalent in C# to the "With Me" syntax in VB; specifically
some means of getting a down-down that enumerates the objects available on a
form instead of having to remember them or having to go look what's there?

Others have given responses to the rest of the "with" debate, but to
address your last point - if you just want to get a drop-down, type
"this." and the members should all show up.
 
You will have alises for namespaces in 2.0 and if you dont want to type alot
you can take a local reference to shorten the list.


Rob Teixeira said:
No, there is no C# counterpart. And, according to C# dev blogs, it's not in
the plan for the foreseeable future either.
The equivalent code to "With
OriginalObjectRef.Property1.Property2.Indexer[5]" is to create a temp
reference:

MyClassType ref = OriginalObjectRef.Property1.Property2.Indexer[5];
ref.Prop1 = 100;
ref.Prop2 = "some text";
ref.Method1();

That keeps you from having to type the entire chain of members every time,
and is basically what VB comiles in the background.

If all you are worried about is the quick drop-down intellisense, just type
"this."
Yes, it's not as convenient as using With, but it's what you have.

-Rob Teixeira [MVP]

William Ryan said:
Unfortunately, no I don't believe there is. In the old days of VB, with
could provide significant performance enhancements every time you cut
out
a
'.' which other than readability, was it's primary allure. However, from
what I've been told, that's no longer the case. Anyway, I agree it's a nice
construct, but I don't believe there's a C# equivalent.
last
available
 
That may help some in not "typing a lot", but it's really a completely
different concept than the VB "With" construct. One shortens acess to
types, the other shortens access to objects' members.

You will have alises for namespaces in 2.0 and if you dont want to type alot
you can take a local reference to shorten the list.


Rob Teixeira said:
No, there is no C# counterpart. And, according to C# dev blogs, it's not in
the plan for the foreseeable future either.
The equivalent code to "With
OriginalObjectRef.Property1.Property2.Indexer[5]" is to create a temp
reference:

MyClassType ref = OriginalObjectRef.Property1.Property2.Indexer[5];
ref.Prop1 = 100;
ref.Prop2 = "some text";
ref.Method1();

That keeps you from having to type the entire chain of members every time,
and is basically what VB comiles in the background.

If all you are worried about is the quick drop-down intellisense, just type
"this."
Yes, it's not as convenient as using With, but it's what you have.

-Rob Teixeira [MVP]

William Ryan said:
Unfortunately, no I don't believe there is. In the old days of VB, with
could provide significant performance enhancements every time you cut
out
a
'.' which other than readability, was it's primary allure. However, from
what I've been told, that's no longer the case. Anyway, I agree it's
a
nice
construct, but I don't believe there's a C# equivalent.
Hello

I am new both to this group and to C# programming having spent the
last
15
years or so writing C and VB.

You will forgive I hope if I am asking the totally obvious but it is a
question that I cannot find an answer to in the doc:

Is there an equivalent in C# to the "With Me" syntax in VB; specifically
some means of getting a down-down that enumerates the objects
available
on
a
form instead of having to remember them or having to go look what's there?

Thanks

Paul BJ
 
Use a 1 letter reference, how hard is that?

DooferFangle d = blah.blah.blah;

Yeah very hard.


Philip Rieck said:
That may help some in not "typing a lot", but it's really a completely
different concept than the VB "With" construct. One shortens acess to
types, the other shortens access to objects' members.

You will have alises for namespaces in 2.0 and if you dont want to type alot
you can take a local reference to shorten the list.


Rob Teixeira said:
No, there is no C# counterpart. And, according to C# dev blogs, it's
not
in
the plan for the foreseeable future either.
The equivalent code to "With
OriginalObjectRef.Property1.Property2.Indexer[5]" is to create a temp
reference:

MyClassType ref = OriginalObjectRef.Property1.Property2.Indexer[5];
ref.Prop1 = 100;
ref.Prop2 = "some text";
ref.Method1();

That keeps you from having to type the entire chain of members every time,
and is basically what VB comiles in the background.

If all you are worried about is the quick drop-down intellisense, just type
"this."
Yes, it's not as convenient as using With, but it's what you have.

-Rob Teixeira [MVP]

Unfortunately, no I don't believe there is. In the old days of VB, with
could provide significant performance enhancements every time you
cut
out
a
'.' which other than readability, was it's primary allure. However, from
what I've been told, that's no longer the case. Anyway, I agree
it's
is
 
It's not difficult. I stated that namespace aliase and the With keyword are
orthagonal concepts, not that typing was difficult.

Use a 1 letter reference, how hard is that?

DooferFangle d = blah.blah.blah;

Yeah very hard.


Philip Rieck said:
That may help some in not "typing a lot", but it's really a completely
different concept than the VB "With" construct. One shortens acess to
types, the other shortens access to objects' members.

You will have alises for namespaces in 2.0 and if you dont want to
type
alot
you can take a local reference to shorten the list.


No, there is no C# counterpart. And, according to C# dev blogs, it's not
in
the plan for the foreseeable future either.
The equivalent code to "With
OriginalObjectRef.Property1.Property2.Indexer[5]" is to create a temp
reference:

MyClassType ref = OriginalObjectRef.Property1.Property2.Indexer[5];
ref.Prop1 = 100;
ref.Prop2 = "some text";
ref.Method1();

That keeps you from having to type the entire chain of members every time,
and is basically what VB comiles in the background.

If all you are worried about is the quick drop-down intellisense, just
type
"this."
Yes, it's not as convenient as using With, but it's what you have.

-Rob Teixeira [MVP]

Unfortunately, no I don't believe there is. In the old days of
VB,
with
could provide significant performance enhancements every time you cut
out
a
'.' which other than readability, was it's primary allure. However,
from
what I've been told, that's no longer the case. Anyway, I agree
it's
a
nice
construct, but I don't believe there's a C# equivalent.
Hello

I am new both to this group and to C# programming having spent the
last
15
years or so writing C and VB.

You will forgive I hope if I am asking the totally obvious but
it
 
Back
Top