0

I have a static array which uses a static variable called Variables.rows to define the number of rows in the array. Variables.rows is defined in another static class called Variables.

public static class TheArrayClass
{
    public static double[,] HiThere = new double[Variables.rows, 6];
}

My problem is that the static array is created as soon as the project in run (I believe). This means that the methods needed to assign the correct value to Variables.Rows are not executed in time. This means that I get an index error when populating the array because the array does not have the correct size.

I need a way around this so that I can access the array from anywhere in my code please.

6
  • Where is Variables.Rows defined? Commented Feb 19, 2013 at 15:23
  • 1
    And where does the rows property come from, why does the array need to be static? Commented Feb 19, 2013 at 15:24
  • 1
    HORRIBLE idea to use global variables. Chances are if you're asking how to do it you're new to the language - keep reading, find a way to not use global variables Commented Feb 19, 2013 at 15:25
  • 1
    @user1920206 Then pass the data to the form when it is create. Commented Feb 19, 2013 at 15:27
  • @StenPetrov That is not entirely true, but I can think why you are saying that. Commented Feb 19, 2013 at 15:34

3 Answers 3

2

Try message passing between your forms.

public partial class Form1 : Form
{
    private string _data;

    public Form1()
    {
        InitializeComponent();
        _data = "Some data";
    }

    private void button1_Click(object sender, EventArgs e)
    {
        var form2 = new Form2();
        form2.Data = _data;
        form2.Show();
    }
}

public partial class Form2 : Form
{
    public Form2()
    {
        InitializeComponent();
    }

    public string Data { get; set; }
}
Sign up to request clarification or add additional context in comments.

4 Comments

Where is he talking about forms?
@lolol Check the comments on the question. He want to use global static variables to pass data between his forms which is bad practice.
I can't find the comment but I trust you, I will change my downvote. Sorry.
@lolol He must have deleted it because my reply is still there. :/
1

It's interesting that you didn't show where Variables.Rows was defined. Either way, you can perform whatever initialization you want in the classe's constructor.

public static class TheArrayClass
{
    public static double[,] HiThere;

    static TheArrayClass()
    {
         HiThere = new double[Variables.rows, 6];
    }
}

3 Comments

Since your class is static, a constructor won't work. You will need to create a type initializer (static TheArrayClass()). Otherwise, this looks fine.
@p.s.w.g Whoops! Forgot the static keyword.
@Johnathan-Wood: Type initializers don't support access modifiers. Drop the public and you're good.
0

I think you have a design problem, so I'm gonna try to add some information that can help you to solve your problem.

First, a static class is, in fact, created as soon as the project runs. But a static class can also have a static constructor (where you can define the variables.rows) that will also runs one time as soon as the project runs.

Also, maybe you should use an ArrayList (http://msdn.microsoft.com/en-us/library/system.collections.arraylist.aspx) or GenericList (http://msdn.microsoft.com/en-us/library/6sh2ey19.aspx), by doing that you can easily avoid the array variable limitations (like changing its size).

4 Comments

Yeah, everyone says that global-anything is awful, but unfortunately at the moment I do not have the time to read up much new documentation. Thanks for the links though.
Man, it is not. Global are dangerous, and because they are dangerous people prefer to avoid it, but they are there for a reason, and the reason is: they are useful. But still, I think there is something wrong in your project.
Please don't suggest that new users use an ArrayList. It shouldn't ever be used by anyone, unless they're forced to as a result of using legacy code. You should always use List instead. Also note that static classes are not created when the project is first run, nor are static constructors run then. Static constructors are run just before the class is first used, which is when the static class is first initialized.

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.