我試圖在陣列串列中選擇一個隨機用戶控制元件。我得到了陣列的索引,但它告訴我它“不能簡單地將 int 轉換為 UserControl”。有誰知道我做錯了什么?
ArrayList notiList = new ArrayList();
int count = 0;
int i;
public MainPage()
{
this.InitializeComponent();
foreach (NotiObject noti in itemsPanel.Children.Where(c => c is NotiObject))
{
notiList.Add(noti);
System.Diagnostics.Debug.WriteLine(noti);
}
i = new Random().Next(0, notiList.Count);
}
void sendNotification()
{
NotiObject randomNoti = notiList.IndexOf(i);
}
uj5u.com熱心網友回復:
正如 Dai 所暗示的那樣,ArrayList 是一個特別古老的東西,早在 .net 相對較新并且沒有被稱為泛型的非常有用的特性的時代。
ArrayList 的手冊頁說明了這一點(我的重點):
重要 我們不建議您將 ArrayList 類用于新開發。相反,我們建議您使用通用 List 類。
甚至制造商都在說“不要使用此產品”
ArrayList 最大的問題是因為它希望能夠存盤任何東西,所以它將其內容保存在一個object陣列中
這意味著您可以將兩個完全不相關的東西放在一起,如果這樣做,您必須檢查它們的型別,并且您總是必須強制轉換以將物件變回您想要的
notiList.Add(123); //whoops, that's not a NotiObject
foreach(var o in notiList)
var n = (NotiObject)notiList[0]; //whoops, that's a crash
}
因此,使用它非常令人厭煩,尤其是您必須一直施放的部分。這很快就會變得無聊:
object o = "hello";
object p = "world";
object q = (string)o (string)p;
object r = ((string)q).Substring(3).IndexOf((stribg)p);
r = (int)r ((int)r)/2;
可以將所有內容存盤在一個物件中,但看看它有多亂。您必須開始將型別名稱放入變數名稱中,以幫助記住 r 是一個 int,而 q 是一個字串 - 匈牙利符號是過去的另一個遺物。
當你把東西放在一個 ArrayList 中時,這就是你正在做的;將它們存盤在object
于是發明了泛型,發明了 List。可以定制的串列來存盤單一型別的物件,如字串、int 或 NotiObject
var nums = new List<int>();
nums.Add(123); //works
var notiList = new List<NotiObject>();
notiList.Add(123); //compiler refuses this one
說了這么多,可以回答你的問題了。這段代碼沒有意義:
NotiObject randomNoti = notiList.IndexOf(i);
i is an integer. IndexOf is a method that finds the numeric index of an item in the list. If the list was "a","b","c" and you asked for IndexOf("b") the result is 1 because b is at the second index, and indexing starts from 0.
IndexOf is not "get me the object at index blahblah", it's "tell me the index of this object blahblah"
The code doesn't make sense because you've passed an integer in and the list stores NotiObject. IndexOf will never find an integer in a list of NotiObject. This was the first mistake. You were allowed to make it because ArrayList stores everything as objects so you're allowed to pass an integer into IndexOf even if there are no integers in the list
IndexOf returns an integer. You cannot assign an integer to a variable of type NotiObject. This is the thing the compiler is complaining about
Even if you form the code correctly, you still have to cast:
NotiObject randomNoti = (NotiObject)notiList[i];
It's all very wearisome and if you persist with ArrayList probably not the last mistake you'll make with it either
If you used a List<NotiObject> you wouldn't have been allowed to pass an integer to IndexOf; the compiler would have stopped you which would hopefully then have made you assess IndexOf in the docs, and see that it's for finding the int index from the object, not the object at int index
You'd write code like:
List<NotiObject> notiList = new List<NotiList>();
...
NotiObject randomNoti = notiList[i];
without the cast. If you want to read more into why there is no cast, check out some introductory articles to generics. In a nutshell generics (any time you see something like <T> or <TBlahBlah>) allow you to specify something like a template code skeleton that the compiler uses to create code for you; code that substitutes the for the kind of object you want to work with. There isn't any casting any more because the compiler will write a whole List class that dedicatedly only works with NotiObjects
轉載請註明出處,本文鏈接:https://www.uj5u.com/ruanti/441852.html
