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

Generic

Scheduled Pinned Locked Moved C#
csharphelp
6 Posts 3 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.
  • J Offline
    J Offline
    Jeeva Jose
    wrote on last edited by
    #1

    C#:rose: How can add two numbers through generic function and the type is int or double or float anybody help me.:omg:

    Continue...

    D 1 Reply Last reply
    0
    • J Jeeva Jose

      C#:rose: How can add two numbers through generic function and the type is int or double or float anybody help me.:omg:

      Continue...

      D Offline
      D Offline
      DavidNohejl
      wrote on last edited by
      #2

      I didn't test it but this should work:

      T Add<T>(T x, T y) where T: int, double, float
      {
         return x+y;
      }
      

      "Throughout human history, we have been dependent on machines to survive. Fate, it seems, is not without a sense of irony. " - Morpheus "Real men use mspaint for writing code and notepad for designing graphics." - Anna-Jayne Metcalfe

      N J 2 Replies Last reply
      0
      • D DavidNohejl

        I didn't test it but this should work:

        T Add<T>(T x, T y) where T: int, double, float
        {
           return x+y;
        }
        

        "Throughout human history, we have been dependent on machines to survive. Fate, it seems, is not without a sense of irony. " - Morpheus "Real men use mspaint for writing code and notepad for designing graphics." - Anna-Jayne Metcalfe

        N Offline
        N Offline
        Not Active
        wrote on last edited by
        #3

        You should have tested it then 'int' is not a valid constraint. A type used as a constraint must be an interface, a non-sealed class or a type parameter. 'double' is not a valid constraint. A type used as a constraint must be an interface, a non-sealed class or a type parameter. 'float' is not a valid constraint. A type used as a constraint must be an interface, a non-sealed class or a type parameter. Operator '+' cannot be applied to operands of type 'T' and 'T'


        only two letters away from being an asset

        D 1 Reply Last reply
        0
        • N Not Active

          You should have tested it then 'int' is not a valid constraint. A type used as a constraint must be an interface, a non-sealed class or a type parameter. 'double' is not a valid constraint. A type used as a constraint must be an interface, a non-sealed class or a type parameter. 'float' is not a valid constraint. A type used as a constraint must be an interface, a non-sealed class or a type parameter. Operator '+' cannot be applied to operands of type 'T' and 'T'


          only two letters away from being an asset

          D Offline
          D Offline
          DavidNohejl
          wrote on last edited by
          #4

          Oh, thank you. That was too cool to work :-O Ok so unless I am missing something obvious it's not as easy as it looks...

          IConvertible Add(T x, T y) where T : IConvertible
          {
          return (IConvertible)(x.ToDouble(CultureInfo.InvariantCulture)+y.ToDouble(CultureInfo.InvariantCulture));
          }
          

          Doesn't look that bad, but that's still not it. I don't want to return IConvertible but T, I want T to be only int, float or double (looks like this is impossible with where). Whatever, why not do just

          int Add(int x, int y){return x+y};
          double Add(double x, double y){return x+y};
          ...
          

          "Throughout human history, we have been dependent on machines to survive. Fate, it seems, is not without a sense of irony. " - Morpheus "Real men use mspaint for writing code and notepad for designing graphics." - Anna-Jayne Metcalfe

          1 Reply Last reply
          0
          • D DavidNohejl

            I didn't test it but this should work:

            T Add<T>(T x, T y) where T: int, double, float
            {
               return x+y;
            }
            

            "Throughout human history, we have been dependent on machines to survive. Fate, it seems, is not without a sense of irony. " - Morpheus "Real men use mspaint for writing code and notepad for designing graphics." - Anna-Jayne Metcalfe

            J Offline
            J Offline
            Jeeva Jose
            wrote on last edited by
            #5

            Thank u for helping me. I need ur help that code is not working properly. Give me the full source and ( where) what is the use of that one in generic. I expect get soon

            Continue...

            D 1 Reply Last reply
            0
            • J Jeeva Jose

              Thank u for helping me. I need ur help that code is not working properly. Give me the full source and ( where) what is the use of that one in generic. I expect get soon

              Continue...

              D Offline
              D Offline
              DavidNohejl
              wrote on last edited by
              #6

              Unfortunately I was wrong as Mark pointed out. where doesn't allow specific structs as type constraint. See http://msdn2.microsoft.com/en-us/library/d5x73970(VS.80).aspx[^]. Because int etc. are structures, you can't do this :( However, from what you said, it looks like easiest (and sufficient) thing to do is to have 3 overloads of Add(x,y) method - one for int, one for float, etc...


              "Throughout human history, we have been dependent on machines to survive. Fate, it seems, is not without a sense of irony. " - Morpheus "Real men use mspaint for writing code and notepad for designing graphics." - Anna-Jayne Metcalfe

              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