with statement in C-sharp?

N

Nhan

Hi,
is there equivalent statement in C# as WITH ... END in VB?

instead of:

frm.dataGridView1.AutoGenerateColumns = true;
frm.dataGridView1.DataSource = l_oDataset;
frm.dataGridView1.DataMember = this.DataObjectName ;

I can write in VB:

with frm.dataGridView1
.AutoGenerateColumns = true;
.DataSource = l_oDataset;
.DataMember = this.DataObjectName ;
end with

This code is better...

Thanks
Nhan
 
M

MikeJ

I Come From Win32 oop programming
lots of companies have a lot of legacy code in vb6
and the with statement in my opinion is usless...but vb needs things like
this for performance reasons...

myobj = new someobj()
mystruct
mytype
etc
all have members, properties, fields, etc and so should be denoted by the
object.member

MJ
 
B

Brian Gideon

Hi,
is there equivalent statement in C# as WITH ... END in VB?

instead of:

frm.dataGridView1.AutoGenerateColumns = true;
frm.dataGridView1.DataSource = l_oDataset;
frm.dataGridView1.DataMember = this.DataObjectName ;

I can write in VB:

with frm.dataGridView1
.AutoGenerateColumns = true;
.DataSource = l_oDataset;
.DataMember = this.DataObjectName ;
end with

This code is better...

Thanks
Nhan

Sure...it's elegantly implemented like this.

DataGridView a = frm.dataGridView1;
a.AutoGenerateColumns = true;
a.DataSource = I_oDataset;
a.DataMember = this.DataObjectName;

....which is both more concise and more readable.
 
?

=?ISO-8859-1?Q?Arne_Vajh=F8j?=

MikeJ said:
I Come From Win32 oop programming
lots of companies have a lot of legacy code in vb6
and the with statement in my opinion is usless...but vb needs things like
this for performance reasons...

Actually Pascal and Modula-2 has it as well. And not for
performance reasons.

Arne
 
?

=?ISO-8859-1?Q?Arne_Vajh=F8j?=

Brian said:
Sure...it's elegantly implemented like this.

DataGridView a = frm.dataGridView1;
a.AutoGenerateColumns = true;
a.DataSource = I_oDataset;
a.DataMember = this.DataObjectName;

...which is both more concise and more readable.

I find it somewhat difficult to see the elegance in the
introduction of a new local variable.

Arne
 
B

Brian Gideon

I find it somewhat difficult to see the elegance in the
introduction of a new local variable.

Arne

Elegant, because there is no need for a superfluous keyword. Concise,
because it requires fewer lines of code. I do see your point about
the additonal variable, but I don't think that warrants an additional
keyword.
 
?

=?ISO-8859-1?Q?Arne_Vajh=F8j?=

Brian said:
Elegant, because there is no need for a superfluous keyword. Concise,
because it requires fewer lines of code. I do see your point about
the additonal variable, but I don't think that warrants an additional
keyword.

It don't save a statement. It is 1 extra assignment + the 3 original
statements instead of 1 with + the 3 original statements.

The extra line/lines you are talking about is curly brackets.

And if you were to limit the scope of the new local variable
similar to the with statement, then you would need those too.

Pascal programmer has both options and I think most experienced
Pascal programmers choose to use WITH.

Arne

PS: What will happen if you use your method on a struct instead
of a class ?
 
J

Jon Skeet [C# MVP]

Arne Vajhøj said:
PS: What will happen if you use your method on a struct instead
of a class ?

The same as in VB, I believe - as far as I'm aware, VB just introduces
a local variable in the compiled code anyway.

Note that C# 3 has this in a more limited fashion for object
initializers - I can see the value there, but I'm glad C# doesn't have
With itself.
 
?

=?ISO-8859-1?Q?Arne_Vajh=F8j?=

Jon said:
The same as in VB, I believe - as far as I'm aware, VB just introduces
a local variable in the compiled code anyway.

No. At least not in VB.NET (I don't know about VB6).

Module Program
Public Class Foo
Public Dim v As Integer
End Class
Public Structure Bar
Public Dim v As Integer
End Structure
Sub Main()
Dim f As Foo = New Foo
f.v = 123
Console.WriteLine(f.v)
With f
.v = 456
End With
Console.WriteLine(f.v)
Dim ftmp As Foo = f
ftmp.v = 789
Console.WriteLine(f.v)
Dim b As Bar = New Bar
b.v = 123
Console.WriteLine(b.v)
With b
.v = 456
End With
Console.WriteLine(b.v)
Dim btmp As Bar = b
btmp.v = 789
Console.WriteLine(b.v)
Console.Write("Press any key to continue . . . ")
Console.ReadKey(True)
End Sub
End Module

gives:

123
456
789
123
456
456

Arne
 
J

Jon Skeet [C# MVP]

Arne Vajhøj said:
No. At least not in VB.NET (I don't know about VB6).

Ah, I see what you mean. Yes, introducing an extra local doesn't work
in that case. (One day I'll investigate what it does when you use
"with" on something other than a local variable in that kind of
situation.)

Of course, mutable structs are generally to be avoided anyway...
 

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

Top