程序中的C#转换数据类型错误 [英] C# conversion data type error in program

查看:51
本文介绍了程序中的C#转换数据类型错误的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

大家好我试着转换这一行

代码末尾:

Hello Everyone i am Try to Convert This Line
at the end of Code :

;classes.RemoveAt(classes.Single() + (Class)randClass);

请告诉解决此错误

运算符+无法从类转换为Int

please Tell Be Solution of this Error
operator "+" cannot Convert from Class to Int

private int clash_teach;
private int over_burden;
private int t_class;
Random rand = new Random();
Day[] days = new Day[10];
Count count = new Count();
Class c = new Class();
int Number;
public Timetable(List<string> times, List<Class> classes, Count _count)
{
count = _count;
over_burden = clash_teach = 0;
t_class = classes.Count;
days = new Day[count.DAYS];
//Now Using Loop 
for (int i = 0; i < count.DAYS; i++)
{

days[i] = new Day();
days[i].setTimings(times, count.ROOMS);
//days.push_back(d);
}
while (classes.Count > 0)
{
Room[] r = days[(rand.Next() % count.DAYS)].getSlots()[rand.Next() % count.SLOTS].GetRooms();
int rnd = (rand.Next() % count.ROOMS);

if (r[rnd].GetClass()==null)
{
//int randClass = (rand() % classes.size());
//r[rnd].setClass(classes.at(randClass));
//classes.erase(classes.begin() + randClass);
//classes = new List<Class>();
int randClass = (rand.Next() % t_class);
r[rnd].SetClass(classes.ElementAtOrDefault(randClass));
classes.RemoveAt((GetData(classes.SingleOrDefault()) + randClass));
//classes.RemoveAt(classes.Single() + (Class)randClass);
}
}
}





我尝试了什么:



//classes.RemoveAt(classes.Single()+(Class)randClass);



What I have tried:

//classes.RemoveAt(classes.Single() + (Class)randClass);

推荐答案

首先,停止尝试通过在完全不同的语言中找到类似的代码并思考那样做 - 它不会,这是一种糟糕的做事方式。

例如,行:

Firstly, stop trying to do this by finding a similar code in a totaly different language and thinking "that'll do" - it won't, it's a bad way to do things.
For example, the line:
int randClass = (rand.Next() % t_class);

是一种旧的,预先的.NET方法,可以在老年人的C程序中找到随机数字。

相反,C#允许你这样做:

is the old, pre .NET way of doing random numbers that you would have found in a geriatric C program.
Instead, C# allows you to do this:

int randClass = rand.Next(classes.Count);



如果C允许你将一个整数转换为不同的类型而不打击眼睛, C#是强类型的 - 所以除非 Class 派生自 int - 它不是,因为 int 是一个 struct ,你不能从 struct 中派生任何东西 - 然后这段代码保证失败:


And where C would allow you to cast an integer to a different type without batting an eye, C# is strongly typed - so unless Class is derived from int - which it isn't, as int is a struct and you can't derive anything from a struct - then this code is guaranteed to fail:

classes.RemoveAt(classes.Single() + (Class)randClass);



停止你想要做的事情,并尝试以适当的C#,OOP方式完成所有这些,而不是找到可能工作和命中的代码它用锤子......:笑:


Stop what you are trying to do, and try to do all this in a proper, C#, OOPs manner instead of finding code that might work and hitting it with a hammer...:laugh:


这篇关于程序中的C#转换数据类型错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆