8

I couldn't find any information pertaining to this question. I am trying to create a global array in C# so that I can input information into it at different points in my code and then call the information back at a later time to display it. For example, I want to take the following information and put it into the array:

string auctionID;
string itemName;
string itemID;
string bid;
string buyout;
string quantity;

I then want to be able to call that array with a for loop, or something similar, so that I can display that information at a later time. Now, one little thing that I forgot to mention is that I am probably going to need either an array of arrays or a multi-dimensional array. The reason for this is because I am going to have lots of different auctions and the data about each auction (hence the variables above) and I want to display that information at the very end of the program. Thank you for your time!

Update 1

So, I think I am not making myself clear because I am getting confused, but that could also be because I'm a little frustrated at the moment with this program. I want to be able to create a global array and then get & set the information stored in that array with different functions within my program as different functions will be modifying different parts of that array. Also, I wouldn't mind using caching, if I understood how that worked or even had a link to read about it. One last thing to add, I am doing this on the Windows Phone 7, so I am limited on which libraries and system calls I can use.

Update 2

I am quite a bit rusty on OOP, so I am going to go read up more on it (thanks to dealing a ton with HTML and not really having a chance to do real programming). Thanks for everyone's suggestions. I will probably post back on this topic if I can't figure out something further. Cheers!

th3n3wguy
  • 3,649
  • 2
  • 23
  • 30
  • 1
    No, don't do that. We don't use global arrays these days. – Richard Jan 04 '12 at 21:39
  • Using caching would be a better ideas than global arrays, as you are likely to run into concurrency and multithreading issues. – Kane Jan 04 '12 at 21:40
  • "I don't know how to call the same array each time I make separate calls to the array because, from what I understand, C# treats arrays as objects." That doesn't make any sense. Why do you think arrays being objects means you will not get "the same array each time"? – Kirk Woll Jan 04 '12 at 21:40
  • You don't want to "wrap the array in a class". You need to create an Auction class, and maintain a collection of Auctions that you access at the appropriate point in code. If this data needs to survive across multiple program runs, you need to persist it (database, file, etc.) and load/save it as appropriate. DO NOT create a multidimensional array for this. It's Just Wrong ™. There are a lot of different ways to handle this, the right answer depends on a lot of information you've left out. – Harper Shelby Jan 04 '12 at 21:42
  • You'd better reformulate the question more specifically: what is specifically needed to do. – eigenein Jan 04 '12 at 21:46
  • Why are you interested in losing the semantic information that those variable names provide by stuffing them into a defined-length string array? – 48klocs Jan 04 '12 at 22:01
  • Arrays are considered bad. Global variables are bad. *Global arrays* are .... doesnt get any worse.. Oh may be you want mutable structs in those arrays :P – nawfal Nov 10 '13 at 20:27

8 Answers8

11

I don't suggest you to use global arrays, is not a best practice what you would need is a data repository and take from there the data you want to use, regarding the structure you want to use I suggest you to do something like this:

public class Auction
{
           public string auctionID {get; set;}
           public string itemName {get; set;}
           public string itemID {get; set;}
           public string bid {get; set;}
           public string buyout {get; set;}
           public int quantity {get; set;} 
}

And then use a List of that particular object to access your data.

List<Auction> acutions = new List<Auction>();

Then you can add or remove items as desired.

auctions.Add(auction object);
auctions.remove(auction object);

Or navigate through the list with a foreach loop

foreach (Auction item in auctions)
{
 // your code to handle items here
}
Xtian Macedo
  • 835
  • 5
  • 19
10

Global variables are almost always a bad idea, so C# makes creating them a difficult thing to do. However, if you really want to, you could do something like this:

public static class GlobalData
{
    public static string[] Foo = new string[16];
};

// From anywhere in your code...
Console.WriteLine(GlobalData.Foo[7]);

Note, however, that as long as your are forced to create a static class to do this, why not go ahead and make the actual array variable private, instead of public, then add static, public getter and setter methods? That approach at least removes some of the danger inherent in global variables.

dgvid
  • 26,293
  • 5
  • 40
  • 57
4

In C# there are no global variables. There are only class members. The best way to pass data from one part of your application to another is passing a reference to an object (an instance of a class) that holds all the relevant data in its members.

You can also use static classes, which behave kind-of like global variables, but when you get more familiar with object-oriented programming you'll realize that this is not a good way to handle data.

Ilya Kogan
  • 21,995
  • 15
  • 85
  • 141
2

Well, aside from the fact that you likely have a structural problem if you're storing stuff globally.

You can use a static property for this.

 public class MyVariables
 {
     private static string[] _MyStuff
     public static string[] MyStuff
     {
           return _MyStuff;
     }
 }

Then access it like this:

MyVariables.MyStuff[0]
Darthg8r
  • 12,377
  • 15
  • 63
  • 100
  • You can also declare the class itself as static (`public static class MyVariables`), although this isn't very important - it will just prevent users from pointlessly instantiating an object. – MusiGenesis Jan 04 '12 at 21:42
2

I'd use a Generic like a List as you don't have to worry about size and you can get an iterator for it in a foreach loop.

List<string> values = new List<string>();

to insert an item:

values.Add("item");

to remove:

values.Remove("item");

If you want multi-dimensional then use:

List<List<string>> multi_dimens_values;
Jonathan Henson
  • 8,076
  • 3
  • 28
  • 52
1
string[] values;

Or to declare one of a particular length:

string[] values = new string[6]; // creates an array with a length of 6.
David Morton
  • 16,338
  • 3
  • 63
  • 73
  • Oh, I understand that. The problem is that I don't know what to do about where to place that so that different methods can call that array and use the information inside of it. – th3n3wguy Jan 04 '12 at 21:39
  • Technically correct, but doesn't actually answer the question IMHO. – Harper Shelby Jan 04 '12 at 21:43
  • @thenewguy Please read more about object-oriented programming and design patterns. You're still thinking in non-OOP ways. – Ilya Kogan Jan 04 '12 at 21:49
1

I think Dictionary<string, string> will be more suitable... You can loop through it and access a specific item. And also pass it to your methods.

eigenein
  • 2,083
  • 3
  • 25
  • 43
1

this is a very bad idea.

  1. avoid global variables as much as possible. there are few instances where you need a public, mutable singleton.
  2. use an explicit object (POCO) instead of a multi-dimensional array. see below for an example
  3. instead create/save/load the objects from storage as needed.

    class Auction { public long Id {get; set;} public string ItemName {get; set;} ... }

Jason Meckley
  • 7,589
  • 1
  • 24
  • 45