开发者

VB.NET: Intellisense on items declared as 'Object'

开发者 https://www.devze.com 2023-01-19 04:06 出处:网络
I have a shared Queue that I use to dump various messages contained in different Classes, so I have its type as Object.

I have a shared Queue that I use to dump various messages contained in different Classes, so I have its type as Object.

The prob is, the messages are dequeued and processed but since the message type vary, Intellisense doesn't show the classes properties or methods.

How doe开发者_StackOverflows one find get Intellisense to work to?


You will need to provide a common base class or interface that these various message classes share. So instead of a queue of Objects, you would instead have a queue of MessageBase or IMessage. The methods and properties you wish to access would need to be defined within the base/interface. Intellisense would then show those properties and methods (but not the additional properties/methods you define within each class).


There are two Queue classes in the .NET Framework, the difference is they were released at different times - one is newer. Use the strongly typed Queue(Of T) shown in the second list item below to achieve Intellisense and to have a strongly typed instance you can program against easily...

Code sample

Dim numbers As New Queue(Of String)

The two Queue classes:

  1. System.Collections.Queue

    • Intellisense shows an Object (Likely this is your problem).
    • from .NET 1.0
    • results in weakly typed Object elements
  2. System.Collections.Generic.Queue(Of T)

    • Intellisense will show the strongly type T members (Use this instead)
    • from .NET 2.0
    • results in strongly typed (Of T) elements where you specify T

Use the links to visit the documentation, put the doc page in VB syntax mode, and scroll down to the Examples section to see its usage.


Other

If you're using a Queue different from the ones mentioned above, you can always Convert/cast the dequeued objects back into their strongly-typed values using mechanisms like the following:

  • System.Convert
  • How should I cast in VB.NET?
0

精彩评论

暂无评论...
验证码 换一张
取 消