转换为“txtInvoiceEndDate.Text”时出错 [英] Error on casting to "txtInvoiceEndDate.Text"
本文介绍了转换为“txtInvoiceEndDate.Text”时出错的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
当我尝试这个时:
string invMonth =(txtInvoiceEndDate.Text)DateTime.Now .GetDateTimeFormats()[ 130 ]。子串( 0 , 3 跨度>);
我收到错误文本框是字段但是像类型一样使用。我做错了什么?
解决方案
字段txtInvoiceEndDate.Text
不是Type。您无法直接将数据转换为实例或值。
您可以转换为类型。因此使用Convert.ToString()
将起作用。string invMonth = Convert.ToString(DateTime.Now.GetDateTimeFormats()[130] .Substring(0 ,3));
这应返回短月名:
string invMonth = DateTime.Now.ToString( MMM );
你还可以传递你想要格式化的文化。
你有一个名为txtInvoiceEndDate的控件(可能是一个TextBox),该控件有一个名为Text的字段。当然这与字符串不一样,顺便说一下你不需要任何演员:
string invMonth = DateTime.Now.GetDateTimeFormats()[ 130 ]。子串( 0 , 3 跨度>);
When I am trying this:
string invMonth = (txtInvoiceEndDate.Text)DateTime.Now.GetDateTimeFormats()[130].Substring(0, 3);
I get an error "Text box is field but used like type". What am I doing wrong?
解决方案
The fieldtxtInvoiceEndDate.Text
is not a Type. You cannot convert data into an instance or value directly.
You can cast to a type. Thus usingConvert.ToString()
will work.string invMonth = Convert.ToString(DateTime.Now.GetDateTimeFormats()[130].Substring(0, 3));
This should return short month name:
string invMonth = DateTime.Now.ToString("MMM");
You can additionally pass the culture you want it formatted for.
You have a Control named "txtInvoiceEndDate" (probably a TextBox) and this control has a field named "Text". Surely this is not the same as a string, BTW you don't need any cast here:
string invMonth = DateTime.Now.GetDateTimeFormats()[130].Substring(0, 3);
这篇关于转换为“txtInvoiceEndDate.Text”时出错的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文