Global access to arrays
Hi,
I am a beginner at C# and am probably trying to run before I can walk! However, I need to build an application which has several Forms. I was brought up on the concept of only writing code once and using it everywhere. So,
What I need to be able to do is to:
- Hold data arrays in one piece of code, say MyData.CS and access it from all of my Forms.
- Hold pieces of code,( like subs in VB ) which perform specific tasks and which are, again, accessible from all my forms. these may access the API or just do general stuff. I can write the code, but I just can't access it.
So far, I have been able to handle single point variables in this way, but I don't seem to be able to do the same with arrays. Also, I can never access my 'global' code.
Am I missing something?
Many thanks in antisiaption.
John Woodiwiss
Both tasks are implemented in C# using static members and functions.
class Class1
{
public static int[] array = new int[100];
public static void Function()
{
...
}
}
You can access these members from any place of the program, without creating Class1 instance: Class1.array, Class1.Function().
you are right: having same code written several times should fire red light...
about your question:
- You can have on static class with static property. Thus you can change it from everywhere
- You can have also static methods (as subs module in VB) - so you can call them from everywhere without having class reference - it is up to you
hope this helps
Holding all your data in one place and using that data from everything is not the same thing as writing code once and using that code from everywhere.
Indeed, it's arguably the opposite. If you have a globally shared data structure that's used throughout your program, this suggests that code whose job it is to work with that data is probably scattered throughout your program rather than being all in one place.
However, ignoring that issue, arrays aren't any different from other variables, so I'm not really sure why you're seeing problems. Here's an example of globally available data of both kinds:
public class BadDesignWithGlobalData
{
public static int SingleDataItem;
public static int[] ArrayOfData = new int[100];
}
You can then use the array anywhere from your code using this kind of syntax: BadDesignWithGlobalData.ArrayOfData[42] = 99;
You might look at that and think I was wrong when I said that arrays aren't any different from other data types: clearly there's a difference here in that I'm having to use the 'new' operator to create the array and I don't have to do that with the int.
This is true, but that's not a feature of arrays. That's a feature of any object. (More technically, any 'reference type'.) If you want to use an object, first you have to create one. Arrays are objects in .NET, so you need to create them. The reason we don't need to do that for int is that int is a special case: it's a 'value type'. Value types don't need to be instantiated with new.
The reason for this is that when you declare a variable or field of reference type (such as my BadDesignWithGlobalData.ArrayOfData field) what you're declaring is a thing able to hold a reference to an object of a particular type (a reference to an array in this case). The variable or field is not the thing, it's a place to store a reference to a thing. But with value types, the variable or field is the value.
So with arrays, or any other kind of object, you need to create an object for the variable to refer to before you can use it. Hence the difference in syntax here.
But arrays aren't special as far as making them accessible globally is concerned. They work much like any other object.
Alex,
Many thanks for your prompt help. I will try this when I get back onto the project tomorrow.
John
Hi,
any thanks for your prompt reply. For small amounts of data, this would be a good solution, but I am looking at dta asets that are 250K points plus! Not a good way of using Access!
Thanks for the links, I will check them out tomorrow.
John Woodiwiss
Hi,
Many thaks for the prompt response.
In some ways you are correct about the data handling. However, when seperate forms are needed to acquire, graph and store data, then a common data array with global access is the only way that my tiny mind can handle it!
Your code is very sueful, may I ask a secondary question? If it is not known how big the array needs to be until at runtime, is there a way of dimensioning the array from one of the Forms?
Many thanks
John Woodiwiss
Sure - just do put this code in the form in question:
BadDesignWithGlobalData.ArrayOfData = new int[requiredSize];
It doesn't matter who creates the array so long as someone does it. The reason I put the initialization into the class itself was to make sure it was always guaranteed to have been initialized. (That, plus the idea of one class initializing static fields in a different class makes me feel slightly ill... But I think we already established that we have divergent tastes when it comes to the design of the code.)
Another alternative is to use a resizeable collection. Arrays in .NET have a fixed size. But sometimes it's useful to be able to change the number of elements at runtime. And you could do that by creating a new array of the required size, and copying the old array's contents in there. But it's much easier just to use the generic List class:
public class BadDesignWithGlobalData
{
public static List<int> MyData = new List<int>();
}
You can then do BadDesignWithGlobalData.MyData.Add(42) to add a new number to the list, and it will grow in size automatically to accommodate the new item. (This List class is defined in the System.Collections.Generic namespace by the way. Visual Studio usually adds a 'using' statement for that namespace to your files for you.)