Pass STRUCTURE as a parameter?

Mark Wills

Member
Joined
Jul 11, 2004
Messages
13
Location
Worldwide
Programming Experience
10+
Hi,

I have a class that raises events. I want the TYPE of the parameter to be a user defined type. Then, in the event handler, intellisense would recognise the typed parameter... However, it isn't working...

In .Net 2.0 TYPES have gone, so I've had to use a structure instead... However I'm sure it no longer works as it used to...

Here's a section of my code:

VB.NET:
Public Structure structTMS9900Register
        Dim PC As Integer
        Dim WP As Integer
        Dim ST As Integer
        Dim R0 As Integer
        Dim R1 As Integer
        Dim R2 As Integer
        Dim R3 As Integer
        Dim R4 As Integer
        Dim R5 As Integer
        Dim R6 As Integer
        Dim R7 As Integer
        Dim R8 As Integer
        Dim R9 As Integer
        Dim R10 As Integer
        Dim R11 As Integer
        Dim R12 As Integer
        Dim R13 As Integer
        Dim R14 As Integer
        Dim R15 As Integer
End Structure

    Public Event BreakpointAsserted(ByVal Register As structTMS9900Register, ByVal Asserted As Boolean)

... other code omitted...

Then, elsewhere (actually on my form) I consume the event advertised in the class:

VB.NET:
AddHandler tms9900.BreakpointAsserted, AddressOf BreakpointAsserted

Public Sub BreakpointAsserted(ByVal Register As cls9900.TMS9900.structTMS9900Register, ByVal Asserted As Boolean)
    ' breakpoint handler for break point assertions on address.
    ' Asserted=True causes the Breakpoint box to light up (white on red)
    ' Asserted=False restores normal colour
    Select Case Register
        Case <---- PROBLEM HERE
    End Select
End Sub

Note: cls9900 is my class, tms9900 is an instance of cls9900

As shown above, when I type register, intellisense is not showing me the members of the type (structure) structTMS9900Register, which leads me to think there is something wrong with the way I've coded it...

Any ideas? In the "old" days (VB6 and I'm *sure* earlier versions of VB.Net) you simply defined a type, and referenced as the type of the parameter you were passing... Intellisense would pick it up.

I'm a bit confused! Help!

If I enter:

Select Case Register
Case Register.PC

Then I get error: Operator '=' is not defines for types cls9900.TMS9900.structTMS9900Register and Integer

Is this a clue?

Thanks

Mark
 
Select [ Case ] testexpression
[ Case expressionlist

testexpression is compared with expressionlist

so what is it you want to compare? surely you don't want to compare Register and Register.PC? maybe you want to compare Register.PC with different integer values?

Both a Class and a Structure is "user defined types".
 
Select Case Register
Case Register.PC

Then I get error: Operator '=' is not defines for types cls9900.TMS9900.structTMS9900Register and Integer

Is this a clue?

Absolutely, it's a huge clue. Think of "Select Case" as an "If" statement. This is what your above code looks like:

VB.NET:
If Register = Register.PC Then
    ' ...
End If

Register is a class while PC is an integer member of that class. That would be like doing this (humor me for a moment and imagine in the following example Tree and Apple are tangible objects.) :

VB.NET:
If Tree = Tree.Apple Then
    ' never gonna happen...
End If

I'm not sure if you're really looking for an Enum here, or if you are wanting to compare between "PC", "WP", and "ST" and the rest of the variables are properties regardless of the type.

If the latter is true, why not just add a variable ("RegisterType" for example) to your Structure (structTMS9900Register) which references an Enum. This could make your class look like this:

VB.NET:
Public Structure structTMS9900Register

        Enum eRegisterType
            PC
            WP
            ST
            ' ...
        End Enum

        Dim RegisterType As eRegisterType

        Dim R0 As Integer
        Dim R1 As Integer
        Dim R2 As Integer
        Dim R3 As Integer
        Dim R4 As Integer
        Dim R5 As Integer
        Dim R6 As Integer
        Dim R7 As Integer
        Dim R8 As Integer
        Dim R9 As Integer
        Dim R10 As Integer
        Dim R11 As Integer
        Dim R12 As Integer
        Dim R13 As Integer
        Dim R14 As Integer
        Dim R15 As Integer
End Structure

Then you would do a:

VB.NET:
Public Sub BreakpointAsserted(ByVal Register As cls9900.TMS9900.structTMS9900Register, ByVal Asserted As Boolean)
    ' breakpoint handler for break point assertions on address.
    ' Asserted=True causes the Breakpoint box to light up (white on red)
    ' Asserted=False restores normal colour
    Select Case Register.RegisterType
        Case structTMS9900Register.eRegisterType.PC
            ' ...
        Case structTMS9900Register.eRegisterType.WP
            ' ...
        Case structTMS9900Register.eRegisterType.ST
            ' ...
        Case Else
            ' impossible in this context but I added a Case Else anyway
    End Select
End Sub

You could make the Enum global if you don't want to reference the base class every time you want to compare (i.e. shorter code ... in this example I chose to not make the Enum global, so we have to use structTMS9900Register.eRegisterType.Other, whereas if the Enum was global/public, outside of the class, it would just be eRegisterType.Other ... a different way around this would be to import structTMS9900Register, but you'd then need to make it a Class instead of a Structure. I try to avoid structures and use classes, but not for any major reason).

Now, on the other hand, if "PC", "WP", "ST" and all your "R0" through "R12" are all "types" by your definition, you are probably just looking for an Enum all by itself, like:

VB.NET:
Enum eTMS9900Register
    PC
    WP
    ST
    R0
    R1
    R2
    R3
    R4
    R5
    R6
    R7
    R8
    R9
    R10
    R11
    R12
End Enum
 
Last edited:
I'm so stupid!

OF COURSE!!! Enum!! My GOD I am so stupid. How could I forget that? :eek: I think I'm getting a bit old :( Thanks very much for taking the time to post and refresh my fading memory. Much appreciated.

Mark
 
Back
Top