Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Declaring and Using Global Arrays c#

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!

like image 647
th3n3wguy Avatar asked Jan 04 '12 21:01

th3n3wguy


2 Answers

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
}
like image 173
Xtian Macedo Avatar answered Oct 05 '22 02:10

Xtian Macedo


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.

like image 29
dgvid Avatar answered Oct 05 '22 01:10

dgvid