Home > database >  BC36913: Cannot infer a common type
BC36913: Cannot infer a common type

Time:11-30

Here's my code:

    Private Const CFE_LINK As UInt32 = &H20

    Public Sub SetSelectionLink(ByVal link As Boolean)
        SetSelectionStyle(CFM_LINK, If(link, CFE_LINK, 0))
    End Sub
    Public Function GetSelectionLink() As Integer
        Return GetSelectionStyle(CFM_LINK, CFE_LINK)
    End Function

    Private Sub SetSelectionStyle(ByVal mask As UInt32, ByVal effect As UInt32)
        Dim cf As CHARFORMAT2_STRUCT = New CHARFORMAT2_STRUCT()
        cf.cbSize = CUInt(Marshal.SizeOf(cf))
        cf.dwMask = mask
        cf.dwEffects = effect
        Dim wpar As IntPtr = New IntPtr(SCF_SELECTION)
        Dim lpar As IntPtr = Marshal.AllocCoTaskMem(Marshal.SizeOf(cf))
        Marshal.StructureToPtr(cf, lpar, False)
        Dim res As IntPtr = SendMessage(Handle, EM_SETCHARFORMAT, wpar, lpar)
        Marshal.FreeCoTaskMem(lpar)
    End Sub

I got an error (Cannot infer a common type for the first and second operands of the binary 'If' operator) on that line:

SetSelectionStyle(CFM_LINK, If(link, CFE_LINK, 0))

CodePudding user response:

The type for the constant 0 is Integer. I think the error is because the compiler can't tell whether it should use Integer or UInt32 as the result type; they're both integer types with the same bit width, the only difference is the upper and lower bounds.

As you've noted, you can use an explicit conversion to make both operands to If have the same type.

You can also use the appropriate type suffix to make the constant 0 have the right type. In this case, the following should work:

SetSelectionStyle(CFM_LINK, If(link, CFE_LINK, 0UI))

The UI suffix tells the compiler to treat the 0 as a UInteger (which is the same type as UInt32) instead of an Integer.

CodePudding user response:

Ok...Following the MSDN Documentation, I tried this and the compilator seems ok.

SetSelectionStyle(CFM_LINK, If(link, CFE_LINK, CUint(0)))
  • Related