Adding properties to a class in C#

Discussion in 'Programming' started by Chaotic42, Jan 22, 2013.

  1. Chaotic42

    Chaotic42 Lifer

    Joined:
    Jun 15, 2001
    Messages:
    31,619
    Likes Received:
    279
    I'm working on a little something in C# and I've run into a problem. This is where having a piecemeal knowledge of the language is an issue, because I don't even know how to ask the question.

    I have an object which I cannot modify called UnModifiedObject. I don't have the code to it. It has 24 foo properties, foo1 through foo24. I would like to add a String property to this and call the new thing ModifiedObject. I thought that this would work:

    Code:
    public class ModifiedObject : UnModifiedObject
    {
        public String FavoriteColor;
    }
    
    ...
    ModifiedObject.foo13 = 13;
    ModifiedObject.FavoriteColor = "Blue";
    But I get a massive number of errors saying "'ModifiedObject' does not implement interface member 'UnModifiedObject.foo1'". I'm taking this to mean that it wants me to create code for each of the dozens of foos. I want ModifiedObject to have everything that UnModifiedObject has plus FavoriteColor. Is this possible? I can't imagine that I would need to tell C# to make foo1=foo1, foo2=foo2, etc. Is there a name for what I'm trying to do? How can I do it?

    Thanks
     
  2. Loading...

    Similar Threads - Adding properties class Forum Date
    Adding together ASCII(h) representation of bytes in a array<Byte> Programming Aug 19, 2017

  3. Crusty

    Crusty Lifer

    Joined:
    Sep 30, 2001
    Messages:
    12,688
    Likes Received:
    1
    From the sounds of it UnModifiedObject is only an interface, so yes you would have to implement all of those. Without seeing the definition of UnModifiedObject, it's hard to say for sure though.

    Even if you can't see the code for something, you can still see the full definition of UnModifiedObject through Visual Studio. You can use either the 'Object Browser' or 'Go to Definition' while right clicking on the name 'UnModifiedObject' in code.
     
  4. Chaotic42

    Chaotic42 Lifer

    Joined:
    Jun 15, 2001
    Messages:
    31,619
    Likes Received:
    279
    Thanks for the quick reply. The actual thing I'm dealing with is indeed an interface with two methods and ten properties. Am I crazy or is it weird that I would have to implement all of these things just to add one little String?

    Anyway, how do I go through all of these methods and properties and tell C# just to basically let them be?
     
  5. Munashiimaru

    Munashiimaru Junior Member

    Joined:
    Jan 14, 2013
    Messages:
    23
    Likes Received:
    0
    Interfaces can not be instantiated. They are not objects that can stand on there own. They're just outlines for how another class that implements it must be.
     
  6. MrChad

    MrChad Lifer

    Joined:
    Aug 22, 2001
    Messages:
    13,507
    Likes Received:
    2
    It sounds like what you want is a base class, not an interface.

    http://csharp-station.com/Tutorial/CSharp/Lesson08
     
  7. sze5003

    sze5003 Lifer

    Joined:
    Aug 18, 2012
    Messages:
    11,359
    Likes Received:
    44
    Yup java is similar. We use interfaces a lot and if you want to use an interface you must also use the other methods too.

    Maybe change it to a base/abstract class.
     
  8. Markbnj

    Markbnj Elite Member <br>Moderator Emeritus
    Moderator

    Joined:
    Sep 16, 2005
    Messages:
    15,688
    Likes Received:
    11
    The whole point of interfaces is that an assembly can expose a public definition of a contract between an implementation and a caller, without specifying the implementation. So the basic problem you've run into is that you want to extend the behavior of their implementation, but you don't know its type. All you have is the interface declaration. The implementation itself is likely internal to the assembly, and for all you know might even be sealed against subclassing. The point is: you're not to know anything about it, by design.

    What do you do in a case like that? A superclass-subclass relationship won't work. You could go spelunking with reflection and build a subclass, but that would be dumb because their internal implementation might change tomorrow for all you know. The best approach in a case like this is not an is-a relationship (superclass-subclass) but rather a has-a relationship (composition). You can extend the behavior of their implementation by wrapping it in your own. That gives you a nice clean line between the wrapped implementation and all of the other code in your system.
     
  9. Mark R

    Mark R Diamond Member

    Joined:
    Oct 9, 1999
    Messages:
    8,508
    Likes Received:
    9
    An interface is merely an indication that a class meets certain minimum requirements. It does not provide any code.

    In your case your UnmodifiedObject interface, merely states that any class that presents an UnmodifiedObject interface must feature foo1...foo24. It contains no code to do that. The compiler is complaining because you have made your class expose an UnmodifiedObject interface, but you have provided no code to make the interface work.

    It sounds like you want to subclass an existing class. In that case you need to find out what the actual type of the class is, and then attempt to subclass it. The easiest way is to run some code where you create an instance of the object of interest, and break into the program with the debugger. Use the debugger to find out the actual type of the object. Once you know that, you can subclass it (provided that the class isn't sealed).

    If the class is sealed, or you want to start from scratch, then you will have to create code for each property. (This is where the visual studio code snippets are gold; properties are such a common task that there is a pre-packed snippet ready for creating the property and it's backing field).