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. Nullables in type parameters?

Nullables in type parameters?

Scheduled Pinned Locked Moved C#
helpquestion
7 Posts 4 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.
  • D Offline
    D Offline
    Dominik Reichl
    wrote on last edited by
    #1

    Hello! I'm trying to write a function that checks whether a given value is in a range defined by a minimum and a maximum. I tried this:

    public static bool ValidateNumber<T>(T tNum, T? tMinimum, bool bMinInclusive,
    	T? tMaximum, bool bMaxInclusive)
    	where T : IComparable<T>
    

    I think it's pretty obvious what I'm trying to do ;) The minimum and the maximum should be optional, therefore nullables. Anyway, the declaration above gives me a compiler error CS0453 ("Type T must not allow NULL values, if it is used as T parameter in the generic type or in the generic method System.Nullable<T>"). How does the correct declaration look like in this case? Best regards Dominik


    _outp(0x64, 0xAD); and __asm mov al, 0xAD __asm out 0x64, al do the same... but what do they do?? ;) (doesn't work on NT)

    C C V 3 Replies Last reply
    0
    • D Dominik Reichl

      Hello! I'm trying to write a function that checks whether a given value is in a range defined by a minimum and a maximum. I tried this:

      public static bool ValidateNumber<T>(T tNum, T? tMinimum, bool bMinInclusive,
      	T? tMaximum, bool bMaxInclusive)
      	where T : IComparable<T>
      

      I think it's pretty obvious what I'm trying to do ;) The minimum and the maximum should be optional, therefore nullables. Anyway, the declaration above gives me a compiler error CS0453 ("Type T must not allow NULL values, if it is used as T parameter in the generic type or in the generic method System.Nullable<T>"). How does the correct declaration look like in this case? Best regards Dominik


      _outp(0x64, 0xAD); and __asm mov al, 0xAD __asm out 0x64, al do the same... but what do they do?? ;) (doesn't work on NT)

      C Offline
      C Offline
      Christian Graus
      wrote on last edited by
      #2

      I think you need to specify INullable as well as IComparable.

      Christian Graus - Microsoft MVP - C++ Metal Musings - Rex and my new metal blog

      D 1 Reply Last reply
      0
      • D Dominik Reichl

        Hello! I'm trying to write a function that checks whether a given value is in a range defined by a minimum and a maximum. I tried this:

        public static bool ValidateNumber<T>(T tNum, T? tMinimum, bool bMinInclusive,
        	T? tMaximum, bool bMaxInclusive)
        	where T : IComparable<T>
        

        I think it's pretty obvious what I'm trying to do ;) The minimum and the maximum should be optional, therefore nullables. Anyway, the declaration above gives me a compiler error CS0453 ("Type T must not allow NULL values, if it is used as T parameter in the generic type or in the generic method System.Nullable<T>"). How does the correct declaration look like in this case? Best regards Dominik


        _outp(0x64, 0xAD); and __asm mov al, 0xAD __asm out 0x64, al do the same... but what do they do?? ;) (doesn't work on NT)

        C Offline
        C Offline
        Colin Angus Mackay
        wrote on last edited by
        #3

        Dominik Reichl wrote:

        I think it's pretty obvious what I'm trying to do

        Not really - I think your compiler message is hiding another. You have declared that T is a IComparibale<T>, which is a reference type which is already nullable (nullables are only useful for value types). But declaring that T is an IComparible<T> is a circular reference. Unless I've missed something, T must therefore be an ICompariblae<IComparible<IComparible<Icomparible<....>>>>


        Upcoming Scottish Developers events: * UK Security Evangelists On Tour (2nd November, Edinburgh) * Developer Day Scotland: are you interested in speaking or attending? My: Website | Blog

        D 1 Reply Last reply
        0
        • C Colin Angus Mackay

          Dominik Reichl wrote:

          I think it's pretty obvious what I'm trying to do

          Not really - I think your compiler message is hiding another. You have declared that T is a IComparibale<T>, which is a reference type which is already nullable (nullables are only useful for value types). But declaring that T is an IComparible<T> is a circular reference. Unless I've missed something, T must therefore be an ICompariblae<IComparible<IComparible<Icomparible<....>>>>


          Upcoming Scottish Developers events: * UK Security Evangelists On Tour (2nd November, Edinburgh) * Developer Day Scotland: are you interested in speaking or attending? My: Website | Blog

          D Offline
          D Offline
          Dominik Reichl
          wrote on last edited by
          #4

          Ok, so I modified it to non-generic comparable:

          private static bool ValidateNumber<T>(T tNum, T? tMinimum, bool bMinInclusive,
          	T? tMaximum, bool bMaxInclusive)
          	where T : IComparable
          

          Anyway, still exactly the same compiler error is thrown... :(


          _outp(0x64, 0xAD); and __asm mov al, 0xAD __asm out 0x64, al do the same... but what do they do?? ;) (doesn't work on NT)

          C 1 Reply Last reply
          0
          • C Christian Graus

            I think you need to specify INullable as well as IComparable.

            Christian Graus - Microsoft MVP - C++ Metal Musings - Rex and my new metal blog

            D Offline
            D Offline
            Dominik Reichl
            wrote on last edited by
            #5

            INullable seems to be deeply hidden in the SQL namespace... Also, this interface only adds a IsNull property (but I also need HasValue and Value of "normal" Nullables). Are you sure I need this interface??


            _outp(0x64, 0xAD); and __asm mov al, 0xAD __asm out 0x64, al do the same... but what do they do?? ;) (doesn't work on NT)

            1 Reply Last reply
            0
            • D Dominik Reichl

              Ok, so I modified it to non-generic comparable:

              private static bool ValidateNumber<T>(T tNum, T? tMinimum, bool bMinInclusive,
              	T? tMaximum, bool bMaxInclusive)
              	where T : IComparable
              

              Anyway, still exactly the same compiler error is thrown... :(


              _outp(0x64, 0xAD); and __asm mov al, 0xAD __asm out 0x64, al do the same... but what do they do?? ;) (doesn't work on NT)

              C Offline
              C Offline
              Christian Graus
              wrote on last edited by
              #6

              The point is that you can remove the ? in T?, because IComparable means you can only pass classes, which are all nullable by default.

              Christian Graus - Microsoft MVP - C++ Metal Musings - Rex and my new metal blog

              1 Reply Last reply
              0
              • D Dominik Reichl

                Hello! I'm trying to write a function that checks whether a given value is in a range defined by a minimum and a maximum. I tried this:

                public static bool ValidateNumber<T>(T tNum, T? tMinimum, bool bMinInclusive,
                	T? tMaximum, bool bMaxInclusive)
                	where T : IComparable<T>
                

                I think it's pretty obvious what I'm trying to do ;) The minimum and the maximum should be optional, therefore nullables. Anyway, the declaration above gives me a compiler error CS0453 ("Type T must not allow NULL values, if it is used as T parameter in the generic type or in the generic method System.Nullable<T>"). How does the correct declaration look like in this case? Best regards Dominik


                _outp(0x64, 0xAD); and __asm mov al, 0xAD __asm out 0x64, al do the same... but what do they do?? ;) (doesn't work on NT)

                V Offline
                V Offline
                Vega02
                wrote on last edited by
                #7

                Remember that nullable types can only take value types as the generic argument (as value types cannot be null). So in your declaration you just need to specify that T is a value type: public static bool ValidateNumber(T tNum, T? tMinimum, bool bMinInclusive, T? tMaximum, bool bMaxInclusive) where T : struct, IComparable Hope this helps. :)

                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