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. How to get an a class without it's base class

How to get an a class without it's base class

Scheduled Pinned Locked Moved C#
questiontutorial
5 Posts 4 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.
  • E Offline
    E Offline
    eggie5
    wrote on last edited by
    #1

    Hi, I have a class Dog which derives from Animal. Suppose my instance of Dog is bulldog. bulldog has all the members of Animal plus Dog. How can copy just the members from Dog to a new Dog called boxer? Just get the Dog members from bulldog and leave out any inherited members from Animal. Dog bulldog=new Dog(); <-has inhereited members from Animal Dob boxer = bulldog - inherited Animal members How can I do this?

    /\ |_ E X E GG

    P C 2 Replies Last reply
    0
    • E eggie5

      Hi, I have a class Dog which derives from Animal. Suppose my instance of Dog is bulldog. bulldog has all the members of Animal plus Dog. How can copy just the members from Dog to a new Dog called boxer? Just get the Dog members from bulldog and leave out any inherited members from Animal. Dog bulldog=new Dog(); <-has inhereited members from Animal Dob boxer = bulldog - inherited Animal members How can I do this?

      /\ |_ E X E GG

      P Offline
      P Offline
      pmarfleet
      wrote on last edited by
      #2

      You could add a method to your Dog class that returns a copy of the current instance of the class. In your code, you could omit copying the member of the Dog class that have been inherited from the Animal class. Paul

      1 Reply Last reply
      0
      • E eggie5

        Hi, I have a class Dog which derives from Animal. Suppose my instance of Dog is bulldog. bulldog has all the members of Animal plus Dog. How can copy just the members from Dog to a new Dog called boxer? Just get the Dog members from bulldog and leave out any inherited members from Animal. Dog bulldog=new Dog(); <-has inhereited members from Animal Dob boxer = bulldog - inherited Animal members How can I do this?

        /\ |_ E X E GG

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

        You want to set properties that are common to the animal class, and then keep them that way ? So, for instance, if sex is a common property, you want to do this: Dog bulldog=new Dog(); // defaults to male bulldog.Sex = Sex.Female; Dob boxer = bulldog; // boxer is male That's a weird and counterintuitive requirement. You could do this with your own operator =, but I would recommend against it. Adding a method that creates a new Dog, then sets the local properties, is the way to go public Dog GetDefaultAnimalWithDogProperties() { Dog dog = new Dog(); dog.DogBreed = this.DogBreed; // Or whatever properties are only on the dog class return dog; }

        Christian Graus - Microsoft MVP - C++ "I am working on a project that will convert a FORTRAN code to corresponding C++ code.I am not aware of FORTRAN syntax" ( spotted in the C++/CLI forum )

        E 1 Reply Last reply
        0
        • C Christian Graus

          You want to set properties that are common to the animal class, and then keep them that way ? So, for instance, if sex is a common property, you want to do this: Dog bulldog=new Dog(); // defaults to male bulldog.Sex = Sex.Female; Dob boxer = bulldog; // boxer is male That's a weird and counterintuitive requirement. You could do this with your own operator =, but I would recommend against it. Adding a method that creates a new Dog, then sets the local properties, is the way to go public Dog GetDefaultAnimalWithDogProperties() { Dog dog = new Dog(); dog.DogBreed = this.DogBreed; // Or whatever properties are only on the dog class return dog; }

          Christian Graus - Microsoft MVP - C++ "I am working on a project that will convert a FORTRAN code to corresponding C++ code.I am not aware of FORTRAN syntax" ( spotted in the C++/CLI forum )

          E Offline
          E Offline
          eggie5
          wrote on last edited by
          #4

          Maybe, I'm not explaining it very well, here's what I want to do without dog metaphors. I have a class that is derived from another class that does lots of reflection. So when I pass my class to JavascriptConvert.SerializeObject(myclass) all of the base class reflection causes a self referencing loop exception to be thrown from SerializeObject. That's why I don't want to include anything from the base class. (SerializeObject creates a JSON string of the object) -- modified at 18:24 Tuesday 14th August, 2007

          /\ |_ E X E GG

          L 1 Reply Last reply
          0
          • E eggie5

            Maybe, I'm not explaining it very well, here's what I want to do without dog metaphors. I have a class that is derived from another class that does lots of reflection. So when I pass my class to JavascriptConvert.SerializeObject(myclass) all of the base class reflection causes a self referencing loop exception to be thrown from SerializeObject. That's why I don't want to include anything from the base class. (SerializeObject creates a JSON string of the object) -- modified at 18:24 Tuesday 14th August, 2007

            /\ |_ E X E GG

            L Offline
            L Offline
            Luc Pattyn
            wrote on last edited by
            #5

            Hi, when you use reflection, you can specify the scope of the things you are looking for. As an example, Type.GetMethods accepts a BindingsFlag, you may want to specify BindingFlags.DeclaredOnly to avoid inherited stuff. :)

            Luc Pattyn [Forum Guidelines] [My Articles]


            this weeks tips: - make Visual display line numbers: Tools/Options/TextEditor/... - show exceptions with ToString() to see all information - before you ask a question here, search CodeProject, then Google


            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