![](/img/trans.png)
[英]How to fix "Cannot Convert from 'bool' Customer.Data.SelectTransactionViewModel"
[英]How to fix "cannot convert from 'bool' to 'string'" error?
我正在嘗試修改現有的不和諧機器人,當我嘗試運行它時出現此錯誤。 當它驗證消息是否從機器人發送時,我試圖做一個例外。
private async Task MessageReceivedHandler(SocketMessage msg)
{
try
{
if (msg.Author.IsBot || !_bot.Ready.Task.IsCompleted) //no bots, wait until bot connected and initialized
throw new ArgumentNullException(msg.Author.Id == 1234)
return;
if (!(msg is SocketUserMessage usrMsg))
return;
#if !GLOBAL_NADEKO
// track how many messagges each user is sending
UserMessagesSent.AddOrUpdate(usrMsg.Author.Id, 1, (key, old) => ++old);
#endif
var channel = msg.Channel as ISocketMessageChannel;
var guild = (msg.Channel as SocketTextChannel)?.Guild;
await TryRunCommand(guild, channel, usrMsg).ConfigureAwait(false);
}
catch (Exception ex)
{
_log.Warn("Error in CommandHandler");
_log.Warn(ex);
if (ex.InnerException != null)
{
_log.Warn("Inner Exception of the error in CommandHandler");
_log.Warn(ex.InnerException);
}
}
}```
你有幾個問題。
第一的:
throw new ArgumentNullException(msg.Author.Id == 1234)
ArgumentNullException
的構造ArgumentNullException
采用一個string
,該string
應該是“導致異常的參數的名稱”。 你給它一個bool
。
但是你只有一個參數,通過這一行你已經知道它不是空的。 所以也許只是ArgumentException
更合適,寫一個句子描述為什么這個參數是不可接受的。
其次,我認為這不會像你認為的那樣做:
if (msg.Author.IsBot || !_bot.Ready.Task.IsCompleted) //no bots, wait until bot connected and initialized
throw new ArgumentNullException(msg.Author.Id == 1234)
return;
如果if
語句后面沒有{ }
,則只有下一行被視為條件代碼的一部分。 (這適用於for
、 foreach
等 - 在此處閱讀有關“嵌入式語句”的更多信息)
換句話說,這相當於你所擁有的:
if (msg.Author.IsBot || !_bot.Ready.Task.IsCompleted) {
throw new ArgumentNullException(msg.Author.Id == 1234)
}
return;
它要么拋出異常,要么返回。 返回后的所有代碼將永遠不會到達。 我不認為那是你想要的。
這是我總是在單行代碼塊周圍使用大括號的原因之一。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.