With - End with block in C#

Z

Zeljko

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
 
J

Jon Skeet [C# MVP]

Zeljko said:
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/learn/columns/ask.aspx#with
for the reasons it's not in C#.
 
S

Steve Jorgensen

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.
 
Z

Zeljko

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
 
J

Jon Skeet [C# MVP]

Zeljko said:
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.
 
Z

Zeljko

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
 
Z

Zeljko

Or objects - you can have multiple objects in a using construct.

Didn't know that.
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
 
C

Cowboy \(Gregory A. Beamer\)

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!
**********************************************************************
 
C

Cowboy \(Gregory A. Beamer\)

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!
**********************************************************************
 
C

Cowboy \(Gregory A. Beamer\)

Jon Skeet said:
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!
**********************************************************************
 
S

Steve Jorgensen

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.
 
C

Cowboy \(Gregory A. Beamer\)

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!
**********************************************************************
 
Z

Zeljko

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 :)
 
Z

Zeljko

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
 
Z

Zeljko

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.
 
K

Kerry Sanders

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.
 
O

ozbear

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
 

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