这是中(类型)objectname.var(类型) [英] What is the (type) in (type)objectname.var

查看:123
本文介绍了这是中(类型)objectname.var(类型)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在C#经历一本书,所遇到的东西,我似乎无法查找,因为我不知道它叫什么,或者试图寻找被描述的东西。



一些能向我解释是怎么回事,或者背后的含义(类型)自带一个对象的引用之前在(INT)objectname.variablename?



好像铸造了我。



感谢。



编辑:
由于大部分你要关闭我的'参考铸造时,我只是猜测,并需要更多的代码,我包括我检讨这带来了对这个问题的代码。我质疑的(INT)的(INT)numericupDown1.Value;

 私人无效numericUpDown1_ValueChanged(对象发件人,EventArgs的发送) 
{
dinnerParty.NumberOfPeople =(INT)numericUpDown1.Value;
DisplayDinnerPartyCost();
}


解决方案

据铸造(转换)该numericUpDown1.Value为int值。这也可以使用



进行

  Convert.toInt32(numericUpDown1.Value)

我假设dinnerParty.NumberOfPeople是一个整数值,因此,如果您不投,如果提供的值不是一个整数,它会抛出一个错误。例如,如果提供的值是一个双重或将抛出一个错误,用铸造它会转换成双串说20.5〜20,这将被接受。对于字符串这将取决于如果字符串中它包含了一个数字。如果字符串是12,然后用上面提到的将其转换为整数12的转换方法。


I am going through a book on C# and have come across something that I can't seem to look up, because I don't know what it is called, or trying to search for something by description.

Could some explain to me what is going on, or the meaning behind the "(type)" that comes before a reference to an object as in (int)objectname.variablename?

It seems like casting to me.

Thanks.

EDIT: Since most of you are going off 'My' reference to casting when I am only guessing, and needed more code, I am including the code that I am reviewing that has brought on this question. I am questioning the (int) in the (int)numericupDown1.Value;

private void numericUpDown1_ValueChanged(object sender, EventArgs e) 
{
    dinnerParty.NumberOfPeople = (int)numericUpDown1.Value;
    DisplayDinnerPartyCost();
}

解决方案

It is casting(converting) the numericUpDown1.Value to an int value. This can also be done using

Convert.toInt32(numericUpDown1.Value)

I'm assuming that dinnerParty.NumberOfPeople is an integer value and therefore if you didn't cast it would throw an error if the value supplied was not an integer. For example if the Value supplied was a double or a string it would throw an error, with casting it would convert the double say 20.5 to 20 and it would be accepted. For the string it would depend on if the string contained a number in it. If the string was "12" then using the convert method mentioned above would convert it to the integer 12.

这篇关于这是中(类型)objectname.var(类型)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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