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. Casting Problem

Casting Problem

Scheduled Pinned Locked Moved C#
helpquestion
2 Posts 2 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
    John Pepper
    wrote on last edited by
    #1

    I have an interesting and stupid problem, I have a class that is dirived from another class and I want to type cast the first class to the second class somewhere in the program as seen below. Class A { } Class B { } A FirstObject=new A(); B SecondObject; SecondObject=(B)A When I attempt to do this in WebApplication I get Specific Cast Not Valid. What am I doing wrong?? John

    N 1 Reply Last reply
    0
    • J John Pepper

      I have an interesting and stupid problem, I have a class that is dirived from another class and I want to type cast the first class to the second class somewhere in the program as seen below. Class A { } Class B { } A FirstObject=new A(); B SecondObject; SecondObject=(B)A When I attempt to do this in WebApplication I get Specific Cast Not Valid. What am I doing wrong?? John

      N Offline
      N Offline
      Nick Pirocanac
      wrote on last edited by
      #2

      Hi John, I don't think that you can cast classes, even if they derive from the same base class in C# (even tho this is legal in C++). Instead, something like this may get you where you want to go. Good luck! Nick. ----------------------------------------------------------- A Sample Run: D:\>ClassCasting.exe ClassA! ClassB! D:\> ----------------------------------------------------------- using System; namespace ClassCasting { /// /// Summary description for Class1. /// class ClassCasting { /// /// The main entry point for the application. /// [STAThread] static void Main(string[] args) { ClassA oClassA = new ClassA(); ClassB oClassB = new ClassB(); // Can't do this in C# yet! //oClassB = (ClassB) oClassA; // But.... you can do this! CoreClass oCoreClass = oClassA; oCoreClass.SomeMethod(); oCoreClass = oClassB; oCoreClass.SomeMethod(); } } abstract class CoreClass { // Define some common methods here. public abstract void SomeMethod(); } class ClassA : CoreClass { public override void SomeMethod() { Console.WriteLine("ClassA!"); } } class ClassB : CoreClass { public override void SomeMethod() { Console.WriteLine("ClassB!"); } } }

      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