With or not with keyword

  • Thread starter Thread starter John S
  • Start date Start date
J

John S

In Visual Basic, there was a keyword called "With". If you referenced
an object with the keyword With you could access all properties and
events just using the "." and not having to use the object name.
Example
With TextBox1
..text = "This"
..enabled=true
..maxLength = 11
end with

Is there anyting like this in C#
 
No, there is no such construct in C# and I believe in VB.NET neither. 'With'
is considered to make the code harder to read
 
Hi,

No , only aliases for namespaces like

using MyAlias = System.Text;

MyAlias.StringBuilder;

that's all,
cheers
Salva
 
It's not in C# but in VB.NET it is. And it doesn't make the code harder to
read.
It even makes it easier to write little faster code.

TextBox1.Text = "This";
TextBox1.Enabled = true;
.....

would each time call the get accessor of the TextBox1-Property (if its not a
field) wich can have a little performance impact.

The with construct makes something like:
TextBox t = TextBox1;
t.Text = "This";
t.Enabled = true;
......
t = null;

imho the with statement is easier to read and to type and in most cases it's
(a bit) faster.
this is one of the few VB-features i would like to have in c#

Christof
 
Lebesgue said:
No, there is no such construct in C# and I believe in VB.NET neither.
'With'
is considered to make the code harder to read

Works fine in VB.Net :)

Mythran
 
John S said:
In Visual Basic, there was a keyword called "With". If you referenced
an object with the keyword With you could access all properties and
events just using the "." and not having to use the object name.
Example
With TextBox1
.text = "This"
.enabled=true
.maxLength = 11
end with

Is there anyting like this in C#

No. You can always use a single-character variable name if you want. It
has the same effect, other than being *slightly* easier to read.
(Alternatively, use a meaningful name and keep readability high.)
 
Christof Nordiek said:
It's not in C# but in VB.NET it is. And it doesn't make the code harder to
read.
It even makes it easier to write little faster code.

TextBox1.Text = "This";
TextBox1.Enabled = true;
....

would each time call the get accessor of the TextBox1-Property (if its not
a field) wich can have a little performance impact.

The with construct makes something like:
TextBox t = TextBox1;
t.Text = "This";
t.Enabled = true;
.....
t = null;

imho the with statement is easier to read and to type and in most cases
it's (a bit) faster.
this is one of the few VB-features i would like to have in c#

Christof


This was true in VB6 (the little faster code) but it isn't any longer in
managed code.
IMO it was the correct decision not to include it. It might be easier to
read for those used to VB, but these were not the developers MS was
targeting at when they designed C#. Those who aren't used to the construct
might find it less readable.

Willy.
 
Jon said:
No. You can always use a single-character variable name if you want. It
has the same effect, other than being *slightly* easier to read.
(Alternatively, use a meaningful name and keep readability high.)

Or name what you are doing "with" a particular instance ie use a method
 
Back
Top