tags:

views:

66

answers:

3

I want to create a class so I can use it like this:

Website.Urls.User.Edit
Website.Urls.User.Add
Website.Urls.Content.List

How can I do this in c#? (they will return strings)

+3  A: 
public class UserClass
{
  public string Edit {get;set;}
  public string Add {get;set;}
}

public class UrlsClass
{
  public UserClass User {get;set;}
}

public class Website
{
  public UrlsClass Urls {get;set;}
}

For what do you need that?

Femaref
If either Urls or User is null, you'll throw an exception.
Cylon Cat
@cylon : your words is right but what;s the problem in that ? if you don't need for creating objects you can make classes and its property static
Space Cracker
@Space, the solution is to "lazy load" those properties. If "User" is null, then create a new user, or load a user from the database, or whatever is appropriate. If it's OK for those properties to be null, then the original poster's coding style for accessing properties will need to be changed.
Cylon Cat
+1  A: 

If the properties are classes, then you can access the properties of the referenced class. Some would argue that this violates the principle of information-hiding, but I think that somewhat depends on the case. For instance, in an ORM (object relational mapping), a property might be a class mapping a foreign key reference, and that's (possibly) a little better than some other circumstances.

Anway, this would not be a case for implicit properties. Use a declared private field for the property to reference, and check it for null before returning it. If null, then fill the property and return.

Cylon Cat
+1  A: 

It seems to me that you should rethink your architecture as this deepy nested structure implies clumsy design. For further information, see the Law of Demeter.

Marius Schulz
its just to manage urls, and have a friendly api....
Blankman
You will receive exceptions if one of the first three values equals `null` ...
Marius Schulz
A *.sitemap basically follows this "deeply nested structure". You could probably use a T4 template in VS 2010 to generate this set of classes automatically from a sitemap.
Nelson