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. Custom type casting

Custom type casting

Scheduled Pinned Locked Moved C#
questioncsharphelptutorial
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.
  • M Offline
    M Offline
    Master Toothless One
    wrote on last edited by
    #1

    Say I have custom class foo. I also have a class bar that inherits from foo. I want a method that returns an instance of foo and then want to cast it to bar. How can I do this? I am working with C# 2.0. This is how I imagine it working, and it compiles this way, but I get a runtime error that bar cannot be converted into foo. public class foo { public string someVariable; } public class bar : foo { public string someotherVariable; } public static foo SomeFunction() { foo returnVal = new foo(); return foo; } public static void AnotherFunction() { bar barVar = (bar) SomeFunction(); } I am almost positive that this can be done, but I can't seem to remember how to do it. I don't want to pass in some kind of reference param or Type.

    L P 2 Replies Last reply
    0
    • M Master Toothless One

      Say I have custom class foo. I also have a class bar that inherits from foo. I want a method that returns an instance of foo and then want to cast it to bar. How can I do this? I am working with C# 2.0. This is how I imagine it working, and it compiles this way, but I get a runtime error that bar cannot be converted into foo. public class foo { public string someVariable; } public class bar : foo { public string someotherVariable; } public static foo SomeFunction() { foo returnVal = new foo(); return foo; } public static void AnotherFunction() { bar barVar = (bar) SomeFunction(); } I am almost positive that this can be done, but I can't seem to remember how to do it. I don't want to pass in some kind of reference param or Type.

      L Offline
      L Offline
      led mike
      wrote on last edited by
      #2

      You can't cast a foo to a bar when you never created a bar new foo(); creates a foo not a bar so you can't cast it to a bar.

      led mike

      1 Reply Last reply
      0
      • M Master Toothless One

        Say I have custom class foo. I also have a class bar that inherits from foo. I want a method that returns an instance of foo and then want to cast it to bar. How can I do this? I am working with C# 2.0. This is how I imagine it working, and it compiles this way, but I get a runtime error that bar cannot be converted into foo. public class foo { public string someVariable; } public class bar : foo { public string someotherVariable; } public static foo SomeFunction() { foo returnVal = new foo(); return foo; } public static void AnotherFunction() { bar barVar = (bar) SomeFunction(); } I am almost positive that this can be done, but I can't seem to remember how to do it. I don't want to pass in some kind of reference param or Type.

        P Offline
        P Offline
        Patricker
        wrote on last edited by
        #3

        What you've set your code up to do is the ability to cast a bar to a foo; but you can't cast a foo to a bar unless it was already a bar to begin with that you had previously cast as a foo.

        M 1 Reply Last reply
        0
        • P Patricker

          What you've set your code up to do is the ability to cast a bar to a foo; but you can't cast a foo to a bar unless it was already a bar to begin with that you had previously cast as a foo.

          M Offline
          M Offline
          Master Toothless One
          wrote on last edited by
          #4

          Any ideas of how to do something along these lines? In this scenario, foo holds a bunch of variables used in as results from some operations. I have several super classes that inherit foo that have variables that are specific output for that operation. As an example, say foo holds a return code and input parameters for calling a Process object. bar has a variable that is assigned later on in the calling method. Another class that also inherits from foo has another unrelated variable that is also set later on by the calling function. Basically, I am trying to write a method that calls the Process object and then returns a base type of foo and then I want to be able to assign the variables that are specific to the superclasses without writing a Convert function. Seems like there is a fairly easy way to do this, but I keep drawing blanks.

          P 1 Reply Last reply
          0
          • M Master Toothless One

            Any ideas of how to do something along these lines? In this scenario, foo holds a bunch of variables used in as results from some operations. I have several super classes that inherit foo that have variables that are specific output for that operation. As an example, say foo holds a return code and input parameters for calling a Process object. bar has a variable that is assigned later on in the calling method. Another class that also inherits from foo has another unrelated variable that is also set later on by the calling function. Basically, I am trying to write a method that calls the Process object and then returns a base type of foo and then I want to be able to assign the variables that are specific to the superclasses without writing a Convert function. Seems like there is a fairly easy way to do this, but I keep drawing blanks.

            P Offline
            P Offline
            Patricker
            wrote on last edited by
            #5

            So inside Foo make a method, lets call it FillFoo(), you make this public and then you can now call it from anything that inherits from Foo. This function however will only fill the values you want to put in Foo. The values for bar and other derived objects can be filled with their own methods contained in their respective objects, such as FillBar(), etc... I think thats sort of what you are trying to do...

            M 1 Reply Last reply
            0
            • P Patricker

              So inside Foo make a method, lets call it FillFoo(), you make this public and then you can now call it from anything that inherits from Foo. This function however will only fill the values you want to put in Foo. The values for bar and other derived objects can be filled with their own methods contained in their respective objects, such as FillBar(), etc... I think thats sort of what you are trying to do...

              M Offline
              M Offline
              Master Toothless One
              wrote on last edited by
              #6

              Yeah, that is basically what I ended up doing. Instead of a method though I made a new protected constructor for foo that takes an instance of foo as a param. I then created a new constructor on bar that takes foo as a param and just pass it down the chain. Kinda silly, but it works. I get why .NET doesn't allow you to do what I was trying originally, but it would be nice if you could cast up the inheritance chain (foo can be cast into bar) as well as down (bar can be cast into foo). Thanks for pointing me down the right path. -- modified at 20:24 Tuesday 8th August, 2006

              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