无法将类型“ void”隐式转换为“ int”? [英] Cannot implicitly convert type 'void' to 'int'?

查看:489
本文介绍了无法将类型“ void”隐式转换为“ 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();

        }

然后在我的processPhishingMail方法中,它调用Store方法。但是然后我得到一个错误:无法将类型'void'隐式转换为'int'。

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

所以我真的找不到找到将void转换为int的解决方案。也许不可能吗?

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.

推荐答案

方法

public void StoreMail(PhishingMail PhishingMail)

不返回任何值。因此,当您这样做时:

is not returning any value. So when you do:

phishingMailId = _storageAgent.StoreMail(phishingMail);

您将收到此错误,因为 StoreMail 被声明为 void ,这意味着不会返回任何内容。

You are getting that error, since StoreMail is declared as void, which means it does not return anything.

要解决此问题,只需像这样调用 StoreMail

To fix this, simply call StoreMail like this:

_storageAgent.StoreMail(phishingMail);

如果要从中返回值 StoreMail ,则必须更改它以返回 int 值:

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

这篇关于无法将类型“ void”隐式转换为“ int”?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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