本文介绍了在int转换中获取错误并添加方法帮助请!!的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

使用System;

使用System.Collections.Generic;

使用System.Linq;

使用System.Text;



命名空间ConsoleApplication49

{

class program

{

static void Main(string [] args)

{

amritEntities context = new amritEntities();

Timesheet xyz = new Timesheet() {projectid =5,moduleid =5,taskid =5};





context.AddToTimesheet(xyz) ;



context.SaveChanges();







}

}

}

错误4'ConsoleApplication49.amritEntities'不包含'AddToTimesheet'的定义,也没有扩展名可以找到方法'AddToTimesheet'接受类型'ConsoleApplication49.amritEntities'的第一个参数(你是否缺少using指令或程序集引用?)c:\ usersrs \ user \ document \ visual studio 2010 \Projects \ConsoleApplication49 \ConsoleApplication49 \ Program.cs 16 21 ConsoleApplication49

错误1无法将类型'string'隐式转换为'int?'c:\ usersrs \ user\documents \ visual studio 2010 \Projects\ConsoleApplication49 \ConsoleApplication49 \Program.cs 13 56 ConsoleApplication49

错误2无法将类型'string'隐式转换为'int?'c:\ usersrs \ user \\ \\做cuments \visual studio 2010 \Projects\ConsoleApplication49 \ConsoleApplication49 \Program.cs 13 70 ConsoleApplication49

错误3无法将类型'string'隐式转换为'int?'c:\ users \ user\documents\visual studio 2010 \Projects \ConsoleApplication49 \ConsoleApplication49 \Program.cs 13 82 ConsoleApplication49

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace ConsoleApplication49
{
class Program
{
static void Main(string[] args)
{
amritEntities context = new amritEntities();
Timesheet xyz = new Timesheet(){ projectid="5", moduleid="5" ,taskid="5"};


context.AddToTimesheet(xyz);

context.SaveChanges();



}
}
}
Error4'ConsoleApplication49.amritEntities' does not contain a definition for 'AddToTimesheet' and no extension method 'AddToTimesheet' accepting a first argument of type 'ConsoleApplication49.amritEntities' could be found (are you missing a using directive or an assembly reference?)c:\users\user\documents\visual studio 2010\Projects\ConsoleApplication49\ConsoleApplication49\Program.cs1621ConsoleApplication49
Error1Cannot implicitly convert type 'string' to 'int?'c:\users\user\documents\visual studio 2010\Projects\ConsoleApplication49\ConsoleApplication49\Program.cs1356ConsoleApplication49
Error2Cannot implicitly convert type 'string' to 'int?'c:\users\user\documents\visual studio 2010\Projects\ConsoleApplication49\ConsoleApplication49\Program.cs1370ConsoleApplication49
Error3Cannot implicitly convert type 'string' to 'int?'c:\users\user\documents\visual studio 2010\Projects\ConsoleApplication49\ConsoleApplication49\Program.cs1382ConsoleApplication49

推荐答案

xyz.AddToTimeTimesheet(context);







仍然得到转换为字符串错误





啊!

这是一个不同的问题!

这可能在这一行:




"still getting the convert to string error"


Ah!
That's a different problem!
That's probably in this line:

Timesheet xyz = new Timesheet(){ projectid="5", moduleid="5" ,taskid="5"};

因为你得到它三次,我怀疑所有三个属性都是 int?而不是 string - 并且因为你明确地尝试将它们设置为字符串,你会得到那个错误。

试试这个:

And since you get it three times, I suspect that all three properties are int? rather than string - and since you explicitly try to set them as strings, you will get that error.
Try this:

Timesheet xyz = new Timesheet(){ projectid=5, moduleid=5 ,taskid=5};





但在您的真实世界代码中,您几乎肯定会从用户那里获得这些代码as strings:在这种情况下使用int.TryParse转换为整数(报告任何验证问题),然后将整数值传递给Timesheet构造函数。



But in your "real world" code you almost certainly are getting these from a user as strings: in which case use int.TryParse to convert then to integers (reporting any validation problems) and then pass the integer values to your Timesheet constructor.


这篇关于在int转换中获取错误并添加方法帮助请!!的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

11-03 02:54