Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. C#
  4. Does Anybody Else Miss the WITH construct of Visual Basic?

Does Anybody Else Miss the WITH construct of Visual Basic?

Scheduled Pinned Locked Moved C#
csharpquestionhelptutorial
34 Posts 10 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • OriginalGriffO OriginalGriff

    I can see what they are trying to do - scope the variable to just the code where the result is actually valid:

    {
    int result;
    if (int.TryParse(input, out result))
    Console.WriteLine(result);
    else
    Console.WriteLine("Could not parse input");
    }

    Only it's out of scope in the else block. And that does make some good sense. But it is ugly - I'll get used to it and it should improve reliability in the long term.

    Sent from my Amstrad PC 1640 Never throw anything away, Griff Bad command or file name. Bad, bad command! Sit! Stay! Staaaay... AntiTwitter: @DalekDave is now a follower!

    D Offline
    D Offline
    Dave Kreskowiak
    wrote on last edited by
    #11

    The scope of result isn't just the true side of the if statement. It's everywhere in the parent scope after the if statement, except for the else block. Get some noob writing a 500 line method and you run into that same problem where you're hunting for where the variable is declared.

    Asking questions is a skill CodeProject Forum Guidelines Google: C# How to debug code Seriously, go read these articles.
    Dave Kreskowiak

    1 Reply Last reply
    0
    • D Dave Kreskowiak

      Speaking of hunting back through the code, C# 7 gives us this little gem:

      if (int.TryParse(input, out int result))
      Console.WriteLine(result);
      else
      Console.WriteLine("Could not parse input");

      You get to declare variables in the call to TryParse, or any other method that takes out parameters. I no likey. I would class this as one of C#'s little "you can't handle the truth" constructs. The docs says it makes the code easier to read. I respectfully disagree.

      Asking questions is a skill CodeProject Forum Guidelines Google: C# How to debug code Seriously, go read these articles.
      Dave Kreskowiak

      B Offline
      B Offline
      BillWoodruff
      wrote on last edited by
      #12

      Hi Dave, I, also, find the in-line 'out variable declaration ... and its persistence beyond what my habitual perceptions suggest should be limited scope ... jarring. I think it needs a little more spice to be really useful in creating FUD:

      Console.WriteLine($"result: {(int.TryParse(input, out var result) ? result.ToString() : "no")}");

      The fact that the in-line 'result variable will persist even in this example ... don't feel right. And, use of 'var in this case seems, intuitively, wrong. But, perhaps these little frissons of cognitive dissonance are just evidence for the old conundrum of 'old dogs cannot learn new tricks' ?

      «Where is the Life we have lost in living? Where is the wisdom we have lost in knowledge? Where is the knowledge we have lost in information?» T. S. Elliot

      D 1 Reply Last reply
      0
      • D David A Gray

        As I read [Adam Storr - Playing with C# 7 - Deconstruct](https://adamstorr.azurewebsites.net/blog/playing-with-csharp-7-deconstruct?utm\_source=Main&utm\_campaign=0cdc8b3a40-EMAIL\_CAMPAIGN\_2017\_12\_19\_COPY\_01&utm\_medium=email&utm\_term=0\_aa2f642d94-0cdc8b3a40-227561569&mc\_cid=0cdc8b3a40&mc\_eid=8087c9508d), I kept hoping that it would answer at long last the question of how to do with in C#. Alas, it fell short again. Does anybody besides me wish that C# supported with blocks, or have I overlooked a seriously underused language feature?

        David A. Gray Delivering Solutions for the Ages, One Problem at a Time Interpreting the Fundamental Principle of Tabular Reporting

        L Offline
        L Offline
        Lost User
        wrote on last edited by
        #13

        Used it mostly in Visual FoxPro; and in VB. Less code; clearer outlines; more generic possibilities. Yep; totally useless.

        "(I) am amazed to see myself here rather than there ... now rather than then". ― Blaise Pascal

        1 Reply Last reply
        0
        • D David A Gray

          As I read [Adam Storr - Playing with C# 7 - Deconstruct](https://adamstorr.azurewebsites.net/blog/playing-with-csharp-7-deconstruct?utm\_source=Main&utm\_campaign=0cdc8b3a40-EMAIL\_CAMPAIGN\_2017\_12\_19\_COPY\_01&utm\_medium=email&utm\_term=0\_aa2f642d94-0cdc8b3a40-227561569&mc\_cid=0cdc8b3a40&mc\_eid=8087c9508d), I kept hoping that it would answer at long last the question of how to do with in C#. Alas, it fell short again. Does anybody besides me wish that C# supported with blocks, or have I overlooked a seriously underused language feature?

          David A. Gray Delivering Solutions for the Ages, One Problem at a Time Interpreting the Fundamental Principle of Tabular Reporting

          B Offline
          B Offline
          BillWoodruff
          wrote on last edited by
          #14

          You really want 'With in C#: [^]. This hack uses reflection ... I'd never use it in production code: [^] And, now, the "sermon:" I think it's a mistake to try and "bend" C# to fit your background in another language. At the same time, imho, most people will, in the first phase of learning, look for, expect, things/structures through mental habits conditioned by other languages. I believe (hypothesis) that programmers often have a kind of imprinting/bonding experience with the first language they learn well. Surrender ! :wtf:

          «Where is the Life we have lost in living? Where is the wisdom we have lost in knowledge? Where is the knowledge we have lost in information?» T. S. Elliot

          D OriginalGriffO 2 Replies Last reply
          0
          • B BillWoodruff

            Hi Dave, I, also, find the in-line 'out variable declaration ... and its persistence beyond what my habitual perceptions suggest should be limited scope ... jarring. I think it needs a little more spice to be really useful in creating FUD:

            Console.WriteLine($"result: {(int.TryParse(input, out var result) ? result.ToString() : "no")}");

            The fact that the in-line 'result variable will persist even in this example ... don't feel right. And, use of 'var in this case seems, intuitively, wrong. But, perhaps these little frissons of cognitive dissonance are just evidence for the old conundrum of 'old dogs cannot learn new tricks' ?

            «Where is the Life we have lost in living? Where is the wisdom we have lost in knowledge? Where is the knowledge we have lost in information?» T. S. Elliot

            D Offline
            D Offline
            Dave Kreskowiak
            wrote on last edited by
            #15

            That code is ..... disturbing. I think about half the new features showing up in C# now are just for syntactic sugar. This is one where I think the community of old-timers would be split on its usefulness vs how many people would actually use it. I'd be interested in seeing any telemetry on its use.

            Asking questions is a skill CodeProject Forum Guidelines Google: C# How to debug code Seriously, go read these articles.
            Dave Kreskowiak

            1 Reply Last reply
            0
            • B BillWoodruff

              You really want 'With in C#: [^]. This hack uses reflection ... I'd never use it in production code: [^] And, now, the "sermon:" I think it's a mistake to try and "bend" C# to fit your background in another language. At the same time, imho, most people will, in the first phase of learning, look for, expect, things/structures through mental habits conditioned by other languages. I believe (hypothesis) that programmers often have a kind of imprinting/bonding experience with the first language they learn well. Surrender ! :wtf:

              «Where is the Life we have lost in living? Where is the wisdom we have lost in knowledge? Where is the knowledge we have lost in information?» T. S. Elliot

              D Offline
              D Offline
              David A Gray
              wrote on last edited by
              #16

              Did you see my terse example? Do you like typing the class name in front of every member name in a code block that sets ten properties one after another?

              David A. Gray Delivering Solutions for the Ages, One Problem at a Time Interpreting the Fundamental Principle of Tabular Reporting

              B 1 Reply Last reply
              0
              • D David A Gray

                Did you see my terse example? Do you like typing the class name in front of every member name in a code block that sets ten properties one after another?

                David A. Gray Delivering Solutions for the Ages, One Problem at a Time Interpreting the Fundamental Principle of Tabular Reporting

                B Offline
                B Offline
                BillWoodruff
                wrote on last edited by
                #17

                Did I see the code sample that would never compile [^]: yes. Did I see a reference in another post you made to something "illustrated" without explanation: yes.

                David A. Gray wrote:

                Do you like typing the class name in front of every member name in a code block that sets ten properties one after another?

                I do not see how this "sucker-punch question" is relevant to anything discussed here :) Beginning with the object initializer syntax in C# (C# 3.0, .NET 3.5), initializing a bunch of whatever when a new object was created became much easier. What's your issue here ? If you want to simulate 'With, techniques are well known, for a long time (see my post here). Now, if you have a better way, that doesn't use reflection, or the usual Extension Method: I'm all ears !

                «Where is the Life we have lost in living? Where is the wisdom we have lost in knowledge? Where is the knowledge we have lost in information?» T. S. Elliot

                D 1 Reply Last reply
                0
                • D David A Gray

                  As I read [Adam Storr - Playing with C# 7 - Deconstruct](https://adamstorr.azurewebsites.net/blog/playing-with-csharp-7-deconstruct?utm\_source=Main&utm\_campaign=0cdc8b3a40-EMAIL\_CAMPAIGN\_2017\_12\_19\_COPY\_01&utm\_medium=email&utm\_term=0\_aa2f642d94-0cdc8b3a40-227561569&mc\_cid=0cdc8b3a40&mc\_eid=8087c9508d), I kept hoping that it would answer at long last the question of how to do with in C#. Alas, it fell short again. Does anybody besides me wish that C# supported with blocks, or have I overlooked a seriously underused language feature?

                  David A. Gray Delivering Solutions for the Ages, One Problem at a Time Interpreting the Fundamental Principle of Tabular Reporting

                  J Offline
                  J Offline
                  Jorgen Andersson
                  wrote on last edited by
                  #18

                  I'm still doing some programming in VB.Net. I never use With.

                  Wrong is evil and must be defeated. - Jeff Ello

                  1 Reply Last reply
                  0
                  • B BillWoodruff

                    You really want 'With in C#: [^]. This hack uses reflection ... I'd never use it in production code: [^] And, now, the "sermon:" I think it's a mistake to try and "bend" C# to fit your background in another language. At the same time, imho, most people will, in the first phase of learning, look for, expect, things/structures through mental habits conditioned by other languages. I believe (hypothesis) that programmers often have a kind of imprinting/bonding experience with the first language they learn well. Surrender ! :wtf:

                    «Where is the Life we have lost in living? Where is the wisdom we have lost in knowledge? Where is the knowledge we have lost in information?» T. S. Elliot

                    OriginalGriffO Offline
                    OriginalGriffO Offline
                    OriginalGriff
                    wrote on last edited by
                    #19

                    BillWoodruff wrote:

                    I think it's a mistake to try and "bend" C# to fit your background in anothr language.

                    I'd agree - and suspect that's what a lot of the recent C# changes have been driven by: VB and C++ developers which want to continue writing VB and C++ code in C# instead of learning a newer, fresher language paradigm. And maybe that's why C# is getting "bloated" and losing it's focus as a coherent language.

                    Sent from my Amstrad PC 1640 Never throw anything away, Griff Bad command or file name. Bad, bad command! Sit! Stay! Staaaay... AntiTwitter: @DalekDave is now a follower!

                    "I have no idea what I did, but I'm taking full credit for it." - ThisOldTony
                    "Common sense is so rare these days, it should be classified as a super power" - Random T-shirt

                    realJSOPR 1 Reply Last reply
                    0
                    • D David A Gray

                      As I read [Adam Storr - Playing with C# 7 - Deconstruct](https://adamstorr.azurewebsites.net/blog/playing-with-csharp-7-deconstruct?utm\_source=Main&utm\_campaign=0cdc8b3a40-EMAIL\_CAMPAIGN\_2017\_12\_19\_COPY\_01&utm\_medium=email&utm\_term=0\_aa2f642d94-0cdc8b3a40-227561569&mc\_cid=0cdc8b3a40&mc\_eid=8087c9508d), I kept hoping that it would answer at long last the question of how to do with in C#. Alas, it fell short again. Does anybody besides me wish that C# supported with blocks, or have I overlooked a seriously underused language feature?

                      David A. Gray Delivering Solutions for the Ages, One Problem at a Time Interpreting the Fundamental Principle of Tabular Reporting

                      L Offline
                      L Offline
                      Lost User
                      wrote on last edited by
                      #20

                      Just assign whatever you'd usually with with to a variable with a short name.

                      Bastard Programmer from Hell :suss: If you can't read my code, try converting it here[^] "If you just follow the bacon Eddy, wherever it leads you, then you won't have to think about politics." -- Some Bell.

                      1 Reply Last reply
                      0
                      • D David A Gray

                        As I read [Adam Storr - Playing with C# 7 - Deconstruct](https://adamstorr.azurewebsites.net/blog/playing-with-csharp-7-deconstruct?utm\_source=Main&utm\_campaign=0cdc8b3a40-EMAIL\_CAMPAIGN\_2017\_12\_19\_COPY\_01&utm\_medium=email&utm\_term=0\_aa2f642d94-0cdc8b3a40-227561569&mc\_cid=0cdc8b3a40&mc\_eid=8087c9508d), I kept hoping that it would answer at long last the question of how to do with in C#. Alas, it fell short again. Does anybody besides me wish that C# supported with blocks, or have I overlooked a seriously underused language feature?

                        David A. Gray Delivering Solutions for the Ages, One Problem at a Time Interpreting the Fundamental Principle of Tabular Reporting

                        J Offline
                        J Offline
                        jsc42
                        wrote on last edited by
                        #21

                        Many years ago, I wrote an expression evaluator for a Pascal compiler. "With" provided extra levels of complexity, which made me dislike it. "With" in VB leads to ambiguities when nested as you can have multiple sets of 'withed' variables in the same inner block. 'with' (now deprecated) in JavaScript lead to ambiguities where you could loop the 'with' and variables in the same construct could be global in one pass and local in subsequent passes. Used carefully, "With" can be a convenient shortcut; but it can be used badly. If you give people knives, they may whittle beautiful sculptures, but they are more likely to injure themselves or someone else.

                        L D 2 Replies Last reply
                        0
                        • D David A Gray

                          I don't miss Basic, either, but I miss that construct, which came to my attention when it became part of VBA as it manifested in Microsoft Access 2.0.

                          David A. Gray Delivering Solutions for the Ages, One Problem at a Time Interpreting the Fundamental Principle of Tabular Reporting

                          realJSOPR Offline
                          realJSOPR Offline
                          realJSOP
                          wrote on last edited by
                          #22

                          Real men don't code in VB, or use any of its bastard constructs, such as with or goto.

                          ".45 ACP - because shooting twice is just silly" - JSOP, 2010
                          -----
                          You can never have too much ammo - unless you're swimming, or on fire. - JSOP, 2010
                          -----
                          When you pry the gun from my cold dead hands, be careful - the barrel will be very hot. - JSOP, 2013

                          1 Reply Last reply
                          0
                          • OriginalGriffO OriginalGriff

                            BillWoodruff wrote:

                            I think it's a mistake to try and "bend" C# to fit your background in anothr language.

                            I'd agree - and suspect that's what a lot of the recent C# changes have been driven by: VB and C++ developers which want to continue writing VB and C++ code in C# instead of learning a newer, fresher language paradigm. And maybe that's why C# is getting "bloated" and losing it's focus as a coherent language.

                            Sent from my Amstrad PC 1640 Never throw anything away, Griff Bad command or file name. Bad, bad command! Sit! Stay! Staaaay... AntiTwitter: @DalekDave is now a follower!

                            realJSOPR Offline
                            realJSOPR Offline
                            realJSOP
                            wrote on last edited by
                            #23

                            OriginalGriff wrote:

                            VB and C++ developers which want to continue writing VB and C++ code in C# instead of learning a newer, fresher language paradigm

                            Not to mention it will rightly piss off the other members of your team when they run across your code in the project.

                            ".45 ACP - because shooting twice is just silly" - JSOP, 2010
                            -----
                            You can never have too much ammo - unless you're swimming, or on fire. - JSOP, 2010
                            -----
                            When you pry the gun from my cold dead hands, be careful - the barrel will be very hot. - JSOP, 2013

                            1 Reply Last reply
                            0
                            • D David A Gray

                              Surely, you jest.

                              with Foo {
                              .Bar = 123;
                              .Baz = @"Zap!";
                              .Goo = 3.14159;
                              .Ergo = 1.1414141414l4;
                              }

                              David A. Gray Delivering Solutions for the Ages, One Problem at a Time Interpreting the Fundamental Principle of Tabular Reporting

                              P Offline
                              P Offline
                              phil o
                              wrote on last edited by
                              #24

                              Foo.Bar = 123;
                              Foo.Baz = @"Zap!";
                              Foo.Goo = 3.14159;
                              Foo.Ergo = 1.1414141414l4;

                              is two lines shorter :)

                              noop()

                              D 1 Reply Last reply
                              0
                              • J jsc42

                                Many years ago, I wrote an expression evaluator for a Pascal compiler. "With" provided extra levels of complexity, which made me dislike it. "With" in VB leads to ambiguities when nested as you can have multiple sets of 'withed' variables in the same inner block. 'with' (now deprecated) in JavaScript lead to ambiguities where you could loop the 'with' and variables in the same construct could be global in one pass and local in subsequent passes. Used carefully, "With" can be a convenient shortcut; but it can be used badly. If you give people knives, they may whittle beautiful sculptures, but they are more likely to injure themselves or someone else.

                                L Offline
                                L Offline
                                Lost User
                                wrote on last edited by
                                #25

                                I got the same speak a few years ago when I would introduce LINQ into an answer because I was not catering to the lowest common denominator. Those same people now have no issues with it. (LINQ)

                                "(I) am amazed to see myself here rather than there ... now rather than then". ― Blaise Pascal

                                1 Reply Last reply
                                0
                                • P phil o

                                  Foo.Bar = 123;
                                  Foo.Baz = @"Zap!";
                                  Foo.Goo = 3.14159;
                                  Foo.Ergo = 1.1414141414l4;

                                  is two lines shorter :)

                                  noop()

                                  D Offline
                                  D Offline
                                  David A Gray
                                  wrote on last edited by
                                  #26

                                  So? How many more characters, each requiring a keystroke, does your proposal require?

                                  David A. Gray Delivering Solutions for the Ages, One Problem at a Time Interpreting the Fundamental Principle of Tabular Reporting

                                  P 1 Reply Last reply
                                  0
                                  • J jsc42

                                    Many years ago, I wrote an expression evaluator for a Pascal compiler. "With" provided extra levels of complexity, which made me dislike it. "With" in VB leads to ambiguities when nested as you can have multiple sets of 'withed' variables in the same inner block. 'with' (now deprecated) in JavaScript lead to ambiguities where you could loop the 'with' and variables in the same construct could be global in one pass and local in subsequent passes. Used carefully, "With" can be a convenient shortcut; but it can be used badly. If you give people knives, they may whittle beautiful sculptures, but they are more likely to injure themselves or someone else.

                                    D Offline
                                    D Offline
                                    David A Gray
                                    wrote on last edited by
                                    #27

                                    Quote:

                                    Used carefully, "With" can be a convenient shortcut; but it can be used badly. If you give people knives, they may whittle beautiful sculptures, but they are more likely to injure themselves or someone else.

                                    The same can be said of almost every construct in any programming language. I'll take my chances. Using programming languages requires wisdom, a commodity that is, unfortunately, in very short supply.

                                    David A. Gray Delivering Solutions for the Ages, One Problem at a Time Interpreting the Fundamental Principle of Tabular Reporting

                                    B 1 Reply Last reply
                                    0
                                    • B BillWoodruff

                                      Did I see the code sample that would never compile [^]: yes. Did I see a reference in another post you made to something "illustrated" without explanation: yes.

                                      David A. Gray wrote:

                                      Do you like typing the class name in front of every member name in a code block that sets ten properties one after another?

                                      I do not see how this "sucker-punch question" is relevant to anything discussed here :) Beginning with the object initializer syntax in C# (C# 3.0, .NET 3.5), initializing a bunch of whatever when a new object was created became much easier. What's your issue here ? If you want to simulate 'With, techniques are well known, for a long time (see my post here). Now, if you have a better way, that doesn't use reflection, or the usual Extension Method: I'm all ears !

                                      «Where is the Life we have lost in living? Where is the wisdom we have lost in knowledge? Where is the knowledge we have lost in information?» T. S. Elliot

                                      D Offline
                                      D Offline
                                      David A Gray
                                      wrote on last edited by
                                      #28

                                      It was a random thought, intended to generate the sort of discussion that has ensued. As a practical matter, I use object initializers, overloaded constructors, and constructors that have optional arguments almost exclusively. Unfortunately, this isn't always a workable option; thankfully, those times are rare.

                                      David A. Gray Delivering Solutions for the Ages, One Problem at a Time Interpreting the Fundamental Principle of Tabular Reporting

                                      B 2 Replies Last reply
                                      0
                                      • D David A Gray

                                        It was a random thought, intended to generate the sort of discussion that has ensued. As a practical matter, I use object initializers, overloaded constructors, and constructors that have optional arguments almost exclusively. Unfortunately, this isn't always a workable option; thankfully, those times are rare.

                                        David A. Gray Delivering Solutions for the Ages, One Problem at a Time Interpreting the Fundamental Principle of Tabular Reporting

                                        B Offline
                                        B Offline
                                        BillWoodruff
                                        wrote on last edited by
                                        #29

                                        David A. Gray wrote:

                                        It was a random thought, intended to generate the sort of discussion that has ensued.

                                        One of the best excuses I've seen on this forum, but, an excuse for what ? But, I'm so happy you have random thoughts that you realize (later?) come from a place of deep wisdom and are, obviously, evidence of your sublime intelligence generously helping mere mortals to say what needs to be said :wtf:

                                        «Where is the Life we have lost in living? Where is the wisdom we have lost in knowledge? Where is the knowledge we have lost in information?» T. S. Elliot

                                        1 Reply Last reply
                                        0
                                        • D David A Gray

                                          It was a random thought, intended to generate the sort of discussion that has ensued. As a practical matter, I use object initializers, overloaded constructors, and constructors that have optional arguments almost exclusively. Unfortunately, this isn't always a workable option; thankfully, those times are rare.

                                          David A. Gray Delivering Solutions for the Ages, One Problem at a Time Interpreting the Fundamental Principle of Tabular Reporting

                                          B Offline
                                          B Offline
                                          BillWoodruff
                                          wrote on last edited by
                                          #30

                                          Well, it did generate discussion :) And, I wouldn't mind having a 'With, or a special flavor of 'Using. cheers, Bill

                                          «Where is the Life we have lost in living? Where is the wisdom we have lost in knowledge? Where is the knowledge we have lost in information?» T. S. Elliot

                                          D 1 Reply Last reply
                                          0
                                          Reply
                                          • Reply as topic
                                          Log in to reply
                                          • Oldest to Newest
                                          • Newest to Oldest
                                          • Most Votes


                                          • Login

                                          • Don't have an account? Register

                                          • Login or register to search.
                                          • First post
                                            Last post
                                          0
                                          • Categories
                                          • Recent
                                          • Tags
                                          • Popular
                                          • World
                                          • Users
                                          • Groups