无法隐式转换类型'T',以'诚信' [英] Cannot implicitly convert type 'T' to 'Int'

查看:180
本文介绍了无法隐式转换类型'T',以'诚信'的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

当试图调用我的codeI该功能得到错误的称号。同时运算符+ ='不能应用于类型为'廉政'和'T'

的操作数

 公众诠释变化与LT; T>(统计型,T值)
    {
        字典<字符串,字符串>临时=新字典<字符串,字符串>();
        临时= sql.Query(的String.Format(选择{0}从球员WHERE fbId ='{1}',type.ToString(),FBId));
        如果(typeof运算(T)== typeof运算(INT))
        {
            INT T = Convert.ToInt16(TEMP [type.ToString());
            T + =价值;
            如果(T< 0)返回-1;
            PlayerStats【类型】= T;

        }
        sql.Upload(的String.Format(更新播放器集{0} ='{1}'WHERE fbId ='{2}',type.ToString(),PlayerStats【类型】,FBId));
        返回0;
    }
 

我调用该函数使用:

 更改< INT>(类型,1);
 

解决方案

您可以尝试铸造值这样的...

  T + =(int)的值;
 

  T + = Convert.ToInt32(值);
 

When trying to call this function in my code i get the error in the title. Also Operator '+=' cannot be applied to the operands of type 'int' and 'T'

public int Change<T>(Stats type, T value)
    {
        Dictionary<string, string> temp = new Dictionary<string, string>();
        temp = sql.Query(string.Format("SELECT {0} FROM player WHERE fbId='{1}'", type.ToString(), FBId));
        if (typeof(T) == typeof(int))
        {
            int t = Convert.ToInt16(temp[type.ToString()]);
            t += value;
            if (t < 0) return -1;
            PlayerStats[type] = t;

        }
        sql.Upload(string.Format("UPDATE player SET {0}='{1}' WHERE fbId='{2}'", type.ToString(), PlayerStats[type], FBId));
        return 0;
    }

I call the function by using:

Change<int>(type, 1);

解决方案

you can try casting the value like this ...

t += (int)value; 

or

t+= Convert.ToInt32(value);

这篇关于无法隐式转换类型'T',以'诚信'的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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