强制转换异常处理程序 [英] Cast Exception Handler

查看:80
本文介绍了强制转换异常处理程序的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我遇到错误指定的类型转换无效

I am having an error Specified cast is not valid

//this is the code in the booking form
// time is public static
time = TimeSpan.Parse(dtpTime.Value.ToShortTimeString());
//this is the code in another form
while (rdr.Read())
{
    for (int i = 0; i < rdr.FieldCount; i++)
    {
        if ((TimeSpan)rdr[6] > Booking.time)
        {
            // etc etc
        }
    }
}

推荐答案

我假设rdr.Read()返回字符串?如果是这样,您将需要使用Timespan.ParseTimespan.TryParse来解析字符串,您不能仅仅因为没有定义字符串的隐式或显式转换而进行强制转换.
I''m assuming rdr.Read() returns a string? If so, you will need to use Timespan.Parse or Timespan.TryParse to parse the string, you cannot just cast as there is no implicit or explicit conversion from string defined.


那是什么?项目的数据类型?
:)
So what is the data type of the item?
:)


什么是rdr?
什么是rdr [6]?
您是否看过使用调试器?在"if((TimeSpan)rdr [6]> Booking.time)"行上放置一个断点,然后查看rdr数组包含的内容.
What is rdr?
What is rdr[6]?
Have you looked using the debugger? Put a breakpoint on the "if ((TimeSpan)rdr[6] > Booking.time)" line and see what the rdr array contains.


这篇关于强制转换异常处理程序的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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