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. Quick generics q

Quick generics q

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

    Hey I am just starting with generics: 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

    R 1 Reply Last reply
    0
    • T thepersonof

      Hey I am just starting with generics: 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

      R Offline
      R Offline
      Rob Graham
      wrote on last edited by
      #2

      Perhaps a snippet or two of your code would help?

      T 1 Reply Last reply
      0
      • R Rob Graham

        Perhaps a snippet or two of your code would help?

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

        It's rather simple: #1: this.X = (T)xValue; where this.X and xValue is float and T is double or float. #2: Math.Sqrt( X*X+Y*Y); where X,Y are of type T which is either double or float (and impleements iconvertable). thank you!

        N 1 Reply Last reply
        0
        • T thepersonof

          It's rather simple: #1: this.X = (T)xValue; where this.X and xValue is float and T is double or float. #2: Math.Sqrt( X*X+Y*Y); where X,Y are of type T which is either double or float (and impleements iconvertable). thank you!

          N Offline
          N Offline
          Nader Elshehabi
          wrote on last edited by
          #4

          Hello Actually this is not the snippet he was lokking for. You should post the definition of your generic class/method itself rather than just its call. Also don't forget to check "Ignore HTML tags..." checkbox present below the editting textbox.

          Regards:rose:

          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