System.Data.OleDb.OleDbDataReader rdr;
switch (ProduceType)
{
case "1": //metal
rdr=zfsystem.GetTopOneReader("select a.*,b.metal from star a,capacity b where a.starid="+Convert.ToInt32(JumpStar.SelectedItem.Value)+" and b.LV=a.MetalMine");
break;
case "2": //crystal
rdr=zfsystem.GetTopOneReader("select a.*,b.crystal from star a,capacity b where a.starid="+Convert.ToInt32(JumpStar.SelectedItem.Value)+" and b.LV=a.CrystalMine");
break;
case "3": //fuel
rdr=zfsystem.GetTopOneReader("select a.*,b.fuel from star a,capacity b where a.starid="+Convert.ToInt32(JumpStar.SelectedItem.Value)+" and b.LV=a.FuelMine");
break;
}
rdr.Read();这种代码,读取时会提示rdr是没付值的局部变量,那么,如果我想按这种思路来使用switch,应该怎么写呢?还是说只能靠IF来解决?谢谢
switch (ProduceType)
{
case "1": //metal
rdr=zfsystem.GetTopOneReader("select a.*,b.metal from star a,capacity b where a.starid="+Convert.ToInt32(JumpStar.SelectedItem.Value)+" and b.LV=a.MetalMine");
break;
case "2": //crystal
rdr=zfsystem.GetTopOneReader("select a.*,b.crystal from star a,capacity b where a.starid="+Convert.ToInt32(JumpStar.SelectedItem.Value)+" and b.LV=a.CrystalMine");
break;
case "3": //fuel
rdr=zfsystem.GetTopOneReader("select a.*,b.fuel from star a,capacity b where a.starid="+Convert.ToInt32(JumpStar.SelectedItem.Value)+" and b.LV=a.FuelMine");
break;
}
rdr.Read();这种代码,读取时会提示rdr是没付值的局部变量,那么,如果我想按这种思路来使用switch,应该怎么写呢?还是说只能靠IF来解决?谢谢
switch (ProduceType)
{
case "1": //metal
rdr = ...
break;
case "2": //crystal
rdr = ...
break;
default:
rdr = null;
break;
}
rdr.Read();加上 default: 就可以了
switch (ProduceType)
{
case "1": //metal
rdr=zfsystem.GetTopOneReader("select a.*,b.metal from star a,capacity b where a.starid="+Convert.ToInt32(JumpStar.SelectedItem.Value)+" and b.LV=a.MetalMine");
break;
case "2": //crystal
rdr=zfsystem.GetTopOneReader("select a.*,b.crystal from star a,capacity b where a.starid="+Convert.ToInt32(JumpStar.SelectedItem.Value)+" and b.LV=a.CrystalMine");
break;
case "3": //fuel
rdr=zfsystem.GetTopOneReader("select a.*,b.fuel from star a,capacity b where a.starid="+Convert.ToInt32(JumpStar.SelectedItem.Value)+" and b.LV=a.FuelMine");
break;
}
rdr.Read();
System.Data.OleDb.OleDbDataReader rdr;
switch (ProduceType)
{
case "1": //metal
rdr=zfsystem.GetTopOneReader("select a.*,b.metal from star a,capacity b where a.starid="+Convert.ToInt32(JumpStar.SelectedItem.Value)+" and b.LV=a.MetalMine");
break;
case "2": //crystal
rdr=zfsystem.GetTopOneReader("select a.*,b.crystal from star a,capacity b where a.starid="+Convert.ToInt32(JumpStar.SelectedItem.Value)+" and b.LV=a.CrystalMine");
break;
case "3": //fuel
rdr=zfsystem.GetTopOneReader("select a.*,b.fuel from star a,capacity b where a.starid="+Convert.ToInt32(JumpStar.SelectedItem.Value)+" and b.LV=a.FuelMine");
break;
default:
break;
}
rdr.Read();