473,587 Members | 2,547 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

C# equivelent of VBA "with..end with" statement

Can someone tell me if there is a C# equivelent to the VBA 'with'
statement that works like this:

Set myControl = CommandBars(Pop UpToUse).Contro ls.Add(msoContr olButton,
before:=5)
With myControl
.BeginGroup = True
.Caption = "Insert Row(s)"
.OnAction = "InsertRows "
.FaceId = 295
End With
Thanks
Jun 27 '08 #1
21 2523
Mike N. <mi************ *@gmail.comwrot e:
Can someone tell me if there is a C# equivelent to the VBA 'with'
statement that works like this:
No, there isn't. C# 3 has object initializers so that when you call a
constructor (and only at that time) you can set a bunch of properties,
like this:

Button button = new Button { Text = "Hi", Size = ... };

Likewise there are collection initializers:

List<stringstri ngs = new List<string{ "Hi", "There" };

But no With statement.

--
Jon Skeet - <sk***@pobox.co m>
Web site: http://www.pobox.com/~skeet
Blog: http://www.msmvps.com/jon.skeet
C# in Depth: http://csharpindepth.com
Jun 27 '08 #2
On Fri, 06 Jun 2008 13:16:34 -0700, Mike N. <mi************ *@gmail.com>
wrote:
Can someone tell me if there is a C# equivelent to the VBA 'with'
statement that works like this:
There is none.

If it makes you feel better, when this has come up in the past, others
have pointed out why the VBA "With" statement can lead to maintenance
problems, especially when they are nested. :)

Pete
Jun 27 '08 #3
Peter Duniho wrote:
On Fri, 06 Jun 2008 13:16:34 -0700, Mike N. <mi************ *@gmail.com>
wrote:
>Can someone tell me if there is a C# equivelent to the VBA 'with'
statement that works like this:

There is none.

If it makes you feel better, when this has come up in the past, others
have pointed out why the VBA "With" statement can lead to maintenance
problems, especially when they are nested. :)
But it is rather interesting that it is usually those that use languages
that does not have WITH (C++, Java, C#) that think it is a problem.
Those that actually have used it (in VB or Pascal) does not see
that problem as a real problem.

Arne
Jun 27 '08 #4

"Arne Vajhøj" <ar**@vajhoej.d kwrote in message
news:48******** *************** @news.sunsite.d k...
Peter Duniho wrote:
>On Fri, 06 Jun 2008 13:16:34 -0700, Mike N. <mi************ *@gmail.com>
wrote:
>>Can someone tell me if there is a C# equivelent to the VBA 'with'
statement that works like this:

There is none.

If it makes you feel better, when this has come up in the past, others
have pointed out why the VBA "With" statement can lead to maintenance
problems, especially when they are nested. :)

But it is rather interesting that it is usually those that use languages
that does not have WITH (C++, Java, C#) that think it is a problem.
Those that actually have used it (in VB or Pascal) does not see
that problem as a real problem.

Arne
BUT note that Wirth chose /not/ to include "with" in Pascal's descendants!
Jun 27 '08 #5
Whenever the religious wars flare up around languages, the C# people are
always dissing VB (I notice that it never seems to go the other way) but in
fact the VB people have given us some quite useful capabilities that the C#
people haven't seen fit to offer us. The With statement is one, better
support for late binding is another (particularly important when you're
talking to certain Win32 PIA assemblies like the Office libraries), simple
IDE setup for event handlers is yet another, and I just happen to notice, by
the way, that if you're going to do programming in Sql Server 2005
Integration Services VB is the only option you have. They intended to do C#,
or so they say, but never actually got around to it.

As you can probably see, I use both languages quite happily, and I'm not
interested in responding to flames.

Have a nice day,
Tom Dacon
Dacon Software Consulting
"Arne Vajhøj" <ar**@vajhoej.d kwrote in message
news:48******** *************** @news.sunsite.d k...
Peter Duniho wrote:
>On Fri, 06 Jun 2008 13:16:34 -0700, Mike N. <mi************ *@gmail.com>
wrote:
>>Can someone tell me if there is a C# equivelent to the VBA 'with'
statement that works like this:

There is none.

If it makes you feel better, when this has come up in the past, others
have pointed out why the VBA "With" statement can lead to maintenance
problems, especially when they are nested. :)

But it is rather interesting that it is usually those that use languages
that does not have WITH (C++, Java, C#) that think it is a problem.
Those that actually have used it (in VB or Pascal) does not see
that problem as a real problem.

Arne


Jun 27 '08 #6
On Fri, 06 Jun 2008 17:37:21 -0700, Arne Vajhøj <ar**@vajhoej.d kwrote:
>If it makes you feel better, when this has come up in the past, others
have pointed out why the VBA "With" statement can lead to maintenance
problems, especially when they are nested. :)

But it is rather interesting that it is usually those that use languages
that does not have WITH (C++, Java, C#) that think it is a problem.
I've never asked those who say they'd prefer not to have "With" what other
languages they use.
Those that actually have used it (in VB or Pascal) does not see
that problem as a real problem.
You've surveyed everyone? It never occurred to me you'd have that much
extra time on your hands.

In any case, I have used "With" myself in VB code, and it's fine as far as
it goes. It obviously didn't get put in the language just for looks; it's
useful in certain scenarios. But like some other aspects of VB, it
introduces some semantic ambiguity that can in fact be a maintenance issue.

Does that make it bad? No. Has anyone posted any "flames" on the issue?
No, not that I've seen. I have no idea what Tom's on about. But it's
also true that we get by just find in C# without it, and given the general
philosophy of C# to not provide language features that introduce
ambiguities, it's not hard to understand why it was left out. Even if
it's not a significant liability in VB.

Pete
Jun 27 '08 #7
"Peter Duniho" <Np*********@nn owslpianmk.comw rote in message
news:op******** *******@petes-computer.local. ..
On Fri, 06 Jun 2008 17:37:21 -0700, Arne Vajhøj <ar**@vajhoej.d kwrote:
>>If it makes you feel better, when this has come up in the past, others
have pointed out why the VBA "With" statement can lead to maintenance
problems, especially when they are nested. :)

But it is rather interesting that it is usually those that use languages
that does not have WITH (C++, Java, C#) that think it is a problem.

I've never asked those who say they'd prefer not to have "With" what other
languages they use.
>Those that actually have used it (in VB or Pascal) does not see
that problem as a real problem.

You've surveyed everyone? It never occurred to me you'd have that much
extra time on your hands.

In any case, I have used "With" myself in VB code, and it's fine as far as
it goes. It obviously didn't get put in the language just for looks; it's
useful in certain scenarios. But like some other aspects of VB, it
introduces some semantic ambiguity that can in fact be a maintenance
issue.

Does that make it bad? No. Has anyone posted any "flames" on the issue?
No, not that I've seen. I have no idea what Tom's on about. But it's
also true that we get by just find in C# without it, and given the general
philosophy of C# to not provide language features that introduce
ambiguities, it's not hard to understand why it was left out. Even if
it's not a significant liability in VB.

Pete

Pete, whenever the issue of "With .. End With" comes up, a language religion
war starts. The reality is that the "With ... End With" construct is a hold
over from VB6, where proper use could actually improve performance. I don't
know if the same is true in VB.Net (any version), but the language construct
is still there and still useful in certain situations. As far as being
ambiguous, just don't use nested With ... End With statements and you avoid
ambiguity.

Mike Ober.
Jun 27 '08 #8
On Fri, 06 Jun 2008 22:32:33 -0700, Michael D. Ober
<obermd.@.alum. mit.edu.nospam. wrote:
Pete, whenever the issue of "With .. End With" comes up, a language
religion war starts.
I think we've proven here that's not actually true. Else you have a very
different definition of "war" than I do.
[...] As far as being ambiguous, just don't use nested With ... End
With statements and you avoid ambiguity.
That's true. So?

Pete
Jun 27 '08 #9
Tom Dacon <td****@communi ty.nospamwrote:
Whenever the religious wars flare up around languages, the C# people are
always dissing VB (I notice that it never seems to go the other way)
Rubbish - I've seen it go the other way plenty of times. *Some* VB
developers (obviously it's not all) are more than happy to diss C# due
to case sensitivity, and the fact that it uses braces instead of
Begin/End etc.

I've also seen many disparaging comments from VB developers claiming
that all C# developers are elitist snobs who are more interested in
purity than in getting things done.
but in fact the VB people have given us some quite useful
capabilities that the C# people haven't seen fit to offer us. The
With statement is one
I'm happy with just object initializers - most of the benefit but less
room for abuse.
better support for late binding is another (particularly important
when you're talking to certain Win32 PIA assemblies like the Office
libraries)
Coming in C# 4.
simple IDE setup for event handlers is yet another,
Click on the lightning bolt in the properties tab, then click on the
relevant event. That's not exactly hard, is it? How much simpler is the
VB way? (Of course for Click etc, you can just click in the designer.)
and I just happen to notice, by the way, that if you're going to do
programming in Sql Server 2005 Integration Services VB is the only
option you have. They intended to do C#, or so they say, but never
actually got around to it.
I wasn't aware of that. Assuming it's VB.NET, that does sound odd.

--
Jon Skeet - <sk***@pobox.co m>
Web site: http://www.pobox.com/~skeet
Blog: http://www.msmvps.com/jon.skeet
C# in Depth: http://csharpindepth.com
Jun 27 '08 #10

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

Similar topics

15
2953
by: sara | last post by:
Hi I'm pretty new to Access here (using Access 2000), and appreciate the help and instruction. I gave myself 2.5 hours to research online and help and try to get this one, and I am not getting it. Simple database: I want to have a user enter Supply Orders (just for tracking purposes) by Item. The user may also enter a new item - "new"...
13
2549
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(){}, my_meth2: function(){} }); to define new methods on the MyObj prototype object. Object.extend
5
1867
by: Alexander Myodov | last post by:
Hello, Having heard that Python 2.5 offers some kind of RIIA concept via PEP343, got it downloaded (Windows version) and tried. But it did not work as expected and as wanted. For the time since I first learned Python, the only reason why I just could not use it was inability to localize the lifetime of some variables inside some...
9
1503
by: Mr Flibble | last post by:
Is there an equivalent of VB's "with" statement in c# ?
4
5280
by: darrell | last post by:
I recently wrote a program in Access VBA that contains this snippet of code: For i = 1 to TotalBanks With Me("txtBank" & Chr(i + 64)) .BackColor = vbBlue .ForeColor = vbWhite End With Next i The above code was tested and works.
8
1783
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 with statement is indeed bad. My initial thoughts on with were based on: http://yuiblog.com/blog/2006/04/11/with-statement-considered-harmful/ ...
25
2558
by: samjnaa | last post by:
Please check for sanity and approve for posting at python-dev. In Visual Basic there is the keyword "with" which allows an object- name to be declared as governing the following statements. For example: with quitCommandButton .enabled = true .default = true end with
14
2117
by: Ivan Voras | last post by:
Hi, I'm looking for a construct that's similar to (Turbo) Pascal's "with" statement. I read about the Python's new "with" statement, but I was dissapointed to learn that it does something different (I still don't see how it's better than try..except..finally, but that's not my question). Is there something similar to what I want that's...
2
1395
by: Dmitry Teslenko | last post by:
Hello! I've made some class that can be used with "with statement". It looks this way: class chdir_to_file( object ): .... def __enter__(self): .... def __exit__(self, type, val, tb): ....
0
8215
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. ...
0
8220
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the...
0
6626
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then...
1
5718
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes...
0
5394
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert...
0
3844
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in...
0
3879
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
2358
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
0
1189
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating...

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.