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