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.
  • 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

    C Offline
    C Offline
    Chris Losinger
    wrote on last edited by
    #21

    1. initialize temp's properties to null 2. try { do all your Get*() calls } or, write Get*Default functions which do the null test and assign either the recordset value, or a default value in case the field is null.

    image processing toolkits | batch image processing

    1 Reply Last reply
    0
    • G Giorgi Dalakishvili

      Sorry, some characters got swallowed. The code will not compile.

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

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

      Ah, yeah, sometimes the ternary is a bear because of the typing. Cast using (double?) instead I believe. Really, however, if this is to migrate into a programming question it should not be in the lounge. The logic is inferrable from what I wrote.

      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.

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

        Ah, yeah, sometimes the ternary is a bear because of the typing. Cast using (double?) instead I believe. Really, however, if this is to migrate into a programming question it should not be in the lounge. The logic is inferrable from what I wrote.

        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.

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

        Nice solution. My solution is similar but a little bit different.

        Ennis Ray Lynch, Jr. wrote:

        Really, however, if this is to migrate into a programming question it should not be in the lounge.

        I don't think it's a programming question as I already had solution when I posted it.

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

        1 Reply Last reply
        0
        • E Ennis Ray Lynch Jr

          I wasn't paying any attention. I just squeezed in an emergency production db fix using that exact code so I naturally used the ''.

          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.

          C Offline
          C Offline
          cpkilekofp
          wrote on last edited by
          #24

          ;-) Happens to the best of us, and even me occasionally :laugh:

          1 Reply Last reply
          0
          • 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