无法将类型string隐式转换为int怎么解决? 10
stringbuildnum=building.Text.Trim();classroom.ClassroomBuilding=buildnum;(无法将类型string...
string buildnum = building.Text.Trim();
classroom.ClassroomBuilding = buildnum;(无法将类型string隐式转换为int)
string floor = flor.Text.Trim();
classroom.ClassroomStep = floor;(无法将类型string隐式转换为int) 展开
classroom.ClassroomBuilding = buildnum;(无法将类型string隐式转换为int)
string floor = flor.Text.Trim();
classroom.ClassroomStep = floor;(无法将类型string隐式转换为int) 展开
7个回答
展开全部
你的ClassroomBuilding和 ClassroomStep这两个变量类型都是int类型的,而 buildnum和 floor是string类型的,所以不能这样直接赋值。把这两句改成:
classroom.ClassroomBuilding = int.Parse(buildnum);
classroom.ClassroomStep = int.Parse(floor);
或者将ClassroomBuilding和 ClassroomStep这两个变量类型改成string
classroom.ClassroomBuilding = int.Parse(buildnum);
classroom.ClassroomStep = int.Parse(floor);
或者将ClassroomBuilding和 ClassroomStep这两个变量类型改成string
展开全部
三种方法:
1、classroom.ClassroomBuilding = (int)buildnum;
2、classroom.ClassroomBuilding = Int.Paras(buildnum);
3、classroom.ClassroomBuilding = Convert.ToInt32(buildnum);
1、classroom.ClassroomBuilding = (int)buildnum;
2、classroom.ClassroomBuilding = Int.Paras(buildnum);
3、classroom.ClassroomBuilding = Convert.ToInt32(buildnum);
追问
我用了以下第三种方法,能够执行,但显示错误说输入字符串的格式不正确,该怎么办
追答
要输入纯数字,如果有字母或符号就会报错
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
展开全部
在前面加(int)强制转换,或者使用转换函数convert.int32()
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
展开全部
把classroom.ClassroomBuilding = buildnum
改成classroom.ClassroomBuilding =Convert.ToInt32(buildnum )
改成classroom.ClassroomBuilding =Convert.ToInt32(buildnum )
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
展开全部
写成这样试试string buildnum = building.Text.Trim();
classroom.ClassroomBuilding = Convent.ToInt32(buildnum);
string floor = flor.Text.Trim();
classroom.ClassroomStep = Convent.ToInt32(floor);
classroom.ClassroomBuilding = Convent.ToInt32(buildnum);
string floor = flor.Text.Trim();
classroom.ClassroomStep = Convent.ToInt32(floor);
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询