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!

7
  • 1
    No, don't do that. We don't use global arrays these days. Commented Jan 4, 2012 at 21:39
  • Using caching would be a better ideas than global arrays, as you are likely to run into concurrency and multithreading issues. Commented Jan 4, 2012 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"? Commented Jan 4, 2012 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. Commented Jan 4, 2012 at 21:42
  • You'd better reformulate the question more specifically: what is specifically needed to do. Commented Jan 4, 2012 at 21:46

8 Answers 8

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
}
Sign up to request clarification or add additional context in comments.

Comments

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.

Comments

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.

Comments

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]

1 Comment

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.
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;

Comments

1
string[] values;

Or to declare one of a particular length:

string[] values = new string[6]; // creates an array with a length of 6.

3 Comments

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.
Technically correct, but doesn't actually answer the question IMHO.
@thenewguy Please read more about object-oriented programming and design patterns. You're still thinking in non-OOP ways.
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.

Comments

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;} ... }

Comments

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.