![](/img/trans.png)
[英]array parameter when overriden in method doesn't change outside of it
[英]Array doesn't keep it's value outside of the method it's filled in
我有這行代碼,它獲取表的ID並將其提供給一個名為idPuntos的int數組(注意:此方法夾在其他地方的Open()和Close()方法之間,因此與數據庫無關)在這種意義上)。
public void refreshIds()
{
idInit.Clear();
try
{
SqlCommand getIds = new SqlCommand("SELECT id_punto FROM Puntos", cnn);
SqlDataReader idsReader = getIds.ExecuteReader();
while (idsReader.Read())
{
//idInit is a global ArrayList
idInit.Add(idsReader[0]);
}
//idPuntos is a global array, declared without value
idPuntos = (int[])idInit.ToArray(typeof(int));
//This line works here, I test printed it and got the values I wanted, yet it doesn't work in the following method...
bajaId = idPuntos[0];
}
catch (SqlException)
{
statusbajas.Text = "Hubo un error al recuperar los datos.";
}
}
它在這里不起作用:
private void listabajas_SelectedIndexChanged(object sender, EventArgs e)
{
//it doesnt work here, and i dont understand why
bajaId = idPuntos[0];
//statusbajas.Text = "es " + listabajas.SelectedIndex.ToString();
}
有任何想法嗎?
編輯:通過不起作用,我的意思是idPuntos [0]不在第一個(它實際上保存我提供的值)時似乎為空。
編輯2:問題似乎從構造函數開始。 在第一個Close()調用上放置一個斷點,使我意識到自該點以來發生崩潰。
這是我的構造函數:
public AltasPuntos()
{
InitializeComponent();
str = Properties.Settings.Default.SWMConnString;
cnn = new SqlConnection(str);
cnn.Open();
refreshListNombres();
cnn.Close();
cnn.Open();
refreshIds();
cnn.Close();
}
您需要確保在refreshIds
之前調用listabajas_SelectedIndexChanged
。
如果使用的是WinForms,則可以refreshIds
的調用放入表單的構造函數中。
從問題中提供的信息中我可以建議的差不多。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.