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. CType in C#?

CType in C#?

Scheduled Pinned Locked Moved C#
csharpquestion
7 Posts 5 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.
  • M Offline
    M Offline
    monrobot13
    wrote on last edited by
    #1

    Is there any function equivalent to the VB function CType (it converts an expression into a type) in C#? - monrobot13

    R M A A M 5 Replies Last reply
    0
    • M monrobot13

      Is there any function equivalent to the VB function CType (it converts an expression into a type) in C#? - monrobot13

      R Offline
      R Offline
      RB Emphasys
      wrote on last edited by
      #2

      Have you tried using the static class with Convert.ChangeType?

      1 Reply Last reply
      0
      • M monrobot13

        Is there any function equivalent to the VB function CType (it converts an expression into a type) in C#? - monrobot13

        M Offline
        M Offline
        Martin Cook
        wrote on last edited by
        #3

        If all you want to do is convert say an object of type "A" in to an object of type "B" (assuming the conversion is valid), then you can just cast like this: A a1 = new A(); B b1 = (A)a1;

        1 Reply Last reply
        0
        • M monrobot13

          Is there any function equivalent to the VB function CType (it converts an expression into a type) in C#? - monrobot13

          A Offline
          A Offline
          Andres Manggini
          wrote on last edited by
          #4

          Hi, Activator.CreateInstance Method[^] Might be what you're looking for. Andres Manggini. Buenos Aires - Argentina.

          1 Reply Last reply
          0
          • M monrobot13

            Is there any function equivalent to the VB function CType (it converts an expression into a type) in C#? - monrobot13

            A Offline
            A Offline
            Atul Kale
            wrote on last edited by
            #5

            Hi there,
            Your problem seems very simple and hence many quick answers are there. But I would elaborate more on ur requirement.
            Changing an Expression to a type could be of 2 types...
            1. "1" as a String to 1 as an Integer(int)
            2. "a" as a String to 97 as an integer(int).
            3. obj as an Object to a Specific type like "Customer"
            You need to find out ur requirement and accordingly use the Solutions given to u.
            Answer 1.: Use Convert.ToInt32 or Int32.Parse functions.
            Answer 2.: Use a typecast i.e. (int)num1
            Answer 3.: Use a typeCast i.e. (Customer)obj or Use Convert.ChangeType function.

            I hope this helps.
            regards
            Atul p.s.: Convert.ChangeType could be the sole solution sometimes. Atul Kale MCSD, MCT Sr. Software Engineer XcelVision Technologies Ltd.

            1 Reply Last reply
            0
            • M monrobot13

              Is there any function equivalent to the VB function CType (it converts an expression into a type) in C#? - monrobot13

              M Offline
              M Offline
              monrobot13
              wrote on last edited by
              #6

              Thank you for all the replies, but unfortunately none are really what I can use. Here is basically what I'm trying to do. I take two variables and divide them, then I multiply by another variable. The division usually yeilds a number below zero so multiplying still gives me zero. Now with the CType function you can convert the expression to an int and it will round the number up to one instead of zero. Like this for example:

              [Visual Basic]
              // var1 = 7 [int]
              // var2 = 10 [int]
              var3 = CType ((var1/var2), Integer)
              // var3 [int] will equal 1, instead of 0

              The variables mentioned above are all ints and changing their type is not an option. I guess what I really need is something that will round an expression to the nearest whole number. - monrobot13

              A 1 Reply Last reply
              0
              • M monrobot13

                Thank you for all the replies, but unfortunately none are really what I can use. Here is basically what I'm trying to do. I take two variables and divide them, then I multiply by another variable. The division usually yeilds a number below zero so multiplying still gives me zero. Now with the CType function you can convert the expression to an int and it will round the number up to one instead of zero. Like this for example:

                [Visual Basic]
                // var1 = 7 [int]
                // var2 = 10 [int]
                var3 = CType ((var1/var2), Integer)
                // var3 [int] will equal 1, instead of 0

                The variables mentioned above are all ints and changing their type is not an option. I guess what I really need is something that will round an expression to the nearest whole number. - monrobot13

                A Offline
                A Offline
                Atul Kale
                wrote on last edited by
                #7

                I was just wondering, the solution to ur problem is so Basic, in fact u must understand here, that u did not phrase ur question properly! U were just asking for ways to Convert the data.
                Its a simple logic that while doing such operations, U never store the intermediate results in variables.
                int num1 = 6, num2 = 10, num3 = 5, result; here if u want, (num1 / num2) * num3 which yields (6/10) * 5 = 0.6 * 5
                Then boss, you should not store num1/num2 in an int variable. That is ur problem.
                Do the Direct calculation,
                result = (num1 / num2) * num3;
                or,
                use an intermediate double or float variable it u still wanna store the division.
                double div = num1/ num2;
                result = div * num3;
                Sometimes one should just concenrate on the basics first and not just jump into complexities.
                Atul Kale MCSD, MCT Sr. Software Engineer XcelVision Technologies Ltd.

                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