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. The Lounge
  3. Small c# challenge [modified]

Small c# challenge [modified]

Scheduled Pinned Locked Moved The Lounge
csharpdatabasecomtoolsquestion
34 Posts 13 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.
  • C cpkilekofp

    Marc Clifton wrote:

    And yeah, you can really get rid of the if this way: object Foo;...temp.Foo=rd[0];

    That's what I would have thought from the C# 2.0 spec. But I have to ask: have you tried it, and observed it to work?

    M Offline
    M Offline
    Marc Clifton
    wrote on last edited by
    #25

    cpkilekofp wrote:

    But I have to ask: have you tried it, and observed it to work?

    It works because all the fields are objects, unless you use a typed data reader. Marc

    Thyme In The Country Interacx My Blog

    1 Reply Last reply
    0
    • G Giorgi Dalakishvili

      rd[0] returns object, not double.

      Giorgi Dalakishvili #region signature My Articles / My Latest Article[^] / My blog[^] #endregion

      M Offline
      M Offline
      Marc Clifton
      wrote on last edited by
      #26

      Giorgi Dalakishvili wrote:

      rd[0] returns object, not double.

      Yes, I know. What did you mean? I was being flippant, because you obviously have to deal with converting the object to a double at some point. But it was a way to get rid of the "if". Marc

      Thyme In The Country Interacx My Blog

      G E 2 Replies Last reply
      0
      • M Marc Clifton

        Giorgi Dalakishvili wrote:

        rd[0] returns object, not double.

        Yes, I know. What did you mean? I was being flippant, because you obviously have to deal with converting the object to a double at some point. But it was a way to get rid of the "if". Marc

        Thyme In The Country Interacx My Blog

        G Offline
        G Offline
        Giorgi Dalakishvili
        wrote on last edited by
        #27

        Sorry I didn't notice changes you have made :)

        Giorgi Dalakishvili #region signature My Articles / My Latest Article[^] / My blog[^] #endregion

        1 Reply Last reply
        0
        • M Marc Clifton

          What BS. A tertiary operator is just like an if. [edit] And the fact that nullable types didn't resolve the mismatch between null and DBNull pisses me off to no end.[/edit] [edit2] And yeah, you can really get rid of the if this way:

          object Foo;
          ...
          temp.Foo=rd[0];

          :laugh: Marc

          Thyme In The Country Interacx My Blog

          L Offline
          L Offline
          leppie
          wrote on last edited by
          #28

          Yip, I call BS too. :)

          xacc.ide - now with TabsToSpaces support
          IronScheme - 1.0 beta 1 - out now!
          ((lambda (x) `((lambda (x) ,x) ',x)) '`((lambda (x) ,x) ',x))

          1 Reply Last reply
          0
          • M Marc Clifton

            Giorgi Dalakishvili wrote:

            rd[0] returns object, not double.

            Yes, I know. What did you mean? I was being flippant, because you obviously have to deal with converting the object to a double at some point. But it was a way to get rid of the "if". Marc

            Thyme In The Country Interacx My Blog

            E Offline
            E Offline
            Ennis Ray Lynch Jr
            wrote on last edited by
            #29

            I knew that flippant was a word. Yet for some reason I always had lingering self-doubt when using it.

            Need software developed? Offering C# development all over the United States, ERL GLOBAL, Inc is the only call you will have to make.
            Happiness in intelligent people is the rarest thing I know. -- Ernest Hemingway
            Most of this sig is for Google, not ego.

            B 1 Reply Last reply
            0
            • E Ennis Ray Lynch Jr

              temp.Foo = rd[0] == DbNull.Value ? null : (double)rd[0]

              Need software developed? Offering C# development all over the United States, ERL GLOBAL, Inc is the only call you will have to make.
              Happiness in intelligent people is the rarest thing I know. -- Ernest Hemingway
              Most of this sig is for Google, not ego.

              R Offline
              R Offline
              Robert Royall
              wrote on last edited by
              #30

              It would be nice if this worked in VB.NET... Unfortunately the VB ternary operator (IIF) has a nasty habit of evaluating both the true and false statements instead of short-circuiting directly to one option or the other like a logical language does.

              Imagine that you are hired to build a bridge over a river which gets slightly wider every day; sometimes it shrinks but nobody can predict when. Your client provides no concrete or steel, only timber and cut stone (but they won't tell you what kind). The coefficient of gravity changes randomly from hour to hour, as does the viscosity of air. Your only tools are a hacksaw, a chainsaw, a rubber mallet, and a length of rope. Welcome to my world. -Me explaining my job to an engineer

              1 Reply Last reply
              0
              • G Giorgi Dalakishvili

                Let's say you have this class:

                class myClass
                {
                public double? Foo;
                }

                and this piece of code:

                myClass temp=new myClass()
                SqlDataReader rd = GetDataSomeHow();

                if(rd.IsDBNull(0))
                temp.Foo = null;
                else
                temp.Foo = rd.GetDouble(0);

                Rewrite the above statement without if. [Edit] One line solution Sorry if it's too easy for you :) Here is the solution in case you can't solve it: Solution[^]

                Giorgi Dalakishvili #region signature My Articles / My Latest Article[^] / My blog[^] #endregion

                modified on Thursday, October 23, 2008 11:23 AM

                T Offline
                T Offline
                Todd Smith
                wrote on last edited by
                #31

                Somehow is one word.

                Todd Smith

                1 Reply Last reply
                0
                • E Ennis Ray Lynch Jr

                  I knew that flippant was a word. Yet for some reason I always had lingering self-doubt when using it.

                  Need software developed? Offering C# development all over the United States, ERL GLOBAL, Inc is the only call you will have to make.
                  Happiness in intelligent people is the rarest thing I know. -- Ernest Hemingway
                  Most of this sig is for Google, not ego.

                  B Offline
                  B Offline
                  Big Daddy Farang
                  wrote on last edited by
                  #32

                  Dictionary? :~ They probably even have them on the Internet.

                  BDF People don't mind being mean; but they never want to be ridiculous. -- Moliere

                  1 Reply Last reply
                  0
                  • G Giorgi Dalakishvili

                    Let's say you have this class:

                    class myClass
                    {
                    public double? Foo;
                    }

                    and this piece of code:

                    myClass temp=new myClass()
                    SqlDataReader rd = GetDataSomeHow();

                    if(rd.IsDBNull(0))
                    temp.Foo = null;
                    else
                    temp.Foo = rd.GetDouble(0);

                    Rewrite the above statement without if. [Edit] One line solution Sorry if it's too easy for you :) Here is the solution in case you can't solve it: Solution[^]

                    Giorgi Dalakishvili #region signature My Articles / My Latest Article[^] / My blog[^] #endregion

                    modified on Thursday, October 23, 2008 11:23 AM

                    P Offline
                    P Offline
                    PIEBALDconsult
                    wrote on last edited by
                    #33

                    Giorgi Dalakishvili wrote:

                    One line solution

                    What's a line?

                    1 Reply Last reply
                    0
                    • G Giorgi Dalakishvili

                      What if you have several properties in your class? Code will get very ugly.

                      Giorgi Dalakishvili #region signature My Articles / My Latest Article[^] / My blog[^] #endregion

                      P Offline
                      P Offline
                      PIEBALDconsult
                      wrote on last edited by
                      #34

                      Which is why it should be hidden.

                      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