Reputation: 3747
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!
Upvotes: 8
Views: 90856
Reputation: 26633
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.
Upvotes: 10
Reputation: 835
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
}
Upvotes: 11
Reputation: 7591
this is a very bad idea.
instead create/save/load the objects from storage as needed.
class Auction { public long Id {get; set;} public string ItemName {get; set;} ... }
Upvotes: 1
Reputation: 2210
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.
Upvotes: 1
Reputation: 8206
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;
Upvotes: 2
Reputation: 22399
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.
Upvotes: 4
Reputation: 12685
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]
Upvotes: 2
Reputation: 16505
string[] values;
Or to declare one of a particular length:
string[] values = new string[6]; // creates an array with a length of 6.
Upvotes: 1