如何将字符串转换为布尔值
问题描述:
任何人都可以更正以下代码:
错误代码:
Can Anyone correct the following code:
Error code:
protected void BulletedList3_Click(object sender, BulletedListEventArgs e)
{
ListItem li = BulletedList3.Items[e.Index];
string price = li.ToString();
price_range = BulletedList3.Items[e.Index].Value;
if (con.State == ConnectionState.Closed)
{
con.Open();
}
switch (price_range)
{
case "Below Rs.500/-":
condition = "<500";
break;
//case "Rs.500/- to Rs.1000/-":
// break;
//case "Rs.1000/- To Rs.2000/-":
// break;
default:
break;
}
con.Close();
price1();
}
public void price1()
{
if(Convert.ToBoolean(price_range == "<500"))
{
SqlCommand cmd = new SqlCommand("select pro_name,pro_img,price from product where status=''A''and type=''retail''", con);
SqlDataAdapter da = new SqlDataAdapter(cmd);
DataSet ds = new DataSet();
da.Fill(ds);
DataList1.DataSource = ds;
DataList1.DataBind();
con.Close();
}
}
答
您要回答以下哪些问题-此问题或 ^ ]?
您要在此代码中纠正什么?
您的字符串到布尔值的转换在这里看起来还可以.
Which of these questions do you want answered - this one or this one[^]?
What do you want corrected in this code?
Your string to boolean conversion looks ok here.
此代码
if(Convert.ToBoolean(price_range == "<500"))
不管用.阅读此 Convert.ToBoolean方法(字符串) [
will not work. Read this Convert.ToBoolean Method (String)[^]
Instead you will need to do something like the following
if (price_range <= 500)
我只是注意到price_range是一个字符串?如果是这样,您尝试做的只是很奇怪,我建议您重新考虑您的设计.
好吧,我想你可以做这样的事情
I just noticed that price_range is a string?? If so what you are trying to do is just weird and I recommend you to rethink your design.
Well I guess you could do something like this
string price_range = "Below Rs.500/-";
string[] priceRange = price_range.Split(new char[] { '.', '/' });
if (Int32.Parse(priceRange[1]) <= 500)
{
}