我有一個包含兩個運營商在VB.NET編譯的組件:平等和賦值運算符
Public Shared Operator =(quarterA As CalendarQuarter, quarterB As CalendarQuarter) As Boolean
Return quarterA.StartDate = quarterB.StartDate AndAlso
quarterA.EndDate = quarterB.EndDate AndAlso
quarterA.Quarter = quarterB.Quarter
End Operator
Public Shared Operator <>(quarterA As CalendarQuarter, quarterB As CalendarQuarter) As Boolean
Return Not (quarterA = quarterB)
End Operator
然而,在C#中使用的組件來執行平等時檢查if (qtr != null)
收到錯誤:
Cannot implicitly convert type 'object' to 'bool'
用法在C#MVC4,剃刀:
@{Html.BeginForm();}
<div class="ui-form ui-form-horizontal form-width-narrow">
<div class="title">
Choose a Quarter</div>
<div class="group">
<label><strong>Control</strong></label>
<div class="field">
@Html.DropDownListFor(x => x.Quarter, new SelectList(Model.AvailableQuarters))
<input value="Select" class="ui-button" type="submit" />
</div>
</div>
@if (Model.Quarter != null) {
// Error in the above statement
}
</div>
@{Html.EndForm();}
我需要做些什麼來使相等運算符正常運行?
你爲什麼在談論轉讓?您的代碼不包含任何分配。也不可能超載分配。 – CodesInChaos
你能否給出一個簡短但完整的例子(VB和C#)來演示這個問題? –
@CodesInChaos,你是對的。我再次通過了代碼,並將它誤認爲是另一個無關的位。感謝您指出了這一點。 –