Dave Burke - Freelance .NET Developer specializing in Online Communities

A freelance .NET Developer

Coding With a With in C# (sounds like I'm lithping, doesn't it?)

I've been coding in C# for, oh, a good year now, but I started my .NET journey in VB.  There are things I miss from VB, but one thing I definitely miss is the WITH statement, as in

with DropDown
 .DataSource = dt.defaultview
 .DataValueField = "id"
 .DataTextField = "desc"
 .DataBind()
end with


“And that's all I've got to say about that.” -- Forest Gump

Posted: Sep 26 2003, 09:57 PM by daveburke | with 5 comment(s)
Filed under:

Comments

Roland Weigelt said:

The piece of code you posted is a *good* example for the use of the "With" statement. Unfortunately, each time I read someone's VB source code, I get something like this:

With Foo
.Bar="Hello World"
If (x>y) Then
...
blah blah blah
...
Else
...
blah blah blah
...
End If
DoSomething()
If (y=z) Then
...
DoSomethingElse()
...
End If
.Baz=1234 ' huh, what?
End With

What I'm trying to say: "With" can be a nice feature, but it also tends to be misused. I wouldn't use it anyway, but I also read a lot of other people's code...
I think C# is definitely better off without the "With" statement.

Roland
# September 27, 2003 7:40 AM

Dave Burke said:

Thanks, Roland. I hadn't ever used it like that, but given your example I would certainly agree on the potential for "with" being misused and for muddying up code rather than reducing it.
# September 27, 2003 8:16 AM

Mike Krautstrunk said:

A bit late to the party, but a 4Guys article (http://www.4guysfromrolla.com/webtech/010202-1.shtml) shows a pretty dramatic drop-off in performance when using With.
# October 2, 2003 11:05 AM

Dave Burke said:

Huh! Didn't know it, Mike. Thx! I need to spend more time at 4Guys.
# October 2, 2003 11:25 AM

Mabinogi Gold said:

good post, thanks!!!

# July 7, 2008 2:35 AM
Leave a Comment

(required) 

(required) 

(optional)

(required)