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. annoying null problems

annoying null problems

Scheduled Pinned Locked Moved C#
visual-studioquestion
11 Posts 7 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 gus_br

    why this code doesn`t work? (VS 2003) declaration, and, possibly, assigment: DateTime dataTermino = null; if(txtDataInicio.Text != "") dataTermino = DateTime.Parse(txtDataTermino.Text); and, in another class, the test: if(dataTermino != null) insertCommand.Parameters.Add(new SqlParameter("@DataTermino", dataTermino)); VS2003 Errors: * Operator '!=' cannot be applied to operands of type 'System.DateTime' and 'null' * Cannot convert null to 'System.DateTime' because it is a value type WTF! :mad::( thanks... ;)

    T Offline
    T Offline
    tcss
    wrote on last edited by
    #2

    before using the datetime variable you must create it. DateTime dataTermino = new DateTime() will do the job for you

    G G 2 Replies Last reply
    0
    • T tcss

      before using the datetime variable you must create it. DateTime dataTermino = new DateTime() will do the job for you

      G Offline
      G Offline
      gus_br
      wrote on last edited by
      #3

      ok, but after i want test if it is null. with a object reference i cant test it, and i dont want reference the actual date.

      J 1 Reply Last reply
      0
      • G gus_br

        why this code doesn`t work? (VS 2003) declaration, and, possibly, assigment: DateTime dataTermino = null; if(txtDataInicio.Text != "") dataTermino = DateTime.Parse(txtDataTermino.Text); and, in another class, the test: if(dataTermino != null) insertCommand.Parameters.Add(new SqlParameter("@DataTermino", dataTermino)); VS2003 Errors: * Operator '!=' cannot be applied to operands of type 'System.DateTime' and 'null' * Cannot convert null to 'System.DateTime' because it is a value type WTF! :mad::( thanks... ;)

        L Offline
        L Offline
        Leslie Sanford
        wrote on last edited by
        #4

        The DateTime class is a struct and so can't be null. It's just like int, double, or one of the other value types, they can't be null.

        1 Reply Last reply
        0
        • G gus_br

          ok, but after i want test if it is null. with a object reference i cant test it, and i dont want reference the actual date.

          J Offline
          J Offline
          Judah Gabriel Himango
          wrote on last edited by
          #5

          There are 2 types of objects in .NET: reference types and value types. All classes are reference types. All structs are value types. All reference types can be assigned null. All value types cannot. The reason for these 2 is for lightweight, fast stack-allocated values. For instance, System.Int32, also aliased as int is a value type: you can pass it to a function which will actually pass a copy of the value. Value types always must have a value; you can't assign them null, as that's not how value types work, nor should it be. Assigning an integer to null doesn't make sense, for instance. And since DateTime is a value type, you can't assign null to it. Now, if you were using .NET 2.0, there is a new wrapper class called System.Nullable, where you can wrap a value type inside an object that can either be null or the value. Here's what that looks like:

          Nullable<DateTime> nullableDateTime = new Nullable<DateTime>;
          nullableDateTime = null;
          nullableDateTime = DateTime.Now;

          C# 2.0 has short-hand syntax for this, using the question mark:

          DateTime? nullableDateTime = new DateTime?();
          nullableDateTime = null;
          nullableDateTime = DateTime.Now;

          Tech, life, family, faith: Give me a visit. I'm currently blogging about: God-as-Judge, God-as-Forgiver The apostle Paul, modernly speaking: Epistles of Paul Judah Himango

          G 1 Reply Last reply
          0
          • G gus_br

            why this code doesn`t work? (VS 2003) declaration, and, possibly, assigment: DateTime dataTermino = null; if(txtDataInicio.Text != "") dataTermino = DateTime.Parse(txtDataTermino.Text); and, in another class, the test: if(dataTermino != null) insertCommand.Parameters.Add(new SqlParameter("@DataTermino", dataTermino)); VS2003 Errors: * Operator '!=' cannot be applied to operands of type 'System.DateTime' and 'null' * Cannot convert null to 'System.DateTime' because it is a value type WTF! :mad::( thanks... ;)

            P Offline
            P Offline
            PlayByTheRules
            wrote on last edited by
            #6

            pontonet wrote:

            WTF!

            Why not learn more about the environment in which you are developing before exclaiming "WTF!". The difference between value and reference types is in any good beginners guide to .NET and/or C#.

            G 1 Reply Last reply
            0
            • J Judah Gabriel Himango

              There are 2 types of objects in .NET: reference types and value types. All classes are reference types. All structs are value types. All reference types can be assigned null. All value types cannot. The reason for these 2 is for lightweight, fast stack-allocated values. For instance, System.Int32, also aliased as int is a value type: you can pass it to a function which will actually pass a copy of the value. Value types always must have a value; you can't assign them null, as that's not how value types work, nor should it be. Assigning an integer to null doesn't make sense, for instance. And since DateTime is a value type, you can't assign null to it. Now, if you were using .NET 2.0, there is a new wrapper class called System.Nullable, where you can wrap a value type inside an object that can either be null or the value. Here's what that looks like:

              Nullable<DateTime> nullableDateTime = new Nullable<DateTime>;
              nullableDateTime = null;
              nullableDateTime = DateTime.Now;

              C# 2.0 has short-hand syntax for this, using the question mark:

              DateTime? nullableDateTime = new DateTime?();
              nullableDateTime = null;
              nullableDateTime = DateTime.Now;

              Tech, life, family, faith: Give me a visit. I'm currently blogging about: God-as-Judge, God-as-Forgiver The apostle Paul, modernly speaking: Epistles of Paul Judah Himango

              G Offline
              G Offline
              gus_br
              wrote on last edited by
              #7

              hey... thanks... i`ve used a overloaded method to deal with this situation, instead of testing if a parameter is null or not. great explanation... it was very instructive. :] cya

              1 Reply Last reply
              0
              • P PlayByTheRules

                pontonet wrote:

                WTF!

                Why not learn more about the environment in which you are developing before exclaiming "WTF!". The difference between value and reference types is in any good beginners guide to .NET and/or C#.

                G Offline
                G Offline
                gus_br
                wrote on last edited by
                #8

                sometimes we forgot the basic, even after read a lot of stuffs. sometimes we are kinda influencied by other languages, like java. a DateTime tends to be a real object instead of a value type - it has properties, methods, constructors, etc. so the difference isn`t so obvious. :-D

                1 Reply Last reply
                0
                • G gus_br

                  why this code doesn`t work? (VS 2003) declaration, and, possibly, assigment: DateTime dataTermino = null; if(txtDataInicio.Text != "") dataTermino = DateTime.Parse(txtDataTermino.Text); and, in another class, the test: if(dataTermino != null) insertCommand.Parameters.Add(new SqlParameter("@DataTermino", dataTermino)); VS2003 Errors: * Operator '!=' cannot be applied to operands of type 'System.DateTime' and 'null' * Cannot convert null to 'System.DateTime' because it is a value type WTF! :mad::( thanks... ;)

                  K Offline
                  K Offline
                  Keith Barrow
                  wrote on last edited by
                  #9

                  If you're using C# 2.0 you can declare DateTime as nullable with the following: DateTime? dataTermino = null; if(txtDataInicio.Text != "") dataTermino = DateTime.Parse(txtDataTermino.Text); if(dataTermino != null) insertCommand.Parameters.Add(new SqlParameter("@DataTermino", dataTermino)); (note the "?" after DateTime)would work. If you're not using 2.0 & need to pass null date to SQL , have a look at: http://www.c-sharpcorner.com/Code/2003/Sept/EnterNullValuesForDateTime.asp[^] Hope this helps

                  1 Reply Last reply
                  0
                  • T tcss

                    before using the datetime variable you must create it. DateTime dataTermino = new DateTime() will do the job for you

                    G Offline
                    G Offline
                    Guffa
                    wrote on last edited by
                    #10

                    tcss wrote:

                    before using the datetime variable you must create it.

                    Not at all. DateTime is a value type, not a reference type.

                    --- b { font-weight: normal; }

                    T 1 Reply Last reply
                    0
                    • G Guffa

                      tcss wrote:

                      before using the datetime variable you must create it.

                      Not at all. DateTime is a value type, not a reference type.

                      --- b { font-weight: normal; }

                      T Offline
                      T Offline
                      tcss
                      wrote on last edited by
                      #11

                      Cut my teeth in C# with .Net 2.0. See comment below. Thanks for correcting my post

                      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