tags:

views:

27

answers:

3

Please help! I don't know if maybe this is too simple but I can't get it right and can't seem to find the right answer on other posts. I am relatively new to C# and I program for hobby only, this is my first post.

I have this method CreateMap() inside a class Met where I have all my methods. It creates an array of dynamic custom controls (stockBox[]) and adds them to a table layout panel (boxContainer) which is inside Form. This method runs when the application opens. I get everything to work fine, THE ISSUE is that I want to access the stockBox controls from another method inside the same class I can't see stockBox with IntelliSense and it says it doesn't exist!

public static class Met
{
    public static StockBox[] CreateMap(string[] stock, TableLayoutPanel boxContainer)
    {
        StockBox[] stockBox = new StockBox[Var.stockCount + 1];

        for (int i = 1; i <= Var.stockCount; i++)
        {
            stockBox[i] = new StockBox();
            stockBox[i].StockText = stock[i];
            boxContainer.Controls.Add(stockBox[i]);
        }

        return stockBox;
    }
}

Can't place StockBox[] stockBox = new StockBox[Var.stockCount + 1] outside the method because then Var.stockCount is 0 and it will create an array with only one object.

Any ideas? What do you think I can do? Thanks in advance.

A: 

Make sure that StockBox is not inside the class met, and it is explicitly declared public if it is. If StockBox is inside met and public, then access it by calling Met.StockBox.

macfanpro
I did what rerun and Lee Sy En said and it worked, if my methods that use stockBox are all inside the Met class, now that I've done this then I don't need to return stockBox right?
VerizonW
A: 

There is quite a few ways to fix the problem. You can't access any variable defined inside of a function do the scope of the variable. Scope is concept which allows for variables to exist with in limited sections of your code. In your case you could simply move you control array declaration to the class level. You will however have to make it static since you are accessing it from a static class

public static class Met
{
    Static StockBox[] = null;
    public static StockBox[] CreateMap(string[] stock, TableLayoutPanel boxContainer)
    {

        stockBox = new StockBox[Var.stockCount + 1];
        for (int i = 1; i <= Var.stockCount; i++)
        {
            stockBox[i] = new StockBox();
            stockBox[i].StockText = stock[i];
            boxContainer.Controls.Add(stockBox[i]);
        }

        return stockBox;
    }
} 
rerun
Thank you it worked!
VerizonW
A: 

Your stockBox is currently n variable in CreateMap(). stockBox will be disposed when the method ends (out of scope). Therefore, you need to move the stockBox to class level as follow:

public static class Met
{
    static StockBox[] stockBox = null;
    public static StockBox[] CreateMap(string[] stock, TableLayoutPanel boxContainer)
    {
        stockBox = new StockBox[Var.stockCount + 1];

        for (int i = 1; i <= Var.stockCount; i++)
        {
            stockBox[i] = new StockBox();
            stockBox[i].StockText = stock[i];
            boxContainer.Controls.Add(stockBox[i]);
        }

        return stockBox;
    }

    public static AccessStockBox()
    {
        if (stockBox != null)
        {
            //you should be able to access stockbox here
        }
    }
}
Lee Sy En
Thank you it worked!
VerizonW
If my methods that use stockBox are all inside the Met class, now that I've done this then I don't need to return stockBox right?
VerizonW
Yes. You don't need to return stockBox too for your CreateMap() method. Just declare it as public static void CreateMap(string[] stock, TableLayoutPanel boxContainer)
Lee Sy En