473,321 Members | 1,669 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,321 software developers and data experts.

With - End with block in C#

Hi,

I moved from VB6 to C# recently, and am very satisfied with the
result. However, I miss this one feature. The closest thing I could
find is "using" statement, but it requires object to implement
IDisposable interface, and it won't save me the trouble of typing the
variable name. What I want to do is something similar to this vb6
block of code:

Dim a as class1
set a = new class1
with a
.dothis
.dothat
.propertyx = 1
end with

Does the similar feature exist in C#, or no ?

Best regards,

Zeljko
Nov 15 '05 #1
32 11697
No, it does not exist.

Miha

"Zeljko" <tr*******@yahoo.com> wrote in message
news:d5********************************@4ax.com...
Hi,

I moved from VB6 to C# recently, and am very satisfied with the
result. However, I miss this one feature. The closest thing I could
find is "using" statement, but it requires object to implement
IDisposable interface, and it won't save me the trouble of typing the
variable name. What I want to do is something similar to this vb6
block of code:

Dim a as class1
set a = new class1
with a
.dothis
.dothat
.propertyx = 1
end with

Does the similar feature exist in C#, or no ?

Best regards,

Zeljko

Nov 15 '05 #2
Zeljko <tr*******@yahoo.com> wrote:
I moved from VB6 to C# recently, and am very satisfied with the
result. However, I miss this one feature. The closest thing I could
find is "using" statement, but it requires object to implement
IDisposable interface, and it won't save me the trouble of typing the
variable name.
In fact, "using" and "with" share basically no characteristics :)
What I want to do is something similar to this vb6
block of code:

Dim a as class1
set a = new class1
with a
.dothis
.dothat
.propertyx = 1
end with

Does the similar feature exist in C#, or no ?


No, it doesn't. (Thanks goodness, in my view.)

See http://www.gotdotnet.com/team/csharp.../ask.aspx#with
for the reasons it's not in C#.

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet
If replying to the group, please do not mail me too
Nov 15 '05 #3
On Tue, 28 Oct 2003 16:21:49 +0100, Zeljko <tr*******@yahoo.com> wrote:
Hi,

I moved from VB6 to C# recently, and am very satisfied with the
result. However, I miss this one feature. The closest thing I could
find is "using" statement, but it requires object to implement
IDisposable interface, and it won't save me the trouble of typing the
variable name. What I want to do is something similar to this vb6
block of code:

Dim a as class1
set a = new class1
with a
.dothis
.dothat
.propertyx = 1
end with

Does the similar feature exist in C#, or no ?


In the course of learning how to think more OOP, Ive figured out that With
... End With is a good idea only in a language like the old VB with poor OOP
capabilities.
When you find yourself doing a series of operations on the same object, you
usually have a case for making this a method of that object's class or a
new inheriting class. Once the code is in the class that is being acted
on, the object reference becomes implicit, just as it would be in a With
block.
Nov 15 '05 #4
Right, they don't share anything, but the fact that a block of code is
somehow related to a particular object. I miss it because I'm lazy -
typing . instead of typing first few letters and pressing ctrl+space
is a much faster thing to do. That's a great feature, if u ask me :)

Thanks for the article,

Zeljko

On Tue, 28 Oct 2003 15:30:32 -0000, Jon Skeet [C# MVP]
<sk***@pobox.com> wrote:
Zeljko <tr*******@yahoo.com> wrote:
I moved from VB6 to C# recently, and am very satisfied with the
result. However, I miss this one feature. The closest thing I could
find is "using" statement, but it requires object to implement
IDisposable interface, and it won't save me the trouble of typing the
variable name.


In fact, "using" and "with" share basically no characteristics :)
What I want to do is something similar to this vb6
block of code:

Dim a as class1
set a = new class1
with a
.dothis
.dothat
.propertyx = 1
end with

Does the similar feature exist in C#, or no ?


No, it doesn't. (Thanks goodness, in my view.)

See http://www.gotdotnet.com/team/csharp.../ask.aspx#with
for the reasons it's not in C#.


Nov 15 '05 #5
Zeljko <tr*******@yahoo.com> wrote:
Right, they don't share anything, but the fact that a block of code is
somehow related to a particular object.
Or objects - you can have multiple objects in a using construct.
I miss it because I'm lazy -
typing . instead of typing first few letters and pressing ctrl+space
is a much faster thing to do. That's a great feature, if u ask me :)


Only if you don't value readability. I believe that "with" reduces
readability - and over the course of some code's lifetime, far more of
the time is likely to be spent reading it than typing it.

As Steve said - if you're doing a whole load of operations with a
single object, chances are that functionality should be encapsulated
within the object itself.

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet
If replying to the group, please do not mail me too
Nov 15 '05 #6
Good point in some cases. Still, what to do with built-in, sealed
classes ? E.g. :

OleDbCommand a = new OleDbCommand ("DoThis" , conn);
a.Parameters.Add ..(
a.Parameters.Add ...

instead of

with a.parameters
.add ...
.add ...
end with

Zeljko
On Tue, 28 Oct 2003 15:39:27 GMT, Steve Jorgensen
<no****@nospam.nospam> wrote:
On Tue, 28 Oct 2003 16:21:49 +0100, Zeljko <tr*******@yahoo.com> wrote:
Hi,

I moved from VB6 to C# recently, and am very satisfied with the
result. However, I miss this one feature. The closest thing I could
find is "using" statement, but it requires object to implement
IDisposable interface, and it won't save me the trouble of typing the
variable name. What I want to do is something similar to this vb6
block of code:

Dim a as class1
set a = new class1
with a
.dothis
.dothat
.propertyx = 1
end with

Does the similar feature exist in C#, or no ?


In the course of learning how to think more OOP, Ive figured out that With
.. End With is a good idea only in a language like the old VB with poor OOP
capabilities.
When you find yourself doing a series of operations on the same object, you
usually have a case for making this a method of that object's class or a
new inheriting class. Once the code is in the class that is being acted
on, the object reference becomes implicit, just as it would be in a With
block.


Nov 15 '05 #7
On Tue, 28 Oct 2003 15:49:31 -0000, Jon Skeet [C# MVP]
<sk***@pobox.com> wrote:
Zeljko <tr*******@yahoo.com> wrote:
Right, they don't share anything, but the fact that a block of code is
somehow related to a particular object.
Or objects - you can have multiple objects in a using construct.


Didn't know that.
I miss it because I'm lazy -
typing . instead of typing first few letters and pressing ctrl+space
is a much faster thing to do. That's a great feature, if u ask me :)


Only if you don't value readability. I believe that "with" reduces
readability - and over the course of some code's lifetime, far more of
the time is likely to be spent reading it than typing it.


I simply don't agree with the readability point. I think the code is
equaly, if not more readable using with blocks. Might be the thing I'm
used to, though.

Zeljko
Nov 15 '05 #8
IMO, Microsoft did that wrong. Instead of the new parameter, .Add should
return the Parameters collection, so you can simply chain .Add calls
together. Parameter constructors should be in charge of the different ways
parameters can be created, not the Parameters.Add method.

On Tue, 28 Oct 2003 16:51:52 +0100, Zeljko <tr*******@yahoo.com> wrote:
Good point in some cases. Still, what to do with built-in, sealed
classes ? E.g. :

OleDbCommand a = new OleDbCommand ("DoThis" , conn);
a.Parameters.Add ..(
a.Parameters.Add ...

instead of

with a.parameters
.add ...
.add ...
end with

Zeljko
On Tue, 28 Oct 2003 15:39:27 GMT, Steve Jorgensen
<no****@nospam.nospam> wrote:
On Tue, 28 Oct 2003 16:21:49 +0100, Zeljko <tr*******@yahoo.com> wrote:
Hi,

I moved from VB6 to C# recently, and am very satisfied with the
result. However, I miss this one feature. The closest thing I could
find is "using" statement, but it requires object to implement
IDisposable interface, and it won't save me the trouble of typing the
variable name. What I want to do is something similar to this vb6
block of code:

Dim a as class1
set a = new class1
with a
.dothis
.dothat
.propertyx = 1
end with

Does the similar feature exist in C#, or no ?


In the course of learning how to think more OOP, Ive figured out that With
.. End With is a good idea only in a language like the old VB with poor OOP
capabilities.
When you find yourself doing a series of operations on the same object, you
usually have a case for making this a method of that object's class or a
new inheriting class. Once the code is in the class that is being acted
on, the object reference becomes implicit, just as it would be in a With
block.


Nov 15 '05 #9
No! It would be an interesting addition, but it is not there. Overall, if
you start working with constructors, you will find that you do not need with
blocks anyway. ;->

--
Gregory A. Beamer
MVP; MCP: +I, SE, SD, DBA

************************************************** ********************
Think Outside the Box!
************************************************** ********************
"Zeljko" <tr*******@yahoo.com> wrote in message
news:d5********************************@4ax.com...
Hi,

I moved from VB6 to C# recently, and am very satisfied with the
result. However, I miss this one feature. The closest thing I could
find is "using" statement, but it requires object to implement
IDisposable interface, and it won't save me the trouble of typing the
variable name. What I want to do is something similar to this vb6
block of code:

Dim a as class1
set a = new class1
with a
.dothis
.dothat
.propertyx = 1
end with

Does the similar feature exist in C#, or no ?

Best regards,

Zeljko

Nov 15 '05 #10
It gets easier in the next version of .NET, but C# will not ever be the same
as VB.NET. You have to make some language decisions.

--
Gregory A. Beamer
MVP; MCP: +I, SE, SD, DBA

************************************************** ********************
Think Outside the Box!
************************************************** ********************
"Zeljko" <tr*******@yahoo.com> wrote in message
news:ai********************************@4ax.com...
Right, they don't share anything, but the fact that a block of code is
somehow related to a particular object. I miss it because I'm lazy -
typing . instead of typing first few letters and pressing ctrl+space
is a much faster thing to do. That's a great feature, if u ask me :)

Thanks for the article,

Zeljko

On Tue, 28 Oct 2003 15:30:32 -0000, Jon Skeet [C# MVP]
<sk***@pobox.com> wrote:
Zeljko <tr*******@yahoo.com> wrote:
I moved from VB6 to C# recently, and am very satisfied with the
result. However, I miss this one feature. The closest thing I could
find is "using" statement, but it requires object to implement
IDisposable interface, and it won't save me the trouble of typing the
variable name.


In fact, "using" and "with" share basically no characteristics :)
What I want to do is something similar to this vb6
block of code:

Dim a as class1
set a = new class1
with a
.dothis
.dothat
.propertyx = 1
end with

Does the similar feature exist in C#, or no ?


No, it doesn't. (Thanks goodness, in my view.)

See http://www.gotdotnet.com/team/csharp.../ask.aspx#with
for the reasons it's not in C#.

Nov 15 '05 #11
This is a good point, but you have to ask yourself a couple of questions:

1. Which methodology is more explicit? Overall, explicit code is less buggy
and easier to read.
2. Which methodology is easier to maintain? I find most people that heavily
use "with" in Visual Basic are pretty much being lazy.

With the OleDb command params, I generally use one of the following
methodologies:

1. Create a code generator based on the stored procedure. A poor man's
version can be created with SQL Query Analyzer and Excel (or Access builder,
et al)

2. Create a generic layer that matches sproc params with a list of some type
(dictionary, hashtable, et al) so I do not have to create the params
programatically.

3. Use the Microsoft.ApplicationBlock.Data project, which gives some neat
overloads to set up params (and caches parameters).

I find, more and more, that I am coding code generators that build the
repetitive code for me. Sure, there are times I have to hand code a few
params, but the coding is simple, so I am not too worried. With saves me
about three keystrokes each line in my coding standard, so it is not a big
deal. If you use less explicit code techniques, you may see a greater
keystroke savings.

--
Gregory A. Beamer
MVP; MCP: +I, SE, SD, DBA

************************************************** ********************
Think Outside the Box!
************************************************** ********************
"Zeljko" <tr*******@yahoo.com> wrote in message
news:vn********************************@4ax.com...
Good point in some cases. Still, what to do with built-in, sealed
classes ? E.g. :

OleDbCommand a = new OleDbCommand ("DoThis" , conn);
a.Parameters.Add ..(
a.Parameters.Add ...

instead of

with a.parameters
.add ...
.add ...
end with

Zeljko
On Tue, 28 Oct 2003 15:39:27 GMT, Steve Jorgensen
<no****@nospam.nospam> wrote:
On Tue, 28 Oct 2003 16:21:49 +0100, Zeljko <tr*******@yahoo.com> wrote:
Hi,

I moved from VB6 to C# recently, and am very satisfied with the
result. However, I miss this one feature. The closest thing I could
find is "using" statement, but it requires object to implement
IDisposable interface, and it won't save me the trouble of typing the
variable name. What I want to do is something similar to this vb6
block of code:

Dim a as class1
set a = new class1
with a
.dothis
.dothat
.propertyx = 1
end with

Does the similar feature exist in C#, or no ?


In the course of learning how to think more OOP, Ive figured out that With.. End With is a good idea only in a language like the old VB with poor OOPcapabilities.
When you find yourself doing a series of operations on the same object, youusually have a case for making this a method of that object's class or a
new inheriting class. Once the code is in the class that is being acted
on, the object reference becomes implicit, just as it would be in a With
block.

Nov 15 '05 #12

"Jon Skeet [C# MVP]" <sk***@pobox.com> wrote in message
news:MP************************@msnews.microsoft.c om...

As Steve said - if you're doing a whole load of operations with a
single object, chances are that functionality should be encapsulated
within the object itself.

And, if these are properties you are setting, you should consider creating a
constructor that allows you to set the properties at instantiation rather
than relying on a "with" construct.

--
Gregory A. Beamer
MVP; MCP: +I, SE, SD, DBA

************************************************** ********************
Think Outside the Box!
************************************************** ********************
Nov 15 '05 #13
Don't get me wrong - I'm perfectly happy with C#. This is just one of
the few rare things that I miss. But wouldn't want to switch back to
VB6 just because of that :)

On Tue, 28 Oct 2003 10:12:09 -0600, "Cowboy \(Gregory A. Beamer\)"
<No************@comcast.netNoSpamM> wrote:
It gets easier in the next version of .NET, but C# will not ever be the same
as VB.NET. You have to make some language decisions.


Nov 15 '05 #14

On Tue, 28 Oct 2003 10:18:59 -0600, "Cowboy \(Gregory A. Beamer\)"
<No************@comcast.netNoSpamM> wrote:
This is a good point, but you have to ask yourself a couple of questions:

1. Which methodology is more explicit? Overall, explicit code is less buggy
and easier to read.
As I said - it might be just because I'm used to it, but I don't think
the with code is less readable. :)
2. Which methodology is easier to maintain? I find most people that heavily
use "with" in Visual Basic are pretty much being lazy.
As I said too - yes, my main reason for objecting is being lazy :)

With the OleDb command params, I generally use one of the following
methodologies:

1. Create a code generator based on the stored procedure. A poor man's
version can be created with SQL Query Analyzer and Excel (or Access builder,
et al)

2. Create a generic layer that matches sproc params with a list of some type
(dictionary, hashtable, et al) so I do not have to create the params
programatically.

3. Use the Microsoft.ApplicationBlock.Data project, which gives some neat
overloads to set up params (and caches parameters).


I might try that one - never used it.

Thanks for the thoughts,

Zeljko
Nov 15 '05 #15
Sure I do - but I still miss with when I want it and need it as in the
previous example :) Anyhow - I already sound too picky - lack of with
block is something I can live with :) Just wondered why they left it
out. The article Jon posted pretty much explained their reasons

Zeljko.
On Tue, 28 Oct 2003 10:11:32 -0600, "Cowboy \(Gregory A. Beamer\)"
<No************@comcast.netNoSpamM> wrote:
No! It would be an interesting addition, but it is not there. Overall, if
you start working with constructors, you will find that you do not need with
blocks anyway. ;->


Nov 15 '05 #16
BTW, what did u mean by "It gets easier in the next version of .NET, "
?

Zeljko
On Tue, 28 Oct 2003 10:12:09 -0600, "Cowboy \(Gregory A. Beamer\)"
<No************@comcast.netNoSpamM> wrote:
It gets easier in the next version of .NET, but C# will not ever be the same
as VB.NET. You have to make some language decisions.


Nov 15 '05 #17
>See http://www.gotdotnet.com/team/csharp.../ask.aspx#with
for the reasons it's not in C#.

Good link. I had not seen that before now, Jon. Thanks for the post.

Nov 15 '05 #18
>Only if you don't value readability. I believe that "with" reduces
readability - and over the course of some code's lifetime, far more of
the time is likely to be spent reading it than typing it.

Yes... over the code's lifetime, I would venture that far more time is going to
be spent reading it by people other than the original coder. :)

Been there, done that.

Nov 15 '05 #19
On Tue, 28 Oct 2003 16:21:49 +0100, Zeljko <tr*******@yahoo.com>
wrote:
Hi,

I moved from VB6 to C# recently, and am very satisfied with the
result. However, I miss this one feature. The closest thing I could
find is "using" statement, but it requires object to implement
IDisposable interface, and it won't save me the trouble of typing the
variable name. What I want to do is something similar to this vb6
block of code:

Dim a as class1
set a = new class1
with a
.dothis
.dothat
.propertyx = 1
end with

Does the similar feature exist in C#, or no ?

Best regards,

Zeljko


"With" is an abomination and should be cast into the pits of hell.

It creates maintenance nightmares and no professional would ever
use it in any language that supports it.

Oz

Nov 15 '05 #20
ozbear <oz****@no.bigpond.spam.com> wrote:
"With" is an abomination and should be cast into the pits of hell.

It creates maintenance nightmares and no professional would ever
use it in any language that supports it.


I'm sure you're holding back... why don't you tell us what you really
think? ;)

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet
If replying to the group, please do not mail me too
Nov 15 '05 #21
On Tue, 28 Oct 2003 10:18:59 -0600, "Cowboy \(Gregory A. Beamer\)"
<No************@comcast.netNoSpamM> wrote:
This is a good point, but you have to ask yourself a couple of questions:

1. Which methodology is more explicit? Overall, explicit code is less buggy
and easier to read.
I disagree. Code that says more in fewer words is easier to read. A with
block can be abused, but a reasonably short With block in ol' VB is a very
good thing. It removes duplication which removes a chance of a
hard-to-find typo, particularly when modifying code later.
2. Which methodology is easier to maintain? I find most people that heavily
use "with" in Visual Basic are pretty much being lazy.
Most? I don't know. Almost any good feature can be abused by someone
being lazy. Again, I'm not saying With should be added to C#, but I'd
argue it was more of a good thing than a bad one in VB.
With the OleDb command params, I generally use one of the following
methodologies:

1. Create a code generator based on the stored procedure. A poor man's
version can be created with SQL Query Analyzer and Excel (or Access builder,
et al)

2. Create a generic layer that matches sproc params with a list of some type
(dictionary, hashtable, et al) so I do not have to create the params
programatically.

3. Use the Microsoft.ApplicationBlock.Data project, which gives some neat
overloads to set up params (and caches parameters).

I find, more and more, that I am coding code generators that build the
repetitive code for me. Sure, there are times I have to hand code a few
params, but the coding is simple, so I am not too worried. With saves me
about three keystrokes each line in my coding standard, so it is not a big
deal. If you use less explicit code techniques, you may see a greater
keystroke savings.


All sound like good suggestions to me.
Nov 15 '05 #22
Its amazing how most C# programmers (in their humble opinion) view the
"with" as degrading or at least, not adding to, the readibility of code but
most VB programmers think it does. Therefore it doesn't technically work
either way, it's all in the perception of those who want/like/accept the
feature and those who don't want/like/accept the feature, but not a
technical one. It's a philisophical one.
Thanks,
Shawn
"Jon Skeet [C# MVP]" <sk***@pobox.com> wrote in message
news:MP************************@msnews.microsoft.c om...
Zeljko <tr*******@yahoo.com> wrote:
Right, they don't share anything, but the fact that a block of code is
somehow related to a particular object.


Or objects - you can have multiple objects in a using construct.
I miss it because I'm lazy -
typing . instead of typing first few letters and pressing ctrl+space
is a much faster thing to do. That's a great feature, if u ask me :)


Only if you don't value readability. I believe that "with" reduces
readability - and over the course of some code's lifetime, far more of
the time is likely to be spent reading it than typing it.

As Steve said - if you're doing a whole load of operations with a
single object, chances are that functionality should be encapsulated
within the object itself.

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet
If replying to the group, please do not mail me too

Nov 15 '05 #23
Why? I"ve never had a problem doing so. It's a great conveniece and have
never in 7 years working with VB suffered a single lost second of
productivity as a result of its presence. I never "rely" on it, but I do
take advantage of the features a tool offers as they are there for a reason.
I've never once in my memory suffered from not being able to read code that
used a "with".

I hear no end to the arguments of C# developers saying how difficult it is
to read. In my experience, I agree, I have worked with many C# and Java
developers who struggle to no end. My take? Adapt and move on. Most VB
programmers don't struggle as much as I've personally witnessed non VB
programmers struggle with it. I'm not making a blanket statement. This is
just an observation based on experience, is all.

Me? I'm primarily an Assembly and C++ programmer, who gets paid well to do
VB and C#. I enjoy all the features of all the languages and often pick the
best tool for the job when given the choice. When not given the choice, I
use the features available.
Thanks,
Shawn

"Cowboy (Gregory A. Beamer)" <No************@comcast.netNoSpamM> wrote in
message news:%2***************@TK2MSFTNGP11.phx.gbl...

"Jon Skeet [C# MVP]" <sk***@pobox.com> wrote in message
news:MP************************@msnews.microsoft.c om...

As Steve said - if you're doing a whole load of operations with a
single object, chances are that functionality should be encapsulated
within the object itself.

And, if these are properties you are setting, you should consider creating

a constructor that allows you to set the properties at instantiation rather
than relying on a "with" construct.

--
Gregory A. Beamer
MVP; MCP: +I, SE, SD, DBA

************************************************** ********************
Think Outside the Box!
************************************************** ********************

Nov 15 '05 #24
It won't because it's not the same language. That never stopped a language
designer from "borrowing" features of other languages. "With" is not unique
to VB. It also exists in JScript and JScript.NET. The syntax of it in
JScript can be improved, though. Nonetheless, that only proves its not a
VB only feature.
Thanks,
Shawn

"Cowboy (Gregory A. Beamer)" <No************@comcast.netNoSpamM> wrote in
message news:OT*************@TK2MSFTNGP11.phx.gbl...
It gets easier in the next version of .NET, but C# will not ever be the same as VB.NET. You have to make some language decisions.

--
Gregory A. Beamer
MVP; MCP: +I, SE, SD, DBA

************************************************** ********************
Think Outside the Box!
************************************************** ********************
"Zeljko" <tr*******@yahoo.com> wrote in message
news:ai********************************@4ax.com...
Right, they don't share anything, but the fact that a block of code is
somehow related to a particular object. I miss it because I'm lazy -
typing . instead of typing first few letters and pressing ctrl+space
is a much faster thing to do. That's a great feature, if u ask me :)

Thanks for the article,

Zeljko

On Tue, 28 Oct 2003 15:30:32 -0000, Jon Skeet [C# MVP]
<sk***@pobox.com> wrote:
Zeljko <tr*******@yahoo.com> wrote:
> I moved from VB6 to C# recently, and am very satisfied with the
> result. However, I miss this one feature. The closest thing I could
> find is "using" statement, but it requires object to implement
> IDisposable interface, and it won't save me the trouble of typing the
> variable name.

In fact, "using" and "with" share basically no characteristics :)

> What I want to do is something similar to this vb6
> block of code:
>
> Dim a as class1
> set a = new class1
> with a
> .dothis
> .dothat
> .propertyx = 1
> end with
>
> Does the similar feature exist in C#, or no ?

No, it doesn't. (Thanks goodness, in my view.)

See http://www.gotdotnet.com/team/csharp.../ask.aspx#with
for the reasons it's not in C#.


Nov 15 '05 #25
The C# syntax should be closer to:

with( a.parameters ) {
.add...
.add..
}
Thanks,
Shawn
"Zeljko" <tr*******@yahoo.com> wrote in message
news:vn********************************@4ax.com...
Good point in some cases. Still, what to do with built-in, sealed
classes ? E.g. :

OleDbCommand a = new OleDbCommand ("DoThis" , conn);
a.Parameters.Add ..(
a.Parameters.Add ...

instead of

with a.parameters
.add ...
.add ...
end with

Zeljko
On Tue, 28 Oct 2003 15:39:27 GMT, Steve Jorgensen
<no****@nospam.nospam> wrote:
On Tue, 28 Oct 2003 16:21:49 +0100, Zeljko <tr*******@yahoo.com> wrote:
Hi,

I moved from VB6 to C# recently, and am very satisfied with the
result. However, I miss this one feature. The closest thing I could
find is "using" statement, but it requires object to implement
IDisposable interface, and it won't save me the trouble of typing the
variable name. What I want to do is something similar to this vb6
block of code:

Dim a as class1
set a = new class1
with a
.dothis
.dothat
.propertyx = 1
end with

Does the similar feature exist in C#, or no ?


In the course of learning how to think more OOP, Ive figured out that With.. End With is a good idea only in a language like the old VB with poor OOPcapabilities.
When you find yourself doing a series of operations on the same object, youusually have a case for making this a method of that object's class or a
new inheriting class. Once the code is in the class that is being acted
on, the object reference becomes implicit, just as it would be in a With
block.

Nov 15 '05 #26
See inline:

"Cowboy (Gregory A. Beamer)" <No************@comcast.netNoSpamM> wrote in
message news:%2***************@TK2MSFTNGP11.phx.gbl...
This is a good point, but you have to ask yourself a couple of questions:

1. Which methodology is more explicit? Overall, explicit code is less buggy and easier to read.
2. Which methodology is easier to maintain? I find most people that heavily use "with" in Visual Basic are pretty much being lazy.
And if they are, so? Work smarter, not harder. It helps get the project
done faster. I find that most people who use it use it because it's there
and because it requires less typing. That's certainly why I use it. But
overall, I'm more productive compared to many of my peers (not because of
the with statement) but the with statement helps. I don't over use it, I
use it when it makes sense.

With the OleDb command params, I generally use one of the following
methodologies:

1. Create a code generator based on the stored procedure. A poor man's
version can be created with SQL Query Analyzer and Excel (or Access builder, et al)

2. Create a generic layer that matches sproc params with a list of some type (dictionary, hashtable, et al) so I do not have to create the params
programatically.

3. Use the Microsoft.ApplicationBlock.Data project, which gives some neat
overloads to set up params (and caches parameters).

I find, more and more, that I am coding code generators that build the
repetitive code for me. Sure, there are times I have to hand code a few
params, but the coding is simple, so I am not too worried. With saves me
about three keystrokes each line in my coding standard, so it is not a big
deal. If you use less explicit code techniques, you may see a greater
keystroke savings.

--
Gregory A. Beamer
MVP; MCP: +I, SE, SD, DBA

************************************************** ********************
Think Outside the Box!
************************************************** ********************
"Zeljko" <tr*******@yahoo.com> wrote in message
news:vn********************************@4ax.com...
Good point in some cases. Still, what to do with built-in, sealed
classes ? E.g. :

OleDbCommand a = new OleDbCommand ("DoThis" , conn);
a.Parameters.Add ..(
a.Parameters.Add ...

instead of

with a.parameters
.add ...
.add ...
end with

Zeljko
On Tue, 28 Oct 2003 15:39:27 GMT, Steve Jorgensen
<no****@nospam.nospam> wrote:
On Tue, 28 Oct 2003 16:21:49 +0100, Zeljko <tr*******@yahoo.com> wrote:

>Hi,
>
>I moved from VB6 to C# recently, and am very satisfied with the
>result. However, I miss this one feature. The closest thing I could
>find is "using" statement, but it requires object to implement
>IDisposable interface, and it won't save me the trouble of typing the
>variable name. What I want to do is something similar to this vb6
>block of code:
>
> Dim a as class1
> set a = new class1
> with a
> .dothis
> .dothat
> .propertyx = 1
> end with
>
>Does the similar feature exist in C#, or no ?

In the course of learning how to think more OOP, Ive figured out that With.. End With is a good idea only in a language like the old VB with poor OOPcapabilities.
When you find yourself doing a series of operations on the same object, youusually have a case for making this a method of that object's class or anew inheriting class. Once the code is in the class that is being actedon, the object reference becomes implicit, just as it would be in a Withblock.


Nov 15 '05 #27
See inline:

"ozbear" <oz****@no.bigpond.spam.com> wrote in message
news:3f9f81a7.276717718@news-server...
On Tue, 28 Oct 2003 16:21:49 +0100, Zeljko <tr*******@yahoo.com>
wrote:
Hi,

I moved from VB6 to C# recently, and am very satisfied with the
result. However, I miss this one feature. The closest thing I could
find is "using" statement, but it requires object to implement
IDisposable interface, and it won't save me the trouble of typing the
variable name. What I want to do is something similar to this vb6
block of code:

Dim a as class1
set a = new class1
with a
.dothis
.dothat
.propertyx = 1
end with

Does the similar feature exist in C#, or no ?

Best regards,

Zeljko
"With" is an abomination and should be cast into the pits of hell.

It creates maintenance nightmares and no professional would ever
use it in any language that supports it.


I've never in 7 years experienced one second of lost time due to
"deciphering" a with statement code block. Yes, I'm a professional, and I
do use it. I've never seen a VB programmer struggle or increase maintenance
issues due to the presence of a "With" block.

The only people I've ever witnessed struggle with it are C# and more so,
Java programmers who just can't seem to grasp it. Of course, they would
struggle with it. Of course, if it was a feature of the language they would
better understand it. If they were maintaining a VB app, there will be
increased maintenance because they are maintaining code in a language they
don't speak well.

I seem to find more C# programmers complain about how it decreases
readibility and increases maintainibility but I don't see too many VB
programmers (who use the feature) say the same thing. Sure it can be
abused, as can any number of features in C#. That doesn't make C# evil.

There are places in C# where I would greatly welcome it. I create custom
web controls, and in the Render methods, I have scores of lines of code that
repeat the same "HtmlTextWriter..." that could easily save typing by using a
with. I actually did start the project over in VB for reasons of having to
type too much in C# and, even though I'm very profficient in C#, I get the
job done quicker in VB because I have to type less, my hands hurt less from
all the massive typing, and so on. Actually, I think it's the better
intellisense features of VB but I degress. I did not make that choice
because of the lack of a "with" but the point being, I'm going to type less
when I have more to do. Arguing with me that I'm "lazy" and less
"professional" as a result wouldn't hold well if we were programming
together in the real world.

But we all have our opinions. In the opinion of many C# programmers, "with"
is to be abhorred. In the opinion of VB programmers, "with" is a welcome
feature. "With" also exists in JScript, by the way, but its syntax could be
improved.
Flame me for that if you wish. But I do use the language that's going to
help me get where I want to go the quickest. For other projects, I've used
C# because it makes more sense to do so. I enjoy both languages and get
paid to do so.
Thanks,
Shawn
Oz

Nov 15 '05 #28

"Shawn B." <le****@html.com> wrote in message
news:%2****************@TK2MSFTNGP11.phx.gbl...
See inline:

"ozbear" <oz****@no.bigpond.spam.com> wrote in message
news:3f9f81a7.276717718@news-server...
On Tue, 28 Oct 2003 16:21:49 +0100, Zeljko <tr*******@yahoo.com>
wrote:
Hi,

I moved from VB6 to C# recently, and am very satisfied with the
result. However, I miss this one feature. The closest thing I could
find is "using" statement, but it requires object to implement
IDisposable interface, and it won't save me the trouble of typing the
variable name. What I want to do is something similar to this vb6
block of code:

Dim a as class1
set a = new class1
with a
.dothis
.dothat
.propertyx = 1
end with

Does the similar feature exist in C#, or no ?

Best regards,

Zeljko
"With" is an abomination and should be cast into the pits of hell.

It creates maintenance nightmares and no professional would ever
use it in any language that supports it.


I've never in 7 years experienced one second of lost time due to
"deciphering" a with statement code block. Yes, I'm a professional, and I
do use it. I've never seen a VB programmer struggle or increase

maintenance issues due to the presence of a "With" block.

The only people I've ever witnessed struggle with it are C# and more so,
Java programmers who just can't seem to grasp it. Of course, they would
struggle with it. Of course, if it was a feature of the language they would better understand it. If they were maintaining a VB app, there will be
increased maintenance because they are maintaining code in a language they
don't speak well.

I seem to find more C# programmers complain about how it decreases
readibility and increases maintainibility but I don't see too many VB
programmers (who use the feature) say the same thing. Sure it can be
abused, as can any number of features in C#. That doesn't make C# evil.

There are places in C# where I would greatly welcome it. I create custom
web controls, and in the Render methods, I have scores of lines of code that repeat the same "HtmlTextWriter..." that could easily save typing by using a with. I actually did start the project over in VB for reasons of having to type too much in C# and, even though I'm very profficient in C#, I get the
job done quicker in VB because I have to type less, my hands hurt less from all the massive typing, and so on. Actually, I think it's the better
intellisense features of VB but I degress. I did not make that choice
because of the lack of a "with" but the point being, I'm going to type less when I have more to do. Arguing with me that I'm "lazy" and less
"professional" as a result wouldn't hold well if we were programming
together in the real world.

But we all have our opinions. In the opinion of many C# programmers, "with" is to be abhorred. In the opinion of VB programmers, "with" is a welcome
feature. "With" also exists in JScript, by the way, but its syntax could be improved.
Flame me for that if you wish. But I do use the language that's going to
help me get where I want to go the quickest. For other projects, I've used C# because it makes more sense to do so. I enjoy both languages and get
paid to do so.

I used With in VB and don't regret it because I used it carefully, in only
some small circumstances. But I don't particularly care for it when coders
start writing long (or worse, nested) with statements to the point I cannot
determine what exactly is going on on a single screen

I simply don't think its something that C# needs, its not *THAT* important,
other features would be more valuable
Thanks,
Shawn
Oz


Nov 15 '05 #29
Out of curiousity. What do you do that requires assembler?

I have a mental block to that language. It reminds me of D's and E's in
compsci. Forced to learn it to master the debugger, but still hate its guts.

--
-----------
Got TidBits?
Get it here: www.networkip.net/tidbits
"Shawn B." <le****@html.com> wrote in message
news:uP**************@tk2msftngp13.phx.gbl...
Why? I"ve never had a problem doing so. It's a great conveniece and have
never in 7 years working with VB suffered a single lost second of
productivity as a result of its presence. I never "rely" on it, but I do
take advantage of the features a tool offers as they are there for a reason. I've never once in my memory suffered from not being able to read code that used a "with".

I hear no end to the arguments of C# developers saying how difficult it is
to read. In my experience, I agree, I have worked with many C# and Java
developers who struggle to no end. My take? Adapt and move on. Most VB
programmers don't struggle as much as I've personally witnessed non VB
programmers struggle with it. I'm not making a blanket statement. This is just an observation based on experience, is all.

Me? I'm primarily an Assembly and C++ programmer, who gets paid well to do VB and C#. I enjoy all the features of all the languages and often pick the best tool for the job when given the choice. When not given the choice, I
use the features available.
Thanks,
Shawn

"Cowboy (Gregory A. Beamer)" <No************@comcast.netNoSpamM> wrote in
message news:%2***************@TK2MSFTNGP11.phx.gbl...

"Jon Skeet [C# MVP]" <sk***@pobox.com> wrote in message
news:MP************************@msnews.microsoft.c om...

As Steve said - if you're doing a whole load of operations with a
single object, chances are that functionality should be encapsulated
within the object itself.

And, if these are properties you are setting, you should consider creating a
constructor that allows you to set the properties at instantiation

rather than relying on a "with" construct.

--
Gregory A. Beamer
MVP; MCP: +I, SE, SD, DBA

************************************************** ********************
Think Outside the Box!
************************************************** ********************


Nov 15 '05 #30
I program windows apps as a hobby in x86 asm. On another note, I program
SNES (16-bit 65c02) in assembly and the original 8-bit NES in asm. But I
enjoy (as a hobby) win32 programming in asm. Nothing I do requires it. But
I do it because I enjoy making large business applications that consume
about 30MB in C++/or VB, but 300k in asm.
Thanks,
Shawn

"Alvin Bruney" <vapordan_spam_me_not@hotmail_no_spamhotmail.com > wrote in
message news:uR**************@TK2MSFTNGP10.phx.gbl...
Out of curiousity. What do you do that requires assembler?

I have a mental block to that language. It reminds me of D's and E's in
compsci. Forced to learn it to master the debugger, but still hate its guts.
--
-----------
Got TidBits?
Get it here: www.networkip.net/tidbits
"Shawn B." <le****@html.com> wrote in message
news:uP**************@tk2msftngp13.phx.gbl...
Why? I"ve never had a problem doing so. It's a great conveniece and have
never in 7 years working with VB suffered a single lost second of
productivity as a result of its presence. I never "rely" on it, but I do take advantage of the features a tool offers as they are there for a

reason.
I've never once in my memory suffered from not being able to read code

that
used a "with".

I hear no end to the arguments of C# developers saying how difficult it is to read. In my experience, I agree, I have worked with many C# and Java
developers who struggle to no end. My take? Adapt and move on. Most VB programmers don't struggle as much as I've personally witnessed non VB
programmers struggle with it. I'm not making a blanket statement. This

is
just an observation based on experience, is all.

Me? I'm primarily an Assembly and C++ programmer, who gets paid well to

do
VB and C#. I enjoy all the features of all the languages and often pick

the
best tool for the job when given the choice. When not given the choice, I use the features available.
Thanks,
Shawn

"Cowboy (Gregory A. Beamer)" <No************@comcast.netNoSpamM> wrote in message news:%2***************@TK2MSFTNGP11.phx.gbl...

"Jon Skeet [C# MVP]" <sk***@pobox.com> wrote in message
news:MP************************@msnews.microsoft.c om...
>
> As Steve said - if you're doing a whole load of operations with a
> single object, chances are that functionality should be encapsulated
> within the object itself.
And, if these are properties you are setting, you should consider

creating
a
constructor that allows you to set the properties at instantiation

rather than relying on a "with" construct.

--
Gregory A. Beamer
MVP; MCP: +I, SE, SD, DBA

************************************************** ********************
Think Outside the Box!
************************************************** ********************



Nov 15 '05 #31
Shawn B. <le****@html.com> wrote:
2. Which methodology is easier to maintain? I find most people that heavily
use "with" in Visual Basic are pretty much being lazy.

And if they are, so? Work smarter, not harder. It helps get the project
done faster. I find that most people who use it use it because it's there
and because it requires less typing. That's certainly why I use it. But
overall, I'm more productive compared to many of my peers (not because of
the with statement) but the with statement helps. I don't over use it, I
use it when it makes sense.


While I agree with laziness being generally a good thing in programmers
(look at Larry Wall's suggestions for a good Perl programmer), I don't
think that "it requires less typing" is *ever* a good enough reason in
itself for doing anything with code. Readability has to come absolutely
first, IMO.

Admittedly I'm not *quite* hard-working enough to always write braces
after if blocks:

if (x)
{
doSomething();
}

but I think in general it's a good idea.

On the other hand, I *do* include white space for the sake of
readability even though it means more typing. I *do* format my code for
readability even though it means more work. I *do* comment my code even
though it means more work. I *do* use Convert.ToString(...) instead of
""+... even though it's slightly longer.

Eventually, the time it takes to type the code is likely to be nearly
insignificant in the course of the lifetime of the code - I'll usually
spend more time thinking about it, debugging it to get it working in
the first place, and maintaining it later on. Anything that helps with
*those* tasks is worth much more than something that gives a short term
typing benefit at the cost of readability - and the nature of "with"
just reduces readability, IMO. It wouldn't be quite so bad if you could
only have one level of "with"-ness (i.e. no nested "with"s) but I still
don't like it in general. I'd still rather give a variable a meaningful
name in one line and use that name in subsequent lines to make it
explicit what's going on.

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet
If replying to the group, please do not mail me too
Nov 15 '05 #32
Shawn B. wrote:
Its amazing how most C# programmers (in their humble opinion) view the
"with" as degrading or at least, not adding to, the readibility of code but
most VB programmers think it does. Therefore it doesn't technically work
either way, it's all in the perception of those who want/like/accept the
feature and those who don't want/like/accept the feature, but not a
technical one. It's a philisophical one.


However, as the "Ask a Language Designer" article
(http://www.gotdotnet.com/team/csharp.../ask.aspx#with)
points out, this snippet of VB code:

With MyDataStructure.GetButton(44)
.Text = "Hello"
.BackColor = Color.Blue
End With

is equivalent to:

Dim b as Button = MyDataStructure.GetButton(44)
b.Text = "Hello"
b.BackColor = Color.Blue

which you can do in C# like so:

Button b = MyDataStructure.GetButton(44);
b.Text = "Hello"
b.BackColor = Color.Blue
So C# does really have the basic functionality of a "With" construct, it
just has a different syntax. I agree with you that it's a philosophical
(or probably more likely a religious) difference. I don't miss the
"With" construct, which is easy for me, since I've never really been a
VB programmer. But, if MS added it to C#, I wouldn't think the world
had ended.

For me, I think it boils down to:

if you really, really need With/End With, use VB.NET

if you want With/End With functionality in C#, create a
short-lived, short-named object and deal with that.
.... snipped old thread ...

--
mikeb

Nov 15 '05 #33

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

Similar topics

2
by: Gary Harvey | last post by:
I have a data intensive program that requires all data to be present in memory. I keep running out of memory at about 2G whenever I run my program. I tried using a 64 bit version of Perl and hit...
9
by: Thomas J. Clancy | last post by:
I was wondering if anyone knew of a way to use std::copy() and istream_iterator<>/ostream_iterator<> write a file copy function that is quick and efficient. Doing this messes up the file because...
1
by: Johnny | last post by:
Hi together, I'm new to XML and have problems with FOP (http://www.apache.org/dyn/closer.cgi/xml/fop) When I try to convert my .fo document to .pdf, I get this message: fo:39:26...
1
by: steflhermitte | last post by:
Dear cpp-ians, I want to apply a stratified sampling on an image. Following simplified example will explain my problem. The original image I with nrows and ncols is now a vector V of length...
6
by: mike | last post by:
I have a page that uses a some javascript and it works fine in IE but fails to work in Firefox. Basically what I'm trying to do is have 3 iframes on a page but only displaying one of them at a...
5
by: Zambien | last post by:
Hi all, Here's my problem. I have tables that are using the menu/submenu idea for hiding rows. This works fine in IE (of course) and does show/hide correctly in netscape, but as soon as the...
0
by: Lokkju | last post by:
I am pretty much lost here - I am trying to create a managed c++ wrapper for this dll, so that I can use it from c#/vb.net, however, it does not conform to any standard style of coding I have seen....
23
by: VB Programmer | last post by:
Variable scope doesn't make sense to me when it comes to Try Catch Finally. Example: In order to close/dispose a db connection you have to dim the connection outside of the Try Catch Finally...
3
by: Alien | last post by:
Hi I am having some problems with the annoying segmentation error when I try to run a program that creates a linked list of numbers that are passed on to it. All I am trying to do is to create a...
5
matheussousuke
by: matheussousuke | last post by:
Hello, I'm using tiny MCE plugin on my oscommerce and it is inserting my website URL when I use insert image function in the emails. The goal is: Make it send the email with the URL...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
1
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...

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.