无法将类型 'void' 隐式转换为 'int'?
Cannot implicitly convert type 'void' to 'int'?
好的,我对编程还很陌生,我不明白为什么会出现此错误。
我的方法应该像在这个方法中那样存储邮件:
public void StoreMail(PhishingMail PhishingMail)
{
using (var phishingMailStorage = new PhishFinderModel())
{
phishingMailStorage.PhishingMail.Attach(PhishingMail);
phishingMailStorage.SaveChanges();
}
然后在我的 processPhishingMail 方法中调用 Store 方法。但后来我收到错误:无法将类型 'void' 隐式转换为 'int'.
public void ProcessPhishingMail(PhishingMail phishingMail)
{
Hashtable phishingUrls;
int phishingMailId;
int phishingUrlId;
//Error convertion
phishingMailId = _storageAgent.StoreMail(phishingMail);
所以我真的找不到从 void 转换为 int 的解决方案。也许这是不可能的?
我的问题是:如何在我的代码中转换这些类型?
请不要苛刻,我对此很陌生。感觉就像我在信息的海洋中游泳,我不知道从哪里看或从哪里开始。所以非常欢迎任何建议的教程。
方法
public void StoreMail(PhishingMail PhishingMail)
没有 returning 任何值。所以当你这样做时:
phishingMailId = _storageAgent.StoreMail(phishingMail);
你收到了那个错误,因为 StoreMail
被声明为 void
,这意味着它没有 return 任何东西。
要解决此问题,只需像这样调用 StoreMail
:
_storageAgent.StoreMail(phishingMail);
如果您打算 return 来自 StoreMail
的值,则必须将其更改为 return 和 int
值:
public int StoreMail(PhishingMail PhishingMail)
{
using (var phishingMailStorage = new PhishFinderModel())
{
phishingMailStorage.PhishingMail.Attach(PhishingMail);
phishingMailStorage.SaveChanges();
}
return someIdWhichYouNeedToFigureOut;
}
创建方法时,您定义了 return 类型。 Void 表示它不会 return 任何东西。您已将您的方法定义为 public void StoreMail()
,因此说它不会 return 任何东西。
当您调用该方法时,您要求 return phishingMailId = _storageAgent.StoreMail(phishingMail)
。但是因为您将方法定义为 void 并且您没有让方法 return 无法获取 id 的任何内容,所以您会收到错误。
要解决此问题,您必须将方法 return 设为 int
public int StoreMail(PhishingMail phishingMail){}
然后在方法中进一步定义您想要的内容 return
return newId;
在我的例子中,我在数据库 SQL 服务器中创建了存储过程,
然后我在 c# 程序中创建了一个 class 并调用了这个存储过程并在 class 和 return 数据 table 中的存储过程中添加了参数,最后我可以在我的程序中的任何地方调用我的 class
像这个例子:
1- 创建存储过程:
CREATE proc [dbo].[GET_SAMPLE_KIND]
@TESTID int
as
select Dept_id,ID_sample from LabTests
where LabTests.TestId = @TESTID
2- 在您的 C# 程序中创建 class 并使用如下代码的参数调用存储过程:
public DataTable GET_SAMPLE_KIND(int TESTID)
{
DAL.DataAccessLayer DAL = new DAL.DataAccessLayer();
DataTable dt = new DataTable();
SqlParameter[] param = new SqlParameter[1];
param[0] = new SqlParameter("@TESTID", SqlDbType.Int);
param[0].Value = TESTID;
dt = DAL.SelectData("GET_SAMPLE_KIND", param);
DAL.close();
return dt;
}
要连接和读取数据库,您必须创建另一个 class 来联系 sql 服务器,在我的例子中是 DAL.selectData 从数据库获取 select 数据的方法,也对于插入、更新和删除语句,您可以创建存储过程,然后在 class 中为该函数创建另一个 void。
3- 最后,您可以从程序中调用 class 和存储过程。
好的,我对编程还很陌生,我不明白为什么会出现此错误。
我的方法应该像在这个方法中那样存储邮件:
public void StoreMail(PhishingMail PhishingMail)
{
using (var phishingMailStorage = new PhishFinderModel())
{
phishingMailStorage.PhishingMail.Attach(PhishingMail);
phishingMailStorage.SaveChanges();
}
然后在我的 processPhishingMail 方法中调用 Store 方法。但后来我收到错误:无法将类型 'void' 隐式转换为 'int'.
public void ProcessPhishingMail(PhishingMail phishingMail)
{
Hashtable phishingUrls;
int phishingMailId;
int phishingUrlId;
//Error convertion
phishingMailId = _storageAgent.StoreMail(phishingMail);
所以我真的找不到从 void 转换为 int 的解决方案。也许这是不可能的?
我的问题是:如何在我的代码中转换这些类型?
请不要苛刻,我对此很陌生。感觉就像我在信息的海洋中游泳,我不知道从哪里看或从哪里开始。所以非常欢迎任何建议的教程。
方法
public void StoreMail(PhishingMail PhishingMail)
没有 returning 任何值。所以当你这样做时:
phishingMailId = _storageAgent.StoreMail(phishingMail);
你收到了那个错误,因为 StoreMail
被声明为 void
,这意味着它没有 return 任何东西。
要解决此问题,只需像这样调用 StoreMail
:
_storageAgent.StoreMail(phishingMail);
如果您打算 return 来自 StoreMail
的值,则必须将其更改为 return 和 int
值:
public int StoreMail(PhishingMail PhishingMail)
{
using (var phishingMailStorage = new PhishFinderModel())
{
phishingMailStorage.PhishingMail.Attach(PhishingMail);
phishingMailStorage.SaveChanges();
}
return someIdWhichYouNeedToFigureOut;
}
创建方法时,您定义了 return 类型。 Void 表示它不会 return 任何东西。您已将您的方法定义为 public void StoreMail()
,因此说它不会 return 任何东西。
当您调用该方法时,您要求 return phishingMailId = _storageAgent.StoreMail(phishingMail)
。但是因为您将方法定义为 void 并且您没有让方法 return 无法获取 id 的任何内容,所以您会收到错误。
要解决此问题,您必须将方法 return 设为 int
public int StoreMail(PhishingMail phishingMail){}
然后在方法中进一步定义您想要的内容 return
return newId;
在我的例子中,我在数据库 SQL 服务器中创建了存储过程, 然后我在 c# 程序中创建了一个 class 并调用了这个存储过程并在 class 和 return 数据 table 中的存储过程中添加了参数,最后我可以在我的程序中的任何地方调用我的 class 像这个例子:
1- 创建存储过程:
CREATE proc [dbo].[GET_SAMPLE_KIND]
@TESTID int
as
select Dept_id,ID_sample from LabTests
where LabTests.TestId = @TESTID
2- 在您的 C# 程序中创建 class 并使用如下代码的参数调用存储过程:
public DataTable GET_SAMPLE_KIND(int TESTID)
{
DAL.DataAccessLayer DAL = new DAL.DataAccessLayer();
DataTable dt = new DataTable();
SqlParameter[] param = new SqlParameter[1];
param[0] = new SqlParameter("@TESTID", SqlDbType.Int);
param[0].Value = TESTID;
dt = DAL.SelectData("GET_SAMPLE_KIND", param);
DAL.close();
return dt;
}
要连接和读取数据库,您必须创建另一个 class 来联系 sql 服务器,在我的例子中是 DAL.selectData 从数据库获取 select 数据的方法,也对于插入、更新和删除语句,您可以创建存储过程,然后在 class 中为该函数创建另一个 void。
3- 最后,您可以从程序中调用 class 和存储过程。