With - End With

  • Thread starter Thread starter Vik
  • Start date Start date
V

Vik

Is there in C# an analog of the With <some obj> - End With construction of
VB?

Thanks.
 
Vik said:
What's wrong with it?

It makes searching more difficult, you might have an object and you know
it's, say, Open method gets called somewhere but searching returns no
results.

It looks like a for loop or an if block or some other branch but it's not.

You have to keep looking up to the top of the with block to find out what
variable is being used.

It's actually slower in vb6. This isn't really a reason not to use it but
speed is definately not a reason to use it.

The main reason I think we're better off without it is that it gets abused.
There are some rare cases where it's passable, such as

With myListView.Items(100)
.Text = "Hello";
.Font = new font(blah);
.Image = whatever
End With

although even in these case I think it's better off with an seperate
variable being defined as someone might add several lines to calculate the
text of the item and then the code starts to look obfuscated.

Michael
 
Thanks Michael.

I supposed the code is faster with the With block though it's more difficult
to debug (because pointing a cursor on an expression in the With block does
not show its value).

Vik
 
My issue is the maintenance.

With txtBox1
.Text = "hello"
.Visible = True
End With

Thats not so bad.

But when you have 40 lines of code by a previous developer, and down in the
middle is some line like:

.DoSomething


It just causes a more maintenance issue. And when it gets out of control,
you're always checking "which with object is it this time?"

Eventually, it gets abused... has been my experience.


I like non ambigious code.

I dont' like

TextBox textbox = new TextBox();
in C#
(the only distinction is the case between the object name, and the
instantiation name)

I like "this." before member variables.

this.m_name = "the sun";
this.m_dob = "4/4/1900";



I like non ambigous code. With and ".DoSomething" is just another version
of it.
 
sloan said:
I like "this." before member variables.

this.m_name = "the sun";
this.m_dob = "4/4/1900";

I don't see the logic behind adding this., you've already got 2 chrs to
distinguish it as a module level.

Michael
 
Hi,
the with block suppose to run faster it if you want to access the
multiple elements under a class.
The test sample just access one element. so it doesn't really give any
performance benefit.


Rgds,
kean
 
keanhee said:
Hi,
the with block suppose to run faster it if you want to access the multiple
elements under a class.
The test sample just access one element. so it doesn't really give any
performance benefit.

That's not true. Add as many as you like and With will still be slower.

Michael
 

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

Back
Top