Jaquarh
Jaquarh

Reputation: 6673

Class Properties are inaccessible

I currently have a Class that firstly looks like this:

public class Controller
{

    public class Configuration
    {

        // Properties of the Database
        public string Name { get; set; }
        public string BusinessID { get; set; }
        public string Address { get; set; }

    }

I then have a few functions that actually bind data to the properties - which works fine. However, I cannot seem to find any documentation on accessing the class properties. I have tried:

Controller ctrl;
ctrl = new Controller();

However, ctrl does not hold any of these values/properties; neither does it hold the Configuration class.

Is there something wrong with this structure which is why Configuration is inaccessible or could anyone possible help me find a solution to obtaining the Name, BusinessID and Address ?

EDIT: I want to obtain the properties outside the class in the aspx.cs file.

Thanks in advance.

Upvotes: 1

Views: 263

Answers (6)

Giorgi Moniava
Giorgi Moniava

Reputation: 28654

If I correctly understood your use case, what is below is one way to go.

You can create an instance for the second class in this way:

Controller.Configuration ac = new Controller.Configuration();

Upvotes: 3

Steve
Steve

Reputation: 216243

Class are just blue-prints for your actual instances. Your code defines two classes but the enclosing one (Controller) doesn't have a property to access the enclosed inner class (Configuration)

If you want to set/get the properties of an instance of a Configuration class that belongs to an instance of a Controller class you should declare a member property in the Controller class of type Configuration

public class Controller
{

    public class Configuration
    {

        // Properties of the Database
        public string Name { get; set; }
        public string BusinessID { get; set; }
        public string Address { get; set; }

    }

    // If you don't want external code set the internal instance remove the set method 
    public Configuration ctrlConfig {get;set;}
    public Controller()
    {
         // Remember to initialize the inner instance of the configuration
         ctrlConfig = new Configuration()
    }
}

And now somewhere else

Controller ctrl;
ctrl = new Controller();
ctrl.ctrlConfig.Name = "YourConfigName";

Upvotes: 3

Dinalan
Dinalan

Reputation: 193

try this:

public class Controller
{
    Configuration c;

    public class Configuration
    {

        // Properties of the Database
        public string Name { get; set; }
        public string BusinessID { get; set; }
        public string Address { get; set; }

    }

}

then you will be able to acces those values:

  Controller ctrl = new Controller();
            ctrl.c = new Configuration();

Upvotes: 2

JNF
JNF

Reputation: 3730

For it to work there are two possibilities (it's unclear from the question which you were expecting)

1. Insantiate

You need an instance of the Configuration class, and initialize it in the constructor

public Configuration Configuration_Instance {get;set;}

public Controller() {
  Configuration_Instance = new Configuration();
}

and use

ctrl.Configuration_Instance.Name //etc.

2. Use static

make Configuration static

public static class Configuration {
    public static string Name { get; set; }
    public static string BusinessID { get; set; }
    public static string Address { get; set; }
}

and use

ctrl.Configuration.Name //etc.

Upvotes: 0

Othello  .net dev
Othello .net dev

Reputation: 438

You have defined a Class (Controller) and a inner class (Controller.Configuration)

So the properties you defined are domain of class Controller.Configuration and not of controller.

So you should:

// istantiate a controller.configuration instance
Controller.Configuration p = new Controller.Configuration();
// and use his properties
p.Name = "test";

Otherwhise you can create a property of type Configuration in Controller and you can use that..

public class Controller
{
    public Controller()
    {
        Config = new Configuration();
    }
    public class Configuration
    {

        // Properties of the Database
        public string Name { get; set; }
        public string BusinessID { get; set; }
        public string Address { get; set; }

    }

    public Configuration Config { get; set; }

}


// istantiate a controller.configuration instance
Controller cont = new Controller();
// and use his properties
cont.Config.Name = "test";

Upvotes: 1

Jeroen
Jeroen

Reputation: 63698

I think you're aiming for this setup:

public class Controller
{
    public Controller()
    {
        this.Config = new Configuration();
    }

    public Configuration Config { get; set; }
}

public class Configuration
{
    public string Name { get; set; }
    public string BusinessID { get; set; }
    public string Address { get; set; }
}

Then you can do this:

Controller ctrl;
ctrl = new Controller();
Console.WriteLine(ctrl.Configuration.Name);

In your original code there is a class defined inside another class. In my example Controller has a property of type Configuration, which is instantiated in the constructor (public Controller()).

Upvotes: 1

Related Questions