hurnhu
hurnhu

Reputation: 936

Declaration expected when accessing structure variable VB

What i am trying to do is just make an VB structure, and then assign things to the elements in the structure.

Structure Computer
    Public strModel As String
    Public dblCost As Double
End Structure

Public Class Form1

    Dim homeUse As Computer
    homeUse.strModel = "IB-50"
    homeUse.dblCost = 2400

End Class

from what i can tell in this code it is correct, but according to visual studios.

   homeUse.strModel = "IB-50"
   homeUse.dblCost = 2400

are incorrect because of the error "Declaration expected"

Upvotes: 1

Views: 435

Answers (1)

James Thorpe
James Thorpe

Reputation: 32212

You can't set the fields of the structure inline in the class declaration like that. It would need to be inside a method, for instance the constructor:

Public Class Form1
    Dim homeUse As Computer

    Sub New()
        homeUse.strModel = "IB-50"
        homeUse.dblCost = 2400
    End Sub
End Class

Upvotes: 3

Related Questions