本文介绍了声明和使用全球排列C#的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我无法找到有关这个问题的任何信息。我想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;



然后我希望能够调用数组for循环,或者类似的东西,使我可以显示在稍后的时间的信息。现在,我忘了提一个小东西是,我可能会需要或者数组的数组或多维数组。这样做的原因是因为我将有很多不同的拍卖中,每种拍卖数据(因此上面的变量),我想显示在节目的最后信息。感谢您的时间!

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!

更新1

所以,我想我我没有让自己清楚,因为我越来越糊涂,但也可能是因为我在这个节目的那一刻有点沮丧。我希望能够建立一个全球的数组,然后获得&安培;设置存储在阵列具有不同的功能在我的程序内,不同的功能将被修改阵列的不同部分的信息。另外,我也不会介意使用缓存,如果我的理解是怎样的工作,甚至有一个链接,了解它。最后一件事补充,我在Windows Phone 7这样做,所以我对我可以使用哪些库和系统调用的限制。

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.

更新2

我对OOP颇有几分生疏,所以我会去更多阅读起来就可以了(感谢处理一吨的HTML并没有真正有机会做真正的编程)。感谢大家的建议。我可能会回发关于这个主题,如果我不能再弄清楚一些东西。干杯!

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!

推荐答案

全局变量几乎总是一个坏主意,所以C#使创建他们很难的事情。但是,如果你真的想,你可以做这样的事情:

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]);

请注意,但是,只要你不得不创建一个静态类来做到这一点,为什么不继续前进,使实​​际的数组变量私有的,而不是公开的,然后添加静态,公共getter和setter方法​​?这种做法至少消除一些全局变量固有的危险。

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.

这篇关于声明和使用全球排列C#的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

05-27 17:22
查看更多