错误:“long”类型的值无法转换为“integer”。
在此代码中,long的转换未转换为long,请帮助解决此问题,错误位置以粗体字显示。
In this code that the conversion of the long is not converted in to the long so please help to solve this the error position is shown in the bold character .
Public Function GetFacetCount(reader As IndexReader, queryFilterOnMainQuery As Filter) As Int32
Dim bs = GetValueBitset(reader)
bs.InPlaceAnd(queryFilterOnMainQuery.GetDocIdSet(reader).Iterator())
If DirectCast(bs.Cardinality(), Int32) >= 1 Then
Return DirectCast(bs.Cardinality(), Int32)
Else
Return 0
End If
End Function
End Class
我尝试过:
Dim bs = GetValueBitset(读者)
bs.InPlaceAnd(queryFilterOnMainQuery.GetDocIdSet(reader).Iterator())
Dim Cardinality As Integer = Convert.ToInt32(bs.Cardinality())
如果基数> = 1那么
返回基数
结束如果
What I have tried:
Dim bs = GetValueBitset(reader)
bs.InPlaceAnd(queryFilterOnMainQuery.GetDocIdSet(reader).Iterator())
Dim Cardinality As Integer = Convert.ToInt32(bs.Cardinality())
If Cardinality >= 1 Then
Return Cardinality
End If
回报是什么? bs.Cardinality()的ype?我假设很长?
因为您将缩小值(从64位到32位),您需要使用CType(参见链接)。此外,您必须确保在缩小后Long值不会溢出Integer值,否则它仍然会失败。
CType函数(Visual Basic) [ ^ ]
上面可能会修复它,但是......你需要问问自己为什么你试图强制结果类型可能是不兼容的类型?如果函数的结果是Long,为什么不将它存储并用作Long?创建函数的人返回Long值可能是因为该值可能非常大。因为您只是检查值是大于还是等于1,您可以轻松使用Long或任何结果类型没有任何问题。如果绝对必要,您应该只使用缩小或加宽。
祝您好运!
What is the return type of bs.Cardinality()? I assume a Long?
Because you would then be narrowing the value (from 64bit to 32bit) you need to use CType (see link). Also, you must ensure that the Long value doesn't overflow the Integer value after narrowing because it would still fail otherwise.
CType Function (Visual Basic)[^]
Above would maybe "fix" it, but... You need to ask yourself why you try to force the result type in a possible incompatible type? If the result of the function is a Long, why not store and use it as a Long? The person who created the function returns a Long value probably because the value could be very large. Because you simply check if the value is larger or equal to one you can easily use a Long or whatever the result type is without any problems. You should only use narrowing or widening if absolutely necessary.
Good luck!
您是否尝试过使用
Have you tried to use
Return CInt(bs.Cardinality())
在VB中进行类型转换??
Ref。:类型转换函数(Visual Basic) [ ^ ]