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

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Mike N.

    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
  • Jon Skeet [C# MVP]

    #2
    Re: C# equivelent of VBA "with.. end with" statement

    Mike N. <michael.niblet t@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 - <skeet@pobox.co m>
    Web site: http://www.pobox.com/~skeet
    Blog: http://www.msmvps.com/jon.skeet
    C# in Depth: http://csharpindepth.com

    Comment

    • Peter Duniho

      #3
      Re: C# equivelent of VBA &quot;with.. end with&quot; statement

      On Fri, 06 Jun 2008 13:16:34 -0700, Mike N. <michael.niblet t@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

      Comment

      • =?UTF-8?B?QXJuZSBWYWpow7hq?=

        #4
        Re: C# equivelent of VBA &quot;with.. end with&quot; statement

        Peter Duniho wrote:
        On Fri, 06 Jun 2008 13:16:34 -0700, Mike N. <michael.niblet t@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


        Comment

        • Bruce C. Baker

          #5
          Re: C# equivelent of VBA &quot;with.. end with&quot; statement


          "Arne Vajhøj" <arne@vajhoej.d kwrote in message
          news:4849d841$0 $90275$14726298 @news.sunsite.d k...
          Peter Duniho wrote:
          >On Fri, 06 Jun 2008 13:16:34 -0700, Mike N. <michael.niblet t@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!


          Comment

          • =?ISO-8859-1?Q?Arne_Vajh=F8j?=

            #6
            Re: C# equivelent of VBA &quot;with.. end with&quot; statement

            Bruce C. Baker wrote:
            "Arne Vajhøj" <arne@vajhoej.d kwrote in message
            news:4849d841$0 $90275$14726298 @news.sunsite.d k...
            >Peter Duniho wrote:
            >>On Fri, 06 Jun 2008 13:16:34 -0700, Mike N. <michael.niblet t@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

            Comment

            • Tom Dacon

              #7
              Re: C# equivelent of VBA &quot;with.. end with&quot; statement

              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" <arne@vajhoej.d kwrote in message
              news:4849d841$0 $90275$14726298 @news.sunsite.d k...
              Peter Duniho wrote:
              >On Fri, 06 Jun 2008 13:16:34 -0700, Mike N. <michael.niblet t@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
              >
              >

              Comment

              • Peter Duniho

                #8
                Re: C# equivelent of VBA &quot;with.. end with&quot; statement

                On Fri, 06 Jun 2008 17:37:21 -0700, Arne Vajhøj <arne@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

                Comment

                • Michael D. Ober

                  #9
                  Re: C# equivelent of VBA &quot;with.. end with&quot; statement

                  "Peter Duniho" <NpOeStPeAdM@nn owslpianmk.comw rote in message
                  news:op.ucc4jwk b8jd0ej@petes-computer.local. ..
                  On Fri, 06 Jun 2008 17:37:21 -0700, Arne Vajhøj <arne@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.


                  Comment

                  • Peter Duniho

                    #10
                    Re: C# equivelent of VBA &quot;with.. end with&quot; statement

                    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

                    Comment

                    • Jon Skeet [C# MVP]

                      #11
                      Re: C# equivelent of VBA &quot;with.. end with&quot; statement

                      Tom Dacon <tdacon@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 - <skeet@pobox.co m>
                      Web site: http://www.pobox.com/~skeet
                      Blog: http://www.msmvps.com/jon.skeet
                      C# in Depth: http://csharpindepth.com

                      Comment

                      • Tom Dacon

                        #12
                        Re: C# equivelent of VBA &quot;with.. end with&quot; statement


                        "Jon Skeet [C# MVP]" <skeet@pobox.co mwrote in message
                        news:MPG.22b437 2acea8bc20d53@m snews.microsoft .com...
                        Tom Dacon <tdacon@communi ty.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


                        Comment

                        • =?ISO-8859-1?Q?G=F6ran_Andersson?=

                          #13
                          Re: C# equivelent of VBA &quot;with.. end with&quot; statement

                          Tom Dacon wrote:
                          "Jon Skeet [C# MVP]" <skeet@pobox.co mwrote in message
                          news:MPG.22b437 2acea8bc20d53@m snews.microsoft .com...
                          >Tom Dacon <tdacon@communi ty.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
                          _____
                          Göran Anderssons privata hemsida.

                          Comment

                          • Tom Dacon

                            #14
                            Re: C# equivelent of VBA &quot;with.. end with&quot; statement


                            "Göran Andersson" <guffa@guffa.co mwrote in message
                            news:OoZpQNMyIH A.2360@TK2MSFTN GP05.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


                            Comment

                            • Tom Dacon

                              #15
                              Re: C# equivelent of VBA &quot;with.. end with&quot; statement

                              "Tom Dacon" <tdacon@communi ty.nospamwrote in message
                              news:e5f6q5MyIH A.4816@TK2MSFTN GP03.phx.gbl...
                              >
                              "Göran Andersson" <guffa@guffa.co mwrote in message
                              news:OoZpQNMyIH A.2360@TK2MSFTN GP05.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


                              Comment

                              Working...