Using properties from a nested class

  • Thread starter Thread starter Guest
  • Start date Start date
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.
 
Tested code in Visual Studio 2005. It works.
class Test
{
public void foo()
{
MaxLength.Project p = new MaxLength.Project();
Console.WriteLine(p.ProjectName);
}
}
 
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.
 
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.
 
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.
 
public class OuterClass
{
public class InnerClass
{

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