PDA

View Full Version : [C#]Alternative a switch


RaouL_BennetH
16-05-2008, 16:59
Ciao a tutti :)

Ho scritto questo codice che mi serve per ottenere alcuni dati in base ad un filtro:



private void GetFilteredData(string tipo)
{
DbClass db = new DbClass();
try
{
string select = "SELECT * FROM vetture WHERE " + tipo + " LIKE '?filtro' ORDER BY " + tipo + " ";

MysqlDataAdapter mda = new MySqlDataAdapter(select, db.ActiveConnection);
DataTable dt = new DataTable();
mda.Fill(dt);

//codice per visualizzare i dati blablabla
}
catch(MysqlException ex)
{
MessageBox.Show(ex.Message);
}
finally
{
db.CloseConnection(db.ActiveConnection);
}
}

private void btnFilter_Click(objext sender, EventArgs e)
{
string tipo = "";
switch (comboScelte.SelectedIndex)
{
case 0:
tipo = "azienda";
GetFilteredData(tipo);
break;

case 1:
tipo = "targa";
GetFilteredData(tipo);
break;

default:
MessageBox.Show("Nessun filtro selezionato");
break;
}
}




Attendo suggerimenti :)

Grazie mille.

RaouL.

tomminno
16-05-2008, 17:30
Intanto potresti usare se possibile

GetFilteredData(comboScelte.SelectedValue);

Oppure se il valore del combobox non fosse direttamente consumabile da GetFilteredData, potresti usare un Dictionary che mappi i valori del combo su quelli attesi.

Dictionary<int,string> mappa;//Oppure mappi stringa su stringa
mappa[0] = "azienda";
mappa[1] = "targa";
...
GetFilteredData(mappa[comboScelte.SelectedIndex]); //oppure comboScelte.SelectedValue