简体   繁体   English

无法将类型“ void”隐式转换为“ int”?

[英]Cannot implicitly convert type 'void' to 'int'?

Oke so I am fairly new to programming and I don't get why I get this error. 好的,所以我对编程还很陌生,我不知道为什么会收到此错误。

My method should store mails which it does in this method: 我的方法应该存储在此方法中执行的邮件:

    public void StoreMail(PhishingMail PhishingMail)
    {
        using (var phishingMailStorage = new PhishFinderModel())
        {
            phishingMailStorage.PhishingMail.Attach(PhishingMail);

            phishingMailStorage.SaveChanges();

        }

And then in my processPhishingMail method it calls the Store method. 然后在我的processPhishingMail方法中,它调用Store方法。 But then I get the error: Cannot implicitly convert type 'void' to 'int'. 但是然后我得到一个错误:无法将类型'void'隐式转换为'int'。

public void ProcessPhishingMail(PhishingMail phishingMail)
{                     
      Hashtable phishingUrls;
      int phishingMailId;
      int phishingUrlId;
      //Error convertion
      phishingMailId = _storageAgent.StoreMail(phishingMail);

So I can't really find a solution to convert from void to int. 所以我真的找不到从void转换为int的解决方案。 Maybe it's not possible? 也许不可能吗?

My question is: how do I convert these types in my code? 我的问题是:如何在代码中转换这些类型?

Please don't be harsh I am really new to this. 请不要苛刻,我真的很陌生。 It feels like I am swimming in an ocean of information and I don't know where to look or start. 感觉就像我在信息海洋中畅游,不知道在哪里寻找或开始。 So any suggested tutorials are very welcome. 因此,任何建议的教程都非常欢迎。

The method 方法

public void StoreMail(PhishingMail PhishingMail)

is not returning any value. 没有返回任何值。 So when you do: 因此,当您这样做时:

phishingMailId = _storageAgent.StoreMail(phishingMail);

You are getting that error, since StoreMail is declared as void , which means it does not return anything. 您收到该错误,因为StoreMail被声明为void ,这意味着它不返回任何内容。

To fix this, simply call StoreMail like this: 要解决此问题,只需像这样调用StoreMail

_storageAgent.StoreMail(phishingMail);

If you intend to return a value from StoreMail , then you must change it to return an int value: 如果打算从StoreMail返回值,则必须将其更改为返回int值:

public int StoreMail(PhishingMail PhishingMail)
{
    using (var phishingMailStorage = new PhishFinderModel())
    {
        phishingMailStorage.PhishingMail.Attach(PhishingMail);

        phishingMailStorage.SaveChanges();
    }

    return someIdWhichYouNeedToFigureOut;
}

When making a method you define a return type. 制作方法时,您定义返回类型。 Void means it will not return anything. 无效意味着它将不返回任何东西。 You have defined your method as public void StoreMail() , thus saying it will not return anything. 您已将方法定义为public void StoreMail() ,因此说它不会返回任何内容。 When you call the method you ask for a return phishingMailId = _storageAgent.StoreMail(phishingMail) . 调用该方法时,您要求返回phishingMailId = _storageAgent.StoreMail(phishingMail) but because you defined the method as a void and you did not let the method return anything you can't get the id and you get the error. 但是因为您将该方法定义为void,并且没有让该方法返回任何内容,所以您无法获取ID,并且会收到错误消息。

To fix this you will have to make your method return an int 要解决此问题,您将必须使您的方法返回一个int

public int StoreMail(PhishingMail phishingMail){}

then further in the method you define what you want to return 然后在方法中进一步定义要返回的内容

return newId;

In my case I created stored procedure in the database SQL server , then i created a class in c# program and called this stored procedures and add the parameter in the stored procedures inside the class and return the data in the data table ,finally i can call my class any where in my program like this example : 以我为例,我在数据库SQL Server中创建了存储过程,然后在c#程序中创建了一个类,并调用了该存储过程,并在该类内部的存储过程中添加了参数,并返回了数据表中的数据,最后我可以调用我的班级在我的程序中的任何地方,例如以下示例:

1- Create Stored Procedure : 1-创建存储过程:

CREATE proc [dbo].[GET_SAMPLE_KIND]
@TESTID int
as 
select Dept_id,ID_sample from LabTests
where LabTests.TestId = @TESTID 

2- Create class in your C# program and call the stored procedure with parameters like this code : 2-在您的C#程序中创建类,并使用类似于以下代码的参数调用存储过程:

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;
        }

To connect and read from database you must make another class to contact sql server which is in my case DAL.selectData method to select data from database , also for insert and update and delete statements you can create stored procedures then create another void in your class for that functions. 要连接数据库并从中读取数据,您必须使另一个类与sql server联系,在本例中为DAL.selectData方法以从数据库中选择数据,对于插入,更新和删除语句,还可以创建存储过程,然后在类中创建另一个void该功能。

3- Finally you can call your class and stored procedures from your program. 3-最后,您可以从程序中调用类和存储过程。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM