简体   繁体   中英

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. But then I get the error: Cannot implicitly convert type 'void' to '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. 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.

To fix this, simply call StoreMail like this:

_storageAgent.StoreMail(phishingMail);

If you intend to return a value from StoreMail , then you must change it to return an int value:

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. When you call the method you ask for a return 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.

To fix this you will have to make your method return an 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 :

1- Create Stored Procedure :

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 :

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.

3- Finally you can call your class and stored procedures from your program.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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