Using properties from a nested class

G

Guest

How is it possible to us a property from a nested class.

Example:

namespace MyApp
{
public class MaxLength
{
public class Project
{
private const int _projectNumber = 3;
private const int _projectName = 20;

public int ProjectNumber
{
get { return _projectNumber; }
}

public int ProjectName
{
get { return _projectName; }
}
}
}
}

I want to use this object in my console application.
MyApp.MaxLength.Project.ProjectNumber

The properties in this nested subclass are not visible why??They are
declared as public.
 
G

Guest

Tested code in Visual Studio 2005. It works.
class Test
{
public void foo()
{
MaxLength.Project p = new MaxLength.Project();
Console.WriteLine(p.ProjectName);
}
}
 
N

Nicholas Paldino [.NET/C# MVP]

Marc,

A nested class is nothing more than a type definition inside a
containing class. It does not mean that instances of the parent class
expose an instance of the nested type as properties.

If you want to expose a nested class like that, you have to create a
property in the parent class and expose an instance of the nested class
(like any other property/class, really).

Hope this helps.
 
G

Guest

Is see what you meam, do you have an example, or a link to, how to expose the
property.

Nicholas Paldino said:
Marc,

A nested class is nothing more than a type definition inside a
containing class. It does not mean that instances of the parent class
expose an instance of the nested type as properties.

If you want to expose a nested class like that, you have to create a
property in the parent class and expose an instance of the nested class
(like any other property/class, really).

Hope this helps.


--
- Nicholas Paldino [.NET/C# MVP]
- (e-mail address removed)

Marc Hoeijmans said:
How is it possible to us a property from a nested class.

Example:

namespace MyApp
{
public class MaxLength
{
public class Project
{
private const int _projectNumber = 3;
private const int _projectName = 20;

public int ProjectNumber
{
get { return _projectNumber; }
}

public int ProjectName
{
get { return _projectName; }
}
}
}
}

I want to use this object in my console application.
MyApp.MaxLength.Project.ProjectNumber

The properties in this nested subclass are not visible why??They are
declared as public.
 
N

Nicholas Paldino [.NET/C# MVP]

Marc,

It's like any other property, you create a get and a set accessor. In
your parent class, you would probably have a field which will store an
instance of the child class.


--
- Nicholas Paldino [.NET/C# MVP]
- (e-mail address removed)

Marc Hoeijmans said:
Is see what you meam, do you have an example, or a link to, how to expose
the
property.

Nicholas Paldino said:
Marc,

A nested class is nothing more than a type definition inside a
containing class. It does not mean that instances of the parent class
expose an instance of the nested type as properties.

If you want to expose a nested class like that, you have to create a
property in the parent class and expose an instance of the nested class
(like any other property/class, really).

Hope this helps.


--
- Nicholas Paldino [.NET/C# MVP]
- (e-mail address removed)

message
How is it possible to us a property from a nested class.

Example:

namespace MyApp
{
public class MaxLength
{
public class Project
{
private const int _projectNumber = 3;
private const int _projectName = 20;

public int ProjectNumber
{
get { return _projectNumber; }
}

public int ProjectName
{
get { return _projectName; }
}
}
}
}

I want to use this object in my console application.
MyApp.MaxLength.Project.ProjectNumber

The properties in this nested subclass are not visible why??They are
declared as public.
 
A

Alan Samet

public class OuterClass
{
public class InnerClass
{

}
public InnerClass MyInnerClass
{
get {return new InnerClass();}
}
}
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top