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. Inheritance question

Inheritance question

Scheduled Pinned Locked Moved C#
questioncsharpoophelp
7 Posts 6 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.
  • X Offline
    X Offline
    xkrja
    wrote on last edited by
    #1

    I've referenced a dll made in VB.Net and now I need to inherit from some of the classes in the dll. Below is an example of how I did that:

    class Tag : DataProvider.Trend.Tag
    {
    }

    Now if I do like this:

    DataProvider.Trend.Tag myBaseTag = new DataProvider.Trend.Tag();
    Tag myTag = myBaseTag as Tag;

    this results in that 'myTag' gets the value null. Why is that? How can I assign the value of myBaseTag to myTag? Thanks for help!

    L C D D 4 Replies Last reply
    0
    • X xkrja

      I've referenced a dll made in VB.Net and now I need to inherit from some of the classes in the dll. Below is an example of how I did that:

      class Tag : DataProvider.Trend.Tag
      {
      }

      Now if I do like this:

      DataProvider.Trend.Tag myBaseTag = new DataProvider.Trend.Tag();
      Tag myTag = myBaseTag as Tag;

      this results in that 'myTag' gets the value null. Why is that? How can I assign the value of myBaseTag to myTag? Thanks for help!

      L Offline
      L Offline
      Lost User
      wrote on last edited by
      #2

      Tag myTag = new Tag(); DataProvider.Trend.Tag myBaseTag = myTag as DataProvider.Trend.Tag; Because myTag inherite from DataProvider.Trend.Tag.

      X 1 Reply Last reply
      0
      • L Lost User

        Tag myTag = new Tag(); DataProvider.Trend.Tag myBaseTag = myTag as DataProvider.Trend.Tag; Because myTag inherite from DataProvider.Trend.Tag.

        X Offline
        X Offline
        xkrja
        wrote on last edited by
        #3

        Thanks for the reply. But the problem for me is that I call methods in the dll I mention and they return the type 'DataProvider.Trend.Tag' BUT I need to cast that to type 'Tag'. I don't want to cast 'Tag' to 'DataProvider.Trend.Tag' (the reason is that 'Tag' so that it is accepted by a Web Service). Thanks again.

        M 1 Reply Last reply
        0
        • X xkrja

          I've referenced a dll made in VB.Net and now I need to inherit from some of the classes in the dll. Below is an example of how I did that:

          class Tag : DataProvider.Trend.Tag
          {
          }

          Now if I do like this:

          DataProvider.Trend.Tag myBaseTag = new DataProvider.Trend.Tag();
          Tag myTag = myBaseTag as Tag;

          this results in that 'myTag' gets the value null. Why is that? How can I assign the value of myBaseTag to myTag? Thanks for help!

          C Offline
          C Offline
          Christian Graus
          wrote on last edited by
          #4

          xkrja wrote:

          DataProvider.Trend.Tag myBaseTag = new DataProvider.Trend.Tag(); Tag myTag = myBaseTag as Tag;

          Of course that does not work. You can cast down, you cannot cast up. You need to read a basic book on OO

          Christian Graus Driven to the arms of OSX by Vista. Read my blog to find out how I've worked around bugs in Microsoft tools and frameworks.

          1 Reply Last reply
          0
          • X xkrja

            I've referenced a dll made in VB.Net and now I need to inherit from some of the classes in the dll. Below is an example of how I did that:

            class Tag : DataProvider.Trend.Tag
            {
            }

            Now if I do like this:

            DataProvider.Trend.Tag myBaseTag = new DataProvider.Trend.Tag();
            Tag myTag = myBaseTag as Tag;

            this results in that 'myTag' gets the value null. Why is that? How can I assign the value of myBaseTag to myTag? Thanks for help!

            D Offline
            D Offline
            DaveyM69
            wrote on last edited by
            #5

            That's not going to work 'out of the box', you need to provide an explicit/implicit conversion method if you want to upcast.

            Dave
            Generic BackgroundWorker - My latest article!
            BTW, in software, hope and pray is not a viable strategy. (Luc Pattyn)
            Why are you using VB6? Do you hate yourself? (Christian Graus)

            1 Reply Last reply
            0
            • X xkrja

              Thanks for the reply. But the problem for me is that I call methods in the dll I mention and they return the type 'DataProvider.Trend.Tag' BUT I need to cast that to type 'Tag'. I don't want to cast 'Tag' to 'DataProvider.Trend.Tag' (the reason is that 'Tag' so that it is accepted by a Web Service). Thanks again.

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

              You can cast an object of type A to a type B that inherits from A only when the object was already declared as B. What you want to achieve is possible by creating a new object of type Tag and then initializing all its properties by using values from the 'DataProvider.Trend.Tag' original object (you can create a copy constructor for this).

              1 Reply Last reply
              0
              • X xkrja

                I've referenced a dll made in VB.Net and now I need to inherit from some of the classes in the dll. Below is an example of how I did that:

                class Tag : DataProvider.Trend.Tag
                {
                }

                Now if I do like this:

                DataProvider.Trend.Tag myBaseTag = new DataProvider.Trend.Tag();
                Tag myTag = myBaseTag as Tag;

                this results in that 'myTag' gets the value null. Why is that? How can I assign the value of myBaseTag to myTag? Thanks for help!

                D Offline
                D Offline
                dojohansen
                wrote on last edited by
                #7

                Say we have

                class A { protected string field1; }
                class B : A { protected string field2; }
                class C : B { protected string field3; }

                void foo(A a) {}
                void bar(B b) {}
                void foobar(C c) {}

                Now consider what instances of these objects *actually* will be inside your hardware. (We can safely ignore the fact that A extends object for the purpose of looking at why your attempted cast doesn't make sense.) First a word about references: A reference is what we might also call a "managed pointer": Like a pointer, it simply contains the address where the data is stored, but unlike a pointer, we cannot obtain this address and the memory manager can move stuff around and update pointers as needed without us knowing anything about it. Since string is a reference type, an instance of A is a word of memory (4 bytes on a 32-bit system) containing a reference to string data. An instance of B however is two words of data, containing two references, to field1 and field2 data. And C of course is three words, laid out in memory like an instance of B appended with the additional reference to field3 data. What you want to do is the equivalent of trying to use an instance of A and pass it to foobar, which takes a parameter of type C. Since an A is just the word for the field1 reference, foobar might access memory outside the bounds of the object and it could screw up other live objects (placed next to the instance of A in memory). So clearly the compiler shouldn't let you do that. If you wanted the compiler to just go ahead and create an instance of C for you when you make the cast, it wouldn't know what to do with field2 and field3. There is no way *in general* to know that the MotorBike class (descendant type) works just fine with only the data it inherited from the Vehicle class. If there were, we could just go like this:

                AmazingWebSerive s = (AmazingWebService)(new object());

                Sadly, this doesn't work. :) Instead, it's you as a programmer who may know this and provide a way to construct a B with only A data, or a C with only A data, and so on. Then you decide what the defaults should be for the data that doesn't exist in A.

                class A { protected string field1; }
                class B : A { protected string field2; }
                class C : B
                {
                protected string field3;
                public C(A a) { field1 = a.field1; }
                }

                Now if I have an A and I want to "use it as a C" I can simply go C c = new C(a); and work from there. BUT... if the reason you derived your own Tag class was be

                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