daisy
daisy

Reputation: 23581

Can't use Type in VB

Is there anything wrong with the following code ? It failed on Form_Load() line , and complains about it.

Private Sub Form_Load()

    Type Human
        Name As String
    End Type

    Dim stu As Student
    With Human:
        .Name = "Someone"
    End With

    Debug.Print ("Name: " & stu.Name)

End Sub

Upvotes: 0

Views: 134

Answers (2)

UnhandledExcepSean
UnhandledExcepSean

Reputation: 12804

The keyword is no longer Type; it is Structure now. Type was used in VB6 and earlier, but not in .NET.

Upvotes: 0

Richard
Richard

Reputation: 6354

You have two options:

1

Create a new class

Private Class Human
    Public Name As String
End Class

(Obviously it would be better to wrap the Name in a public property, but for simplicity, exposing it as a public variable is easier.)

2

Create a new struct:

Structure Human
    Dim Name As String
End Structure

Note
It should be noted that both of these options must be done outside of the function, not within Form_Load function

Upvotes: 2

Related Questions