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
CODE PROJECT For Those Who Code
  • Home
  • Articles
  • FAQ
Community
  1. Home
  2. General Programming
  3. C#
  4. Generics Problem

Generics Problem

Scheduled Pinned Locked Moved C#
helpquestion
5 Posts 3 Posters 1 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.
  • T Offline
    T Offline
    thepersonof
    wrote on last edited by
    #1

    Hi I have a generic class: myclass where I put T as either a float or a double. However whenever I compile I get the error: Cannot convert type 'T' to 'float' I know that any of the types I use can be converted to a float. How can I tell the compiler this? thank you

    E S 2 Replies Last reply
    0
    • T thepersonof

      Hi I have a generic class: myclass where I put T as either a float or a double. However whenever I compile I get the error: Cannot convert type 'T' to 'float' I know that any of the types I use can be converted to a float. How can I tell the compiler this? thank you

      E Offline
      E Offline
      eggsovereasy
      wrote on last edited by
      #2

      You didn't give the code so I'm guessing here... Did you do an exlicit cast?

      myclass x = new myclass(5.6);

      double y = (double)x.value;

      If that fails, you could always do something uglier:

      float z = x.value;

      double y = (double)z;

      1 Reply Last reply
      0
      • T thepersonof

        Hi I have a generic class: myclass where I put T as either a float or a double. However whenever I compile I get the error: Cannot convert type 'T' to 'float' I know that any of the types I use can be converted to a float. How can I tell the compiler this? thank you

        S Offline
        S Offline
        Stefan Troschuetz
        wrote on last edited by
        #3

        Use the where clause to add a constraint to the generic type T, so that it has to implement the IConvertible interface. Afterwards you can use the ToSingle method to convert instances of the generic type T to Single (float) type.

        class myclass where T : IConvertible
        {
        private void Test(T q)
        {
        float x = q.ToSingle(null);
        }
        }


        "Programming today is a race between software engineers striving to build bigger and better idiot-proof programs, and the Universe trying to produce bigger and better idiots. So far, the Universe is winning." - Rick Cook

        www.troschuetz.de

        T 1 Reply Last reply
        0
        • S Stefan Troschuetz

          Use the where clause to add a constraint to the generic type T, so that it has to implement the IConvertible interface. Afterwards you can use the ToSingle method to convert instances of the generic type T to Single (float) type.

          class myclass where T : IConvertible
          {
          private void Test(T q)
          {
          float x = q.ToSingle(null);
          }
          }


          "Programming today is a race between software engineers striving to build bigger and better idiot-proof programs, and the Universe trying to produce bigger and better idiots. So far, the Universe is winning." - Rick Cook

          www.troschuetz.de

          T Offline
          T Offline
          thepersonof
          wrote on last edited by
          #4

          thank you very much

          T 1 Reply Last reply
          0
          • T thepersonof

            thank you very much

            T Offline
            T Offline
            thepersonof
            wrote on last edited by
            #5

            Problem 1... I now have the same problem, but the other way round. How can I convert a float to type T (which is either a float or a double and is iconvertible)? I get the error: Cannot convert type 'float' to 'T' Problem 2... The error: Operator '*' cannot be applied to operands of type 'T' and 'T' when I multiply two numbers What can I do about this? Does float inherit from some class that defines basic mathematical operations? All help is greatly appreciated.. I feel the end is in sight!! thank you

            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