473,403 Members | 2,338 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,403 software developers and data experts.

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(PopUpToUse).Controls.Add(msoControlBut ton,
before:=5)
With myControl
.BeginGroup = True
.Caption = "Insert Row(s)"
.OnAction = "InsertRows"
.FaceId = 295
End With
Thanks
Jun 27 '08 #1
21 2503
Mike N. <mi*************@gmail.comwrote:
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<stringstrings = new List<string{ "Hi", "There" };

But no With statement.

--
Jon Skeet - <sk***@pobox.com>
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.dkwrote in message
news:48***********************@news.sunsite.dk...
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
Bruce C. Baker wrote:
"Arne Vajhøj" <ar**@vajhoej.dkwrote in message
news:48***********************@news.sunsite.dk...
>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.

BUT note that Wirth chose /not/ to include "with" in Pascal's descendants!
Modula-2 has WITH.

Oberon has not (actually it has, but it does something differently).

Arne
Jun 27 '08 #6
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.dkwrote in message
news:48***********************@news.sunsite.dk...
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 #7
On Fri, 06 Jun 2008 17:37:21 -0700, Arne Vajhøj <ar**@vajhoej.dkwrote:
>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 #8
"Peter Duniho" <Np*********@nnowslpianmk.comwrote in message
news:op***************@petes-computer.local...
On Fri, 06 Jun 2008 17:37:21 -0700, Arne Vajhøj <ar**@vajhoej.dkwrote:
>>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 #9
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 #10
Tom Dacon <td****@community.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.com>
Web site: http://www.pobox.com/~skeet
Blog: http://www.msmvps.com/jon.skeet
C# in Depth: http://csharpindepth.com
Jun 27 '08 #11

"Jon Skeet [C# MVP]" <sk***@pobox.comwrote in message
news:MP*********************@msnews.microsoft.com. ..
Tom Dacon <td****@community.nospamwrote:

I wasn't aware of that. Assuming it's VB.NET, that does sound odd.
yes, of course, it is VB.Net. I think they just ran out of time for a C#
integration.

Tom Dacon
Dacon Software Consulting
Jun 27 '08 #12
Tom Dacon wrote:
"Jon Skeet [C# MVP]" <sk***@pobox.comwrote in message
news:MP*********************@msnews.microsoft.com. ..
>Tom Dacon <td****@community.nospamwrote:

I wasn't aware of that. Assuming it's VB.NET, that does sound odd.

yes, of course, it is VB.Net. I think they just ran out of time for a C#
integration.
Then it would most likely have been implemented in C# 2 or C# 3.

--
Göran Andersson
_____
http://www.guffa.com
Jun 27 '08 #13

"Göran Andersson" <gu***@guffa.comwrote in message
news:Oo**************@TK2MSFTNGP05.phx.gbl...
Then it would most likely have been implemented in C# 2 or C# 3.
If they ever do it (which they have not) it will of course be whatever is
the current version of C#. It seems unlikely that they wouldn't eventually
offer it.

But after all, as I tell anyone who will listen, if you're a good
programmer - especially if you have a computer science education - picking
up another language is no big deal, so I don't see why it makes much
difference whether they do or don't. It always baffles me when people
restrict themselves to a single language. It's so limiting. I see these
plaintive posts on the ng's asking people to convert code from one language
to another for them, and I want to say spend three days learning the
language syntax (the libraries are the same) and then do it yourself.

Over my career, I've programmed professionally (actually been paid to
program in) at least a dozen languages. I estimate that over about
thirty-five years of programming I've written well over a million lines of
code in whatever the current flavor of the month was, from assembly language
on up. Nowadays, when I program for my own enjoyment I usually, but not
always, use C# because I was a C and C++ programmer long before VB came
along and get along fine with curlicues and semicolons. But the company I've
been working for for the last few years requires VB.Net so I use it without
complaint. Five years from now, who knows? It could be G# by then.

Tom Dacon
Jun 27 '08 #14
"Tom Dacon" <td****@community.nospamwrote in message
news:e5**************@TK2MSFTNGP03.phx.gbl...
>
"Göran Andersson" <gu***@guffa.comwrote in message
news:Oo**************@TK2MSFTNGP05.phx.gbl...
>Then it would most likely have been implemented in C# 2 or C# 3.

If they ever do it (which they have not) it will of course be whatever is
the current version of C#. It seems unlikely that they wouldn't eventually
offer it.
THIS JUST IN: it looks like C# support will be offered in Integration
Services in SQL Server 2008 (this from the Microsoft SQL Server web site)

Tom Dacon
Dacon Software Consulting
Jun 27 '08 #15
Jon Skeet [C# MVP] wrote:
Tom Dacon <td****@community.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.
It does happen.

But I don't think it happen as much as the other way around.

"Mort" is rather widely known.

Arne
Jun 27 '08 #16
Peter Duniho wrote:
On Fri, 06 Jun 2008 17:37:21 -0700, Arne Vajhøj <ar**@vajhoej.dkwrote:
>>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.
Maybe you should.

(there are also other ways, but asking it always a possibility)
>Those that actually have used it (in VB or Pascal) does not see
that problem as a real problem.

You've surveyed everyone?
You get two guesses.
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.
Almost any feature can be a maintenance issue if abused.
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.
Unless someone that were part of the decisions has spoken about it, then
"curly brace languages does not have WITH" is just as good an
explanation as the "ambiguities".

Arne
Jun 27 '08 #17
Arne Vajhøj <ar**@vajhoej.dkwrote:
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.
It does happen.

But I don't think it happen as much as the other way around.
I think that may have been true before VB.NET, from C/C++ programmers -
but I think most C# programmers understand that C# and VB.NET are
largely equally capable. I think the backlash from some members of the
VB.NET community, accusing C# developers of being elitist, is largely
misplaced and based on old data, as it were.
"Mort" is rather widely known.
Sure, although I think Mort is also largely misunderstood. See
http://blogs.msdn.com/ericlippert/ar.../02/82840.aspx

Mort was not intended to disparage, IMO - just to indicate a particular
set of needs and roles.

--
Jon Skeet - <sk***@pobox.com>
Web site: http://www.pobox.com/~skeet
Blog: http://www.msmvps.com/jon.skeet
C# in Depth: http://csharpindepth.com
Jun 27 '08 #18
"Tom Dacon" <td****@community.nospamwrote in message
news:e5**************@TK2MSFTNGP03.phx.gbl...
>
"Göran Andersson" <gu***@guffa.comwrote in message
news:Oo**************@TK2MSFTNGP05.phx.gbl...
>Then it would most likely have been implemented in C# 2 or C# 3.

If they ever do it (which they have not) it will of course be whatever is
the current version of C#. It seems unlikely that they wouldn't eventually
offer it.

But after all, as I tell anyone who will listen, if you're a good
programmer - especially if you have a computer science education - picking
up another language is no big deal, so I don't see why it makes much
difference whether they do or don't. It always baffles me when people
restrict themselves to a single language. It's so limiting. I see these
plaintive posts on the ng's asking people to convert code from one
language to another for them, and I want to say spend three days learning
the language syntax (the libraries are the same) and then do it yourself.

Over my career, I've programmed professionally (actually been paid to
program in) at least a dozen languages. I estimate that over about
thirty-five years of programming I've written well over a million lines of
code in whatever the current flavor of the month was, from assembly
language on up. Nowadays, when I program for my own enjoyment I usually,
but not always, use C# because I was a C and C++ programmer long before VB
came along and get along fine with curlicues and semicolons. But the
company I've been working for for the last few years requires VB.Net so I
use it without complaint. Five years from now, who knows? It could be G#
by then.

Tom Dacon
Bravo -

I have used multiple languages over the years as well and learning a new
language simply isn't that big of a deal. The one thing I really wish MS
would do is allow mixed language programming in a single project instead of
forcing us to create a DLL for each language that is different from the
projects primary language. There a constructs in VB that are easier to do
than in C# and there are constructs that are easier to do in C# than in VB.
It would be really nice if we could have both .CS and .VB source files in
the same project.

Mike Ober.
Jun 27 '08 #19
Michael D. Ober wrote:
I have used multiple languages over the years as well and learning a new
language simply isn't that big of a deal. The one thing I really wish
MS would do is allow mixed language programming in a single project
instead of forcing us to create a DLL for each language that is
different from the projects primary language. There a constructs in VB
that are easier to do than in C# and there are constructs that are
easier to do in C# than in VB. It would be really nice if we could have
both .CS and .VB source files in the same project.
..NET can build a single assembly from multiple languages.

It is just VS that does not support it.

Arne
Jun 27 '08 #20
"Arne Vajhøj" <ar**@vajhoej.dkwrote in message
news:48***********************@news.sunsite.dk...
Michael D. Ober wrote:
>I have used multiple languages over the years as well and learning a new
language simply isn't that big of a deal. The one thing I really wish MS
would do is allow mixed language programming in a single project instead
of forcing us to create a DLL for each language that is different from
the projects primary language. There a constructs in VB that are easier
to do than in C# and there are constructs that are easier to do in C#
than in VB. It would be really nice if we could have both .CS and .VB
source files in the same project.

.NET can build a single assembly from multiple languages.

It is just VS that does not support it.

Arne

Does anyone know the URL for requesting features in VS? This feature should
also be in VS.

Mike.
Jun 27 '08 #21
Arne Vajhøj wrote:
Michael D. Ober wrote:
>I have used multiple languages over the years as well and learning a
new language simply isn't that big of a deal. The one thing I really
wish MS would do is allow mixed language programming in a single
project instead of forcing us to create a DLL for each language that
is different from the projects primary language. There a constructs
in VB that are easier to do than in C# and there are constructs that
are easier to do in C# than in VB. It would be really nice if we could
have both .CS and .VB source files in the same project.

.NET can build a single assembly from multiple languages.

It is just VS that does not support it.
And in case someone wonder the switches are:
/t:module
/addmodule

Arne
Jun 27 '08 #22

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

Similar topics

15
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...
13
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(){},...
5
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...
9
by: Mr Flibble | last post by:
Is there an equivalent of VB's "with" statement in c# ?
4
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...
8
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...
25
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...
14
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...
2
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
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
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,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
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...

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.