will new version of C# let me do this

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Gary Brewer

    will new version of C# let me do this

    It would be cool if I could use what I evaulate in an if statement useable
    in its code block


    for example


    if(g>3434) { g=rightHandValu e }

    g would not be 3434 if it was bigger than 3434



    Gary Brewer


  • Nicholas Paldino [.NET/C# MVP]

    #2
    Re: will new version of C# let me do this

    Gary,

    This is easily accomplished.

    g = Math.Min(g, 3434);

    Hope this helps.


    --
    - Nicholas Paldino [.NET/C# MVP]
    - nicholas.paldin o@exisconsultin g.com

    "Gary Brewer" <no@spam.for.me .thanks.com> wrote in message
    news:%23YgL1a%2 3ZDHA.1640@TK2M SFTNGP10.phx.gb l...[color=blue]
    > It would be cool if I could use what I evaulate in an if statement useable
    > in its code block
    >
    >
    > for example
    >
    >
    > if(g>3434) { g=rightHandValu e }
    >
    > g would not be 3434 if it was bigger than 3434
    >
    >
    >
    > Gary Brewer
    >
    >[/color]


    Comment

    • Christian

      #3
      Re: will new version of C# let me do this

      Doesn't this work in c#?

      g = ( g>3434 ? 3434 : g );

      Chris.
      "Gary Brewer" <no@spam.for.me .thanks.com> ha scritto nel messaggio
      news:%23YgL1a%2 3ZDHA.1640@TK2M SFTNGP10.phx.gb l...[color=blue]
      > It would be cool if I could use what I evaulate in an if statement useable
      > in its code block
      >
      >
      > for example
      >
      >
      > if(g>3434) { g=rightHandValu e }
      >
      > g would not be 3434 if it was bigger than 3434
      >
      >
      >
      > Gary Brewer
      >
      >[/color]


      Comment

      • Morten Wennevik

        #4
        Re: will new version of C# let me do this

        On Thu, 21 Aug 2003 14:17:30 +0100, Gary Brewer <no@spam.for.me .thanks.com>
        wrote:
        [color=blue]
        > It would be cool if I could use what I evaulate in an if statement
        > useable
        > in its code block
        >
        >
        > for example
        >
        >
        > if(g>3434) { g=rightHandValu e }
        >
        > g would not be 3434 if it was bigger than 3434
        >
        >
        >
        > Gary Brewer
        >
        >
        >[/color]

        g = g > 3434 ? g : 3434;

        Translates to if(g > 3434){g = g} else{g = 3434}

        --
        Using M2, Opera's revolutionary e-mail client: http://www.opera.com/m2/

        Comment

        • Colin Young

          #5
          Re: will new version of C# let me do this

          If I understand correctly, what you are looking for is the introduce
          explaining variable (see
          http://www.refactoring.com/catalog/i...Variable.html). Your
          proposal could make code rather difficult to comprehend or maintain:

          if (g < 123) {
          g = rightHandValue;

          if (h < 456) {
          h = rightHandValue;

          if (i < 789) {
          i = rightHandValue + 456 + 123; // back to using magic numbers
          } } }

          much better (although I still don't know what this code might accomplish):

          int gMin = 123, hMin = 456, iMin = 789;

          if (g < gMin) {
          g = rightHandValue;

          if (h < hMin) {
          h = rightHandValue;

          if (i < iMin) {
          i = gMin + hMin + iMin;
          } } }

          Colin

          "Gary Brewer" <no@spam.for.me .thanks.com> wrote in message
          news:%23YgL1a%2 3ZDHA.1640@TK2M SFTNGP10.phx.gb l...[color=blue]
          > It would be cool if I could use what I evaulate in an if statement useable
          > in its code block
          >
          >
          > for example
          >
          >
          > if(g>3434) { g=rightHandValu e }
          >
          > g would not be 3434 if it was bigger than 3434
          >
          >
          >
          > Gary Brewer
          >
          >[/color]


          Comment

          • Jay B. Harlow [MVP - Outlook]

            #6
            Re: will new version of C# let me do this

            Michael,[color=blue]
            > Now what *I* wish is that I didn't have to write "Math." in front of the
            > names of all the math functions.[/color]
            In VB.NET you can use Class names on the Imports statement (using
            statement).

            Which allows:

            Imports System.Math ' aka "using System.Math;"

            g = Min(g, 3434)

            Granted I use it sparingly, as it weakens encapsulation. However I agree
            with you Math is one place where I find it useful, especially in a class
            full of math functions.

            Jay

            "Michael A. Covington" <Michael@Coving tonInnovations. com> wrote in message
            news:%23NUvou%2 3ZDHA.3768@tk2m sftngp13.phx.gb l...[color=blue]
            >
            > "Nicholas Paldino [.NET/C# MVP]" <nicholas.paldi no@exisconsulti ng.com>[/color]
            wrote[color=blue]
            > in message news:u1BUfi%23Z DHA.1492@TK2MSF TNGP12.phx.gbl. ..[color=green]
            > > Gary,
            > >
            > > This is easily accomplished.
            > >
            > > g = Math.Min(g, 3434);
            > >
            > > Hope this helps.
            > >[/color]
            >
            > Now what *I* wish is that I didn't have to write "Math." in front of the
            > names of all the math functions. Doing so makes a long formula look[/color]
            rather[color=blue]
            > clumsy. Is there a way around that?
            >
            > I guess what I'm wishing is for the "using" keyword to apply to class[/color]
            names[color=blue]
            > as well as namespaces. Or does it?
            >
            >[/color]


            Comment

            • Jon Skeet

              #7
              Re: will new version of C# let me do this

              Rob Tillie <Rob.Tillie@stu dent.tul.edu> wrote:[color=blue]
              > Nope, you cannot do this. This is completly against the rules of OO
              > programming. You would have one big mess then.
              > In Java you can import (using) a specific class, but still you have to
              > quantify it.[/color]

              You do at the moment, but not from the next version of Java. You'll be
              able to use something called "static imports" which will precisely
              allow you to use static method names without qualifying them.

              --
              Jon Skeet - <skeet@pobox.co m>
              Pobox has been discontinued as a separate service, and all existing customers moved to the Fastmail platform.

              If replying to the group, please do not mail me too

              Comment

              • Jay B. Harlow [MVP - Outlook]

                #8
                Re: will new version of C# let me do this

                Jon,
                So do you think C# will gain the ability. VB.NET has it now!

                Its not really an IL issue.

                Like any construct, misuse of it does not make it evil. ;-) As I said
                earlier for System.Math I find it very useful in VB.NET. Otherwise most of
                the time I prefer the 'encapsulation' of needing to prefix with the class
                name.

                Jay

                "Jon Skeet" <skeet@pobox.co m> wrote in message
                news:MPG.19af04 698563d13b98a36 1@news.microsof t.com...[color=blue]
                > Rob Tillie <Rob.Tillie@stu dent.tul.edu> wrote:[color=green]
                > > Nope, you cannot do this. This is completly against the rules of OO
                > > programming. You would have one big mess then.
                > > In Java you can import (using) a specific class, but still you have to
                > > quantify it.[/color]
                >
                > You do at the moment, but not from the next version of Java. You'll be
                > able to use something called "static imports" which will precisely
                > allow you to use static method names without qualifying them.
                >
                > --
                > Jon Skeet - <skeet@pobox.co m>
                > http://www.pobox.com/~skeet/
                > If replying to the group, please do not mail me too[/color]


                Comment

                • Jon Skeet

                  #9
                  Re: will new version of C# let me do this

                  Jay B. Harlow [MVP - Outlook] <Jay_Harlow@ema il.msn.com> wrote:[color=blue]
                  > So do you think C# will gain the ability. VB.NET has it now![/color]

                  Don't know - I suspect that Java having it will have more bearing on
                  whether or not C# gets it than VB.NET having it. My guess is that MS
                  will watch closely what the reaction of Java programmers is to having
                  it, and make a decision based on that. That's certainly what I'd do,
                  anyway :)
                  [color=blue]
                  > Its not really an IL issue.[/color]

                  Of course not
                  [color=blue]
                  > Like any construct, misuse of it does not make it evil. ;-) As I said
                  > earlier for System.Math I find it very useful in VB.NET. Otherwise most of
                  > the time I prefer the 'encapsulation' of needing to prefix with the class
                  > name.[/color]

                  Absolutely. I can't see myself using it much apart from for
                  System.Math, and that's the example which is canonically quoted as
                  well...

                  --
                  Jon Skeet - <skeet@pobox.co m>
                  Pobox has been discontinued as a separate service, and all existing customers moved to the Fastmail platform.

                  If replying to the group, please do not mail me too

                  Comment

                  • Jay B. Harlow [MVP - Outlook]

                    #10
                    Re: will new version of C# let me do this

                    Jon,[color=blue]
                    > Don't know - I suspect that Java having it will have more bearing on
                    > whether or not C# gets it than VB.NET having it. My guess is that MS
                    > will watch closely what the reaction of Java programmers is to having
                    > it, and make a decision based on that. That's certainly what I'd do,
                    > anyway :)[/color]
                    I didn't mean to imply that C# needs it because VB.NET has it. ;-)

                    VB.NET having it was more a demonstration that its not an IL issue. (my line
                    breaks were off)

                    I don't know if I would watch the reaction of Java programmers as much, as
                    try to get a feel from C# programmers if they 'have a need or want' for it.
                    (if I was MS). And possible the OOP community as a whole.
                    [color=blue]
                    > Absolutely. I can't see myself using it much apart from for
                    > System.Math[/color]
                    I started writing a Console Application once, where I imported
                    System.Console. I quickly changed back to Console.WriteLi ne instead of just
                    WriteLine for lack of the encapsulation of where WriteLine came from...

                    Importing System.Console, just didn't feel right ;-)

                    Just a thought
                    Jay

                    "Jon Skeet" <skeet@pobox.co m> wrote in message
                    news:MPG.19af09 38870238e898a36 4@news.microsof t.com...[color=blue]
                    > Jay B. Harlow [MVP - Outlook] <Jay_Harlow@ema il.msn.com> wrote:[color=green]
                    > > So do you think C# will gain the ability. VB.NET has it now![/color]
                    >
                    > Don't know - I suspect that Java having it will have more bearing on
                    > whether or not C# gets it than VB.NET having it. My guess is that MS
                    > will watch closely what the reaction of Java programmers is to having
                    > it, and make a decision based on that. That's certainly what I'd do,
                    > anyway :)
                    >[color=green]
                    > > Its not really an IL issue.[/color]
                    >
                    > Of course not
                    >[color=green]
                    > > Like any construct, misuse of it does not make it evil. ;-) As I said
                    > > earlier for System.Math I find it very useful in VB.NET. Otherwise most[/color][/color]
                    of[color=blue][color=green]
                    > > the time I prefer the 'encapsulation' of needing to prefix with the[/color][/color]
                    class[color=blue][color=green]
                    > > name.[/color]
                    >
                    > Absolutely. I can't see myself using it much apart from for
                    > System.Math, and that's the example which is canonically quoted as
                    > well...
                    >
                    > --
                    > Jon Skeet - <skeet@pobox.co m>
                    > http://www.pobox.com/~skeet/
                    > If replying to the group, please do not mail me too[/color]


                    Comment

                    • Jon Skeet

                      #11
                      Re: will new version of C# let me do this

                      Jay B. Harlow [MVP - Outlook] <Jay_Harlow@ema il.msn.com> wrote:[color=blue]
                      > Jon,[color=green]
                      > > Don't know - I suspect that Java having it will have more bearing on
                      > > whether or not C# gets it than VB.NET having it. My guess is that MS
                      > > will watch closely what the reaction of Java programmers is to having
                      > > it, and make a decision based on that. That's certainly what I'd do,
                      > > anyway :)[/color]
                      > I didn't mean to imply that C# needs it because VB.NET has it. ;-)[/color]

                      Fair enough.
                      [color=blue]
                      > VB.NET having it was more a demonstration that its not an IL issue. (my line
                      > breaks were off)[/color]

                      I think it's fairly obvious that it's not an IL issue, as the source
                      code

                      using ClassName;
                      Whatever();

                      would translate into the same IL as:

                      ClassName.Whate ver();

                      If the compiler can work out what method should be called, it's very
                      easy to call it :)
                      [color=blue]
                      > I don't know if I would watch the reaction of Java programmers as much, as
                      > try to get a feel from C# programmers if they 'have a need or want' for it.
                      > (if I was MS). And possible the OOP community as a whole.[/color]

                      I don't know - I think there are often features which sound great but
                      don't pan out well in the real world. Watching how things are used (and
                      abused) in a very similar language (which Java is) can be a good gauge
                      for this kind of thing.
                      [color=blue][color=green]
                      > > Absolutely. I can't see myself using it much apart from for
                      > > System.Math[/color]
                      > I started writing a Console Application once, where I imported
                      > System.Console. I quickly changed back to Console.WriteLi ne instead of just
                      > WriteLine for lack of the encapsulation of where WriteLine came from...
                      >
                      > Importing System.Console, just didn't feel right ;-)[/color]

                      Hmm... I think it would depend on how much console IO I was doing, and
                      whether or not I was doing *any* other IO. If I wasn't writing to any
                      files/streams/whatever, I think it would be reasonably unambiguous -
                      otherwise, I'd avoid it.

                      As least, that's my guess :)

                      --
                      Jon Skeet - <skeet@pobox.co m>
                      Pobox has been discontinued as a separate service, and all existing customers moved to the Fastmail platform.

                      If replying to the group, please do not mail me too

                      Comment

                      • Peter Vidler

                        #12
                        Re: will new version of C# let me do this

                        > In VB.NET you can use Class names on the Imports statement (using[color=blue]
                        > statement).[/color]

                        Doesn't VB allow you to use the "With" statement for smaller blocks like
                        this?

                        Pete


                        Comment

                        • Michael A. Covington

                          #13
                          Re: will new version of C# let me do this


                          "Rob Tillie" <Rob.Tillie@stu dent.tul.edu> wrote in message
                          news:OPFo6C$ZDH A.1280@tk2msftn gp13.phx.gbl...[color=blue]
                          > Nope, you cannot do this. This is completly against the rules of OO
                          > programming. You would have one big mess then.
                          > In Java you can import (using) a specific class, but still you have to
                          > quantify it.[/color]
                          [color=blue]
                          > If the compiler would allow such a thing...
                          > Now, somebody else would have to go through all imported classes to look
                          > where MyFunction is defined. Even for yourself, this can get messy and
                          > unclear pretty quickly.[/color]

                          But Pascal had the "with" statement for structs (records), and it wasn't a
                          problem.

                          Have you ever coded a long mathematical formula full of trig functions in
                          Java or C#? All that "Math." gets awfully repetitious. Instead of

                          y = Math.sqrt(Math. sin(x) + Math.cos(x) + Math.tan(y));

                          (yes, I know this is a silly formula), I'd like to write:

                          with Math { y = sqrt(sin(x) + cos(x) + tan(y)); }

                          For the mathematically literate, in the Fortran tradition (which is great
                          for numerical mathematics), the second of these is decidedly more readable
                          and less error-prone. Having familiar formulas *look* familiar is
                          important.

                          You could even stipulate that if there are nested withs, you can't use
                          anything that is defined in more than one of them.



                          Comment

                          • Chad Myers

                            #14
                            Re: will new version of C# let me do this


                            "Michael A. Covington" <Michael@Coving tonInnovations. com> wrote in
                            message news:%23LhWKNFa DHA.2632@TK2MSF TNGP09.phx.gbl. ..[color=blue]
                            >
                            > "Rob Tillie" <Rob.Tillie@stu dent.tul.edu> wrote in message
                            > news:OPFo6C$ZDH A.1280@tk2msftn gp13.phx.gbl...[color=green]
                            > > Nope, you cannot do this. This is completly against the rules of OO
                            > > programming. You would have one big mess then.
                            > > In Java you can import (using) a specific class, but still you have[/color][/color]
                            to[color=blue][color=green]
                            > > quantify it.[/color]
                            >[color=green]
                            > > If the compiler would allow such a thing...
                            > > Now, somebody else would have to go through all imported classes to[/color][/color]
                            look[color=blue][color=green]
                            > > where MyFunction is defined. Even for yourself, this can get messy[/color][/color]
                            and[color=blue][color=green]
                            > > unclear pretty quickly.[/color]
                            >
                            > But Pascal had the "with" statement for structs (records), and it[/color]
                            wasn't a[color=blue]
                            > problem.
                            >
                            > Have you ever coded a long mathematical formula full of trig functions[/color]
                            in[color=blue]
                            > Java or C#? All that "Math." gets awfully repetitious. Instead of
                            >
                            > y = Math.sqrt(Math. sin(x) + Math.cos(x) + Math.tan(y));
                            >
                            > (yes, I know this is a silly formula), I'd like to write:
                            >
                            > with Math { y = sqrt(sin(x) + cos(x) + tan(y)); }
                            >
                            > For the mathematically literate, in the Fortran tradition (which is[/color]
                            great[color=blue]
                            > for numerical mathematics), the second of these is decidedly more[/color]
                            readable[color=blue]
                            > and less error-prone. Having familiar formulas *look* familiar is
                            > important.
                            >
                            > You could even stipulate that if there are nested withs, you can't use
                            > anything that is defined in more than one of them.[/color]

                            There are patterns you could use to get around this.

                            Derive from a MathHelper class which has all those methods
                            as protected methods, for example.

                            -c


                            Comment

                            • Jay B. Harlow [MVP - Outlook]

                              #15
                              Re: will new version of C# let me do this

                              Peter,
                              The 'With' statement does not allow Class names. The With statement requires
                              an instance of an object (a variable, not a type).

                              (as he runs off and double checks) ;-)

                              Yep, you get a compile error with a 'Type'

                              'Math' is a type and cannot be used as an expression.

                              I can see where a localized 'import/with' would be useful...

                              Hope this helps
                              Jay

                              "Peter Vidler" <pvidler@gawab. com> wrote in message
                              news:jx91b.484$ Dg4.183338@news fep2-gui.server.ntli .net...[color=blue][color=green]
                              > > In VB.NET you can use Class names on the Imports statement (using
                              > > statement).[/color]
                              >
                              > Doesn't VB allow you to use the "With" statement for smaller blocks like
                              > this?
                              >
                              > Pete
                              >
                              >[/color]


                              Comment

                              Working...