473,387 Members | 1,283 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,387 software developers and data experts.

with statement in C-sharp?

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

Aug 3 '07 #1
11 1879
"Nhan" <le*****@freenet.dewrote in message
news:%2****************@TK2MSFTNGP02.phx.gbl...

is there equivalent statement in C# as WITH ... END in VB?
No, thank God!
http://www.google.co.uk/search?hl=en...ement%22&meta=
--
Mark Rae
ASP.NET MVP
http://www.markrae.net

Aug 3 '07 #2
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

"Nhan" <le*****@freenet.dewrote in message
news:%2****************@TK2MSFTNGP02.phx.gbl...
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

Aug 3 '07 #3
On Aug 3, 10:43 am, "Nhan" <le.n...@freenet.dewrote:
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.

Aug 3 '07 #4
Nhan wrote:
is there equivalent statement in C# as WITH ... END in VB?
There are not.

http://msdn2.microsoft.com/en-us/vcsharp/Aa336816.aspx

gives MS's explanation of why.

Arne
Aug 3 '07 #5
MikeJ wrote:
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
Aug 3 '07 #6
Brian Gideon wrote:
On Aug 3, 10:43 am, "Nhan" <le.n...@freenet.dewrote:
>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

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
Aug 3 '07 #7
On Aug 3, 5:03 pm, Arne Vajhøj <a...@vajhoej.dkwrote:
Brian Gideon wrote:
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
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.

Aug 4 '07 #8
Brian Gideon wrote:
On Aug 3, 5:03 pm, Arne Vajhøj <a...@vajhoej.dkwrote:
>Brian Gideon wrote:
>>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.

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 ?
Aug 4 '07 #9
Arne Vajhøj <ar**@vajhoej.dkwrote:
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.

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet Blog: http://www.msmvps.com/jon.skeet
If replying to the group, please do not mail me too
Aug 5 '07 #10
Jon Skeet [C# MVP] wrote:
Arne Vajhøj <ar**@vajhoej.dkwrote:
>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.
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
Aug 5 '07 #11
Arne Vajhøj <ar**@vajhoej.dkwrote:
Jon Skeet [C# MVP] wrote:
Arne Vajhøj <ar**@vajhoej.dkwrote:
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.
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...

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet Blog: http://www.msmvps.com/jon.skeet
If replying to the group, please do not mail me too
Aug 5 '07 #12

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

68
by: Marco Bubke | last post by:
Hi I have read some mail on the dev mailing list about PEP 318 and find the new Syntax really ugly. def foo(x, y): pass I call this foo(1, 2), this isn't really intuitive to me! Also I...
3
by: Jason Callas | last post by:
I have a stored procedure that runs as a step in a scheduled job. For some reason the job does not seem to finish when ran from the job but does fine when run from a window in SQL Query. I know...
22
by: nobody | last post by:
hello everybody, is there a way of creating an array with help of a function that would accept the name of this array as a parameter and then create global Array type variable of that name? so...
61
by: Toby Austin | last post by:
I'm trying to replace <table>s with <div>s as much as possible. However, I can't figure out how to do the following… <table> <tr> <td valign="top" width="100%">some data that will...
27
by: C Gillespie | last post by:
Dear All, Hopefully I have a simple problem. Basically, I just want to alter some text with JS. Here is some of my test code: <snip> <script type="text/javascript"> var tmp='a';
13
by: eman1000 | last post by:
I was recently looking at the prototype library (http://prototype.conio.net/) and I noticed the author used the following syntax: Object.extend(MyObj.prototype, { my_meth1: function(){},...
0
by: NM | last post by:
Hello, I've got a problem inserting binary objects into the postgres database. I have binary objects (e.g. images or smth else) of any size which I want to insert into the database. Funny is it...
8
by: Rasmus Kromann-Larsen | last post by:
The With Conundrum I'm currently writing a master thesis on (preparations for) static analysis of JavaScript, and after investigating the with statement, it only even more evident to me that the...
5
oll3i
by: oll3i | last post by:
my librarybean package library.ejb; import java.sql.*; import javax.ejb.*; import library.common.*; @Stateless @Remote
2
jwwicks
by: jwwicks | last post by:
C/C++ Programs and Debugging in Linux This tutorial will give you a basic idea how to debug a program in Linux using GDB. As you are aware Visual Studio doesn’t run on Linux so you have to use...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.