简体   繁体   中英

C# - How to return an Array to set a Class property

I am trying to create a Class Method which can be called to Query the Database. The function itself works but for some reason, when the Array is returned, they're not set .

My function code is:

public Configuration[] tbl_bus(string type, string match)
{

    // Create Obejct Instance
    var db = new rkdb_07022016Entities2();
    // Create List
    List<Configuration> ConfigurationList = new List<Configuration>();

    // Allow Query
    if (type.ToLower() == "bustype")
    {
        foreach (var toCheck in db.tblbus_business.Where(b => b.BusType == match))
        {
            // Create Class Instance
            var model = new Configuration { Name = toCheck.Name, BusinessID = toCheck.BusinessID };
            // Append to the property
            ConfigurationList.Add(model);
        }
    }
    else if (type.ToLower() == "businessid")
    {
        foreach (var toCheck in db.tblbus_business.Where(b => b.BusinessID == match))
        {
            // Create Class Instance
            var model = new Configuration { Name = toCheck.Name, BusinessID = toCheck.BusinessID };
            // Append to the property
            ConfigurationList.Add(model);
        }
    }

    return ConfigurationList.ToArray();

}

And my Configuration code is:

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; }

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

On my Handler I am doing:

// Inside the NameSpace area
Controller ctrl;

// Inside the Main Void
ctrl = new Controller();
ctrl.tbl_bus("bustype", "CUS");
context.Response.Write(ctrl.Config.Name);

I tried watching the Class function and it does create the Array, only, when I watch the ctrl.Config.Name it is always set to NULL . Could anyone possibly help me in understanding why the return isn't actually setting the properties inside the Configuration class?

Edit: The function does run and it fetches 3006 rows of Data when matching the bus_type to customer. (Its a large Database) - Only, the properties are never set on return .

Edit: Is there a specific way to return an Array to a Class to set the Properties?

Thanks in advance!

Change your Configs in Controller to array

public Configuration[] Configs { get; set; }

Change your tbl_bus function to void, and set the Configs inside the function.

public void tbl_bus(string type, string match)
{
    // do your code
    // set the configs here
    Configs = ConfigurationList.ToArray();
}

Hope it helps.

Although this is not a complete answer to your question, the problem probably lies in the fact that you're not doing anything with the array returned by the method. You're simply discarding it right away. If you change your code to

ctrl = new Controller();
Configuration[] config = ctrl.tbl_bus("bustype", "CUS");

you will be able to reference the array later on.

Console.WriteLine(config.Length);

Now you can use it to set any properties you like.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM