locked
C# Classes and member variables RRS feed

  • Question

  • I have 4 classes with 3 unique member variables in each class. I want to join ClassOne to ClassTwo, ClassThree and ClassFour, and ClassTwo to ClassOne, ClassThree, and ClassFour ect. Anyone have tips?
    • Edited by johnnyphil Friday, May 6, 2016 3:52 PM
    Friday, May 6, 2016 3:52 PM

Answers

  • Well, I think I might need a bit of further clarification on what you mean by join... but, it seems that you're looking to create an inheritance hierarchy.  The part that's a bit unclear in particular is  "ClassOne 'join' ClassTwo" and then "ClassTwo 'join' ClassOne". If we were to replace the word join with inherits and the words One and Two with Parent and Child respectively, we would have a bit of a problem. However, if you're truley looking for a "join," then the closest thing would be a partial class, but partial classes would have to have the same name. But assuming a hierarchical structure...

    How you implement this structure, would highly depend on things like can ClassOne stand on its own? 

    To give another example: Let's look of the case with shapes...

    public abstract class Shape
    {
    	public string PropertyThatIsCommonToAllShapes1{ get; set; }
    	public int PropertyThatIsCommonToAllShapes2 { get; set; }
    }

    I shape cannot create a shape. A shape by itself, has no meaning.... But something like a Square would

    public class Square : Shape
    {
    	public string PropertyThatIsOnlyCommonToASquare2 { get; set; }
    }

    You could also implement this using interfaces, but I think by doing it this way would have a bit more of "joining" feel to it, as the two properties in the abstract base class will automatically be represented in the inheriting (child) class.

     
    • Proposed as answer by Kristin Xie Friday, May 13, 2016 9:30 AM
    • Marked as answer by DotNet Wang Monday, May 16, 2016 12:39 PM
    Friday, May 6, 2016 7:57 PM

All replies

  • Classes can share things between each other by using an Interface, a contract between two classes.

    http://www.csharp-station.com/Tutorial/CSharp/Lesson13

    Friday, May 6, 2016 4:23 PM
  • Well, I think I might need a bit of further clarification on what you mean by join... but, it seems that you're looking to create an inheritance hierarchy.  The part that's a bit unclear in particular is  "ClassOne 'join' ClassTwo" and then "ClassTwo 'join' ClassOne". If we were to replace the word join with inherits and the words One and Two with Parent and Child respectively, we would have a bit of a problem. However, if you're truley looking for a "join," then the closest thing would be a partial class, but partial classes would have to have the same name. But assuming a hierarchical structure...

    How you implement this structure, would highly depend on things like can ClassOne stand on its own? 

    To give another example: Let's look of the case with shapes...

    public abstract class Shape
    {
    	public string PropertyThatIsCommonToAllShapes1{ get; set; }
    	public int PropertyThatIsCommonToAllShapes2 { get; set; }
    }

    I shape cannot create a shape. A shape by itself, has no meaning.... But something like a Square would

    public class Square : Shape
    {
    	public string PropertyThatIsOnlyCommonToASquare2 { get; set; }
    }

    You could also implement this using interfaces, but I think by doing it this way would have a bit more of "joining" feel to it, as the two properties in the abstract base class will automatically be represented in the inheriting (child) class.

     
    • Proposed as answer by Kristin Xie Friday, May 13, 2016 9:30 AM
    • Marked as answer by DotNet Wang Monday, May 16, 2016 12:39 PM
    Friday, May 6, 2016 7:57 PM
  • It really depends on why you want this. Perhaps you might explain why there are four classes in the first place and what would you gain from what is being asked.

    Please remember to mark the replies as answers if they help and unmark them if they provide no help, this will help others who are looking for solutions to the same or similar problem. Contact via my Twitter (Karen Payne) or Facebook (Karen Payne) via my MSDN profile but will not answer coding question on either.
    VB Forums - moderator
    profile for Karen Payne on Stack Exchange, a network of free, community-driven Q&A sites

    Friday, May 6, 2016 8:19 PM