2

I have object Item, and it has Data Object in it. In order to access Data's properties I use the following code:

Item.Data.PropertyName

Is any way in C# to access Data's properties in the following way:

Item.PropertyName

without copy properties to "Item" object?

Example of Item class:

class Item{

    public DataObject Data;
    public AnotherDataObject Data1;
    public AnotherDataObject Data2;
}
class DataObject{
    public int Property1;
    public int Property2; 
    .....
}
class DataObject1{.....}
......

other DataObjects classess similar to DataObject Implementation

Boris
  • 119
  • 2
  • 11
  • If you explain why you would want to do that there could be another solutions. – Andrew Savinykh Jul 28 '13 at 19:50
  • @Boris can you provide the full implementation of your `Item` class (the relevant parts with `Data`)? – Erik Philips Jul 28 '13 at 19:54
  • I tryed to make it simple as possible. The true problem is that I have Item object with a lot of Child objects (Data1,Data2,Data3...) and each has many properties. – Boris Jul 28 '13 at 19:56
  • I do not see any correlation between `Item` and `DataObject`. Neither object has a property type of the other type. – Erik Philips Jul 28 '13 at 20:11
  • @ErikPhilips I just need to use Item.Property1 and not Item.Data.Property1. All these "Data" objects come from database structure, It has no sense in business logic. – Boris Jul 28 '13 at 20:17
  • 1
    I think you're looking for something like Traits in Scala. C# doesn't have anything like that. – Dax Fohl Jul 29 '13 at 01:30

1 Answers1

6

Yeah by having wrapper property in Item class which will return PropertyName of Data class -

public string PropertyName
{
   get
   {
      return this.Data.PropertyName;
   }
   set
   {
      this.Data.PropertyName = value; 
   }
}

This way you can write Item.PropertyName.

Rohit Vats
  • 79,502
  • 12
  • 161
  • 185
  • To make it clear that I am accessing a property on the object itself, I typically prefix with `this` for clarity, in your example it would be `return this.Data.PropertyName;`. It makes it absolutely clear to other developers where `Data` came from. – Erik Philips Jul 28 '13 at 19:46
  • 1
    This solution is obvious.My problem is that "Data" object has a lot of properties , and I have a lot of "Data"'s objects . So I can't just rewrite all properties of Data in "Item" object. – Boris Jul 28 '13 at 19:48
  • @Boris, is your `Data` a `private` or `public` property on Item? – Erik Philips Jul 28 '13 at 19:52
  • @Boris - You said you have `Data` object? Is it `List` or what? – Rohit Vats Jul 28 '13 at 19:53
  • 2
    You don't have to prefix the property name with `this`. This is redundant and just clutters the code. In fact Resharper, the leader in code assistance, by default mark these as ones that could use refactoring. Given that in modern age we have things such as intellisense and code navigation as long as we adhere to sane naming conventions there is simply no need for this. – Andrew Savinykh Jul 28 '13 at 19:55
  • @ErikPhilips - let's assume that all props are public – Boris Jul 28 '13 at 20:04
  • 1
    @Boris What does `MyObject.PropertyName` solve that `MyObject.Data.PropertyName` wouldn't? – Erik Philips Jul 28 '13 at 20:05
  • Let's say Item is "Person" and Data object holds Address data, so I need to acccess to Street property with Person.Street and not Person.Address.Street.I have a lot of "Data" objects with a lot of properties in each. I can't remember for what "Data" object belongs each Property. The real problem is much more difficult, I can't explain all here – Boris Jul 28 '13 at 20:06
  • @zespri or any future readers, if you have any question on the [this keyword check out this SO answer](http://stackoverflow.com/questions/23250/when-do-you-use-the-this-keyword). As I stated it was my own personal tastes and I'm not one to argue the merits of code readability and maintainability when the OP question isn't asking about that. – Erik Philips Jul 28 '13 at 20:10
  • 1
    It sounds like you need a lot more complex parent class to do the work for. If you make the property Street on your Person then your getter could do the work for you to get the value from your data objects. It that doesn't work then you need to most likely reevaluate your objects so that they actually are what you say they are. You could just make a single Person.GetProperty() function that you pass the property name in and then return the value from your Data objects in your Person class. – Nick H Jul 28 '13 at 20:50
  • Thaks all, I thought there is some C# language feature I don't know. But I think I wrong – Boris Jul 28 '13 at 21:10