0

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!

4
  • You seem to have a few issues with the code. There is nothing in tbl_bus which modifies the containing class. Also you aren't doing anything with the return array from to the call to tbl_bus. And there's nothing in your code that lets us know that b.BusType == match or b.BusinessID == match every succeed. We can't possibly answer your question without an minimal reproducible example. Commented Mar 3, 2016 at 11:37
  • tbl_bus is the method the handler calls, the method (is meant to) return an array of a query to a separate Class to set the values of each. But I don't see where I am going wrong because I have specified that I want to return it to the Configuration Class?? Commented Mar 3, 2016 at 11:39
  • Where have you specified to return it to the Configuration class? Commented Mar 3, 2016 at 12:17
  • I haven't, that was the issue. I thought that declaring the controller and then using the return method to the controller would automatically set it for me, but like the answers showed: It only returns it to the called method. Commented Mar 3, 2016 at 12:52

2 Answers 2

3

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.

Sign up to request clarification or add additional context in comments.

1 Comment

That makes clear sense, reading both yours and @Kapols answers, I can see where I am going wrong. This has provided me with how to achieve what I am trying to do though, thanks!
2

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.

5 Comments

So the return itself isn't setting the Configuration properties? Is there any references that you could send me that could show me how to directly set the properties rather than return them to where the method was called?
return is just a C# keyword that terminates execution of the method
Right, so even if I didn't void the method and declared the class, it still would return it to where it was called?
@KyleE4K The execution returns to the caller regardless of the return type.
I can't believe I didn't know that, I appreciate that!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.