Nested classes

  • Thread starter Thread starter Jof
  • Start date Start date
J

Jof

Can anyone help with this problem I'm having.

What I want to do is to create a class (maybe it should be a struct)
which does nothing but hold data. It doesn't have any methods etc..


public class ShippingOptionsDetails
{
public static class SaturdayDelivery
{
public decimal Price;
public int DeliveryID;
}

public static class EuropeanDelivery
{
public decimal Price;
public int DeliveryID;
}

}

What I want to be able to do is to do this...

public ShippingOptionsDetails MyFunction()
{
ShippingOptionsDetails options = new ShippingOptionsDetails();
options.SaturdayDelivery.Price = 3.00;

Return options;
}

However, you cannot do this. C# wants me to:

ShippingOptionsDetails.SaturdayDelivery options = new
ShippingOptionsDetails.SaturdayDelivery();

But I want to return the holding class and not the nested class.

How do I do this?

Many thanks

Jonathan
 
Nested classes are just a scoping mechanism, nothing more. You still
need to create an instance of each of your nested classes as fields on your
SOD...

public class SOD {
public SaturdayDelivery SaturdayDelivery = new SaturdayDeliver();
public EuropeanDelivery EuropeanDeliver = new EuropeanDelivery();

.... Define nested classes stilll ...
}

You may still get some namespace scoping issues with this method, so there might
be more work yet to do. The reason is that the field names might conflict with
the
nested class names. If they do, then you can rename the fields something like:

SaturdayDelivery SaturdayDeliveryOptions = new SaturdayDelivery();
 
Back
Top