Go Back   AnandTech Forums > Software > Programming

Forums
· Hardware and Technology
· CPUs and Overclocking
· Motherboards
· Video Cards and Graphics
· Memory and Storage
· Power Supplies
· Cases & Cooling
· SFF, Notebooks, Pre-Built/Barebones PCs
· Networking
· Peripherals
· General Hardware
· Highly Technical
· Computer Help
· Home Theater PCs
· Consumer Electronics
· Digital and Video Cameras
· Mobile Devices & Gadgets
· Audio/Video & Home Theater
· Software
· Software for Windows
· All Things Apple
· *nix Software
· Operating Systems
· Programming
· PC Gaming
· Console Gaming
· Distributed Computing
· Security
· Social
· Off Topic
· Politics and News
· Discussion Club
· Love and Relationships
· The Garage
· Health and Fitness
· Merchandise and Shopping
· For Sale/Trade
· Hot Deals with Free Stuff/Contests
· Black Friday 2014
· Forum Issues
· Technical Forum Issues
· Personal Forum Issues
· Suggestion Box
· Moderator Resources
· Moderator Discussions
   

Reply
 
Thread Tools
Old 01-22-2013, 08:57 AM   #1
Chaotic42
Lifer
 
Chaotic42's Avatar
 
Join Date: Jun 2001
Location: Mississippi Gulf Coast - 16RBU
Posts: 29,678
Default Adding properties to a class in C#

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
__________________
Complex numbers are all fun and games until someone loses an i.
Pictures
Chaotic42 is offline   Reply With Quote
Old 01-22-2013, 09:19 AM   #2
Crusty
Lifer
 
Crusty's Avatar
 
Join Date: Sep 2001
Location: ATX
Posts: 12,323
Default

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.
__________________
Consequences will never be the same!

/^1?$|^(11+?)\1+$/
Crusty is offline   Reply With Quote
Old 01-22-2013, 09:38 AM   #3
Chaotic42
Lifer
 
Chaotic42's Avatar
 
Join Date: Jun 2001
Location: Mississippi Gulf Coast - 16RBU
Posts: 29,678
Default

Quote:
Originally Posted by Crusty View Post
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.
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?
__________________
Complex numbers are all fun and games until someone loses an i.
Pictures
Chaotic42 is offline   Reply With Quote
Old 01-22-2013, 09:51 AM   #4
Munashiimaru
Junior Member
 
Join Date: Jan 2013
Posts: 23
Default

Quote:
Originally Posted by Chaotic42 View Post
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?
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.
Munashiimaru is offline   Reply With Quote
Old 01-22-2013, 10:12 AM   #5
MrChad
Lifer
 
MrChad's Avatar
 
Join Date: Aug 2001
Posts: 13,499
Default

Quote:
Originally Posted by Chaotic42 View Post
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?
It sounds like what you want is a base class, not an interface.

http://csharp-station.com/Tutorial/CSharp/Lesson08
MrChad is offline   Reply With Quote
Old 01-22-2013, 10:16 AM   #6
sze5003
Diamond Member
 
sze5003's Avatar
 
Join Date: Aug 2012
Posts: 7,035
Default

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.
__________________
Asrock Extreme3 Gen3| Core i5 2500k @4.2Ghz-Hyper212 EVO| 8GB Corsair Vengence 1600 | 1TB Seagate 7200 rpm | 256GB Samsung 830 |128GB Crucial M4 | Saphire VaporX 7970Ghz 3GB | NZXT Phantom 410 | 750W Corsair Enthusiast
sze5003 is online now   Reply With Quote
Old 01-22-2013, 12:11 PM   #7
Markbnj
Moderator
Programming
 
Markbnj's Avatar
 
Join Date: Sep 2005
Posts: 12,884
Default

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.
__________________
Everytime I try to tell you, the words just come out wrong

**
Some meaningless scribbling of no account

The 4th Realm

Arts and Letters Daily - Get some culture
Markbnj is offline   Reply With Quote
Old 01-24-2013, 06:23 PM   #8
Mark R
Diamond Member
 
Mark R's Avatar
 
Join Date: Oct 1999
Posts: 8,283
Default

Quote:
Originally Posted by Chaotic42 View Post
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?
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).
Mark R is offline   Reply With Quote
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump


All times are GMT -5. The time now is 12:15 PM.


Powered by vBulletin® Version 3.8.7
Copyright ©2000 - 2014, vBulletin Solutions, Inc.