无法将类型'object'隐式转换为'int'.存在显式转换(您是否缺少演员表?) [英] Cannot implicitly convert type 'object' to 'int'. An explicit conversion exists (are you missing a cast?)

查看:82
本文介绍了无法将类型'object'隐式转换为'int'.存在显式转换(您是否缺少演员表?)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

大家好,

C#中的新手,出现了给定的错误,


无法将类型"object"隐式转换为"int".存在显式转换(您是否缺少演员表?)



源错误:

第262行:int verifycount_result = 0;
第263行:
第264行:verifycount_result = Destinations_Alloc_Count();
265行:
第266行:if(verifycount_result =="1")


我的代码是:

hi all,

am new in c#, i got the given error,


Cannot implicitly convert type ''object'' to ''int''. An explicit conversion exists (are you missing a cast?)



Source Error:

Line 262: int verifycount_result=0;
Line 263:
Line 264: verifycount_result = Destinations_Alloc_Count();
Line 265:
Line 266: if(verifycount_result=="1")


my code is:

int verifycount_result=0;

    verifycount_result = Destinations_Alloc_Count();

    if(verifycount_result=="1")

    {




    public object Destinations_Alloc_Count()
    {
        string result = null;

        result = "";
        try
        {
            SqlConnection con = new SqlConnection(sqlconn_cmsstr);
            if (con.State == ConnectionState.Closed)
            {
                con.Open();
            }
            SqlCommand cmd = new SqlCommand("select count(divertedto) from Hotdial where accountnumber='" + Session["user_id"] + "'", con);
            int i = 0;
            i = 0;
            i = int.Parse(cmd.ExecuteScalar());
            if (i > 200)
            {
                result = "0";
                return result;
            }
            else
            {
                result = "1";
                return result;
            }


        }
        catch (Exception ex)
        {
            result = "-1";
            return result;
        }
    }

推荐答案

这类似于您的其他问题

您的函数正在返回一个对象.在函数内,您将返回一个字符串.调用函数时,就是将其分配给int!

1)更改函数以返回类型字符串.

This is similar to your other question

Your function is returning an object. Within the function, you are returning a string. When you are calling the function, you are assigning it to an int!

1) Change your function to return type string.

public string Destinations_Alloc_Count()



2)将变量更改为字符串类型



2) Change your variable to be a string type

string verifycount_result = string.Empty;
verifycount_result = Destinations_Alloc_Count();



尝试保持相同的类型,否则您将遇到类似这样的问题



Try and keep your types the same, or you will have issues like this


这篇关于无法将类型'object'隐式转换为'int'.存在显式转换(您是否缺少演员表?)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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