C#不是C++
数字类型不能转换为bool类型
小数也不能转换为整数如果想小数转整数,你需要先转double,double再转int
数字类型不能转换为bool类型
小数也不能转换为整数如果想小数转整数,你需要先转double,double再转int
解决方案 »
- 求类似foxmail 7界面导航风格的winfrm源码
- if (System.Diagnostics.Debugger.IsAttached) 判断的什么
- C# 类修饰 [ some class ] 是干啥的~?
- dataGridView多列求和的问题
- 这个函数()输入什么字符串获取所有文件和文件夹的名字?
- [高分!!]使用axacropdf如何设置Pdf全屏显示?
- 请问Session是保存在哪的
- DataTable删除一行怎么解决?
- 怎么从父页面传值到iframes??????
- 请高手指点,为什么在Windows模式客户端响应.net remoting服务组件中的一个event就出现错误呢?
- 控件怎么调用窗体?
- 如何给Word中的图片添加tag标签?
需要注意的就是bool类型和string类型
不是任何string都能转成别的类型的
比如"XYZ"这个字符串,你不管转成什么数值类型,不管按什么进制转换,都是转不了的.
而bool类型,ToString之后,是"True"或"False",而不是"1"和"0".
但是这样
string misc = "36.987";
double dbu = Convert.ToDouble(misc);
bool boolean = Convert.ToBoolean(dbu);
却是正确的,请问是什么情况呢
数值可以转bool,0是false,不等于0是true不过"0"是个字符串,你从字符串直接转bool不行.
要么就写成"False"或"True",也是可以转的
必须是符合数值规则的字符串才能转成对应类型的数值
然后数值之间你就可以任意转了.
书上这么说,是说错了,还是我哪里理解错了吗?求指点
确实是任意类型都可以互相转啊,不过对于数据有要求,数据本身必须能转,才能转.数据本身不是那个格式的,就转不了.
string确实可以转int或double或bool
不过必须是整数才能转int,小数转double,true或false转bool
不是随便写个XYZ就能转成任意数字了
不能“想当然”,要根据库提供的函数和参数操作,否则就出错。
Convert.ToBoolean 方法 (String)
参数必须能被它的规则识别才行,而不是0和1就行的。
using System;public class BooleanConversion
{
public static void Main()
{
String[] values = { null, String.Empty, "true", "TrueString",
"False", " false ", "-1", "0" };
foreach (var value in values) {
try
{
Console.WriteLine("Converted '{0}' to {1}.", value,
Convert.ToBoolean(value));
}
catch (FormatException)
{
Console.WriteLine("Unable to convert '{0}' to a Boolean.", value);
}
}
}
}
// The example displays the following output:
// Converted '' to False.
// Unable to convert '' to a Boolean.
// Converted 'true' to True.
// Unable to convert 'TrueString' to a Boolean.
// Converted 'False' to False.
// Converted ' false ' to False.
// Unable to convert '-1' to a Boolean.
// Unable to convert '0' to a Boolean.
不能“想当然”,要根据库提供的函数和参数操作,否则就出错。
Convert.ToBoolean 方法 (String)
参数必须能被它的规则识别才行,而不是0和1就行的。
using System;public class BooleanConversion
{
public static void Main()
{
String[] values = { null, String.Empty, "true", "TrueString",
"False", " false ", "-1", "0" };
foreach (var value in values) {
try
{
Console.WriteLine("Converted '{0}' to {1}.", value,
Convert.ToBoolean(value));
}
catch (FormatException)
{
Console.WriteLine("Unable to convert '{0}' to a Boolean.", value);
}
}
}
}
// The example displays the following output:
// Converted '' to False.
// Unable to convert '' to a Boolean.
// Converted 'true' to True.
// Unable to convert 'TrueString' to a Boolean.
// Converted 'False' to False.
// Converted ' false ' to False.
// Unable to convert '-1' to a Boolean.
// Unable to convert '0' to a Boolean.
没仔细看MSDN文档,谢谢了嗯,基本理解了,谢谢指点了