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

  • Thread starter Thread starter Mike N.
  • Start date Start date
M

Mike N.

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(msoControlButton,
before:=5)
With myControl
.BeginGroup = True
.Caption = "Insert Row(s)"
.OnAction = "InsertRows"
.FaceId = 295
End With


Thanks
 
Mike N. said:
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<string> strings = new List<string> { "Hi", "There" };

But no With statement.
 
Peter said:
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
 
Arne Vajhøj said:
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!
 
Bruce said:
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
 
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
 
Tom Dacon said:
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 said:
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
 
Tom said:
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.
 
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
 
Tom Dacon said:
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
 
Jon said:
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
 
Peter said:
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)
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
 
Arne Vajhøj said:
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/archive/2004/03/02/82840.aspx

Mort was not intended to disparage, IMO - just to indicate a particular
set of needs and roles.
 
Michael said:
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
 
Arne said:
.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
 

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