2013-04-29 110 views
1

如果您在VB 2010中構建了一個HousePet的結構,並且您想訪問名爲Coat的結構元素,那麼將元素移動到變量Coat所需的代碼行是什麼?VB 2010元素到變量

Structure HousePet 
    Public _Breed as String 
    Public _PetCoat as String 
    Public _Cost as Double 
End Structure 

    Sub Main() 
     Dim HoundDog As HousePet 
     Dim Coat as String 
     **Insert Code Here** 
    End Sub 
+0

只需在一個側面說明,你正在使用錯誤的變量命名約定。規範是在局部變量前加下劃線(例如_SomeLocalVariable),而Public變量應該聲明爲不帶下劃線和Pascal大小寫。 因此,命名結構變量的更好的方法應該是'Breed','PetCoat'和'Cost'。儘管如此,這並不會損害您的程序。 – 2013-04-29 21:02:28

回答

2

首先,你需要結構體的類型的變量:

Private MyPet As HousePet 

然後你就可以像這樣訪問:

Coat = MyPet._PetCoat 
+1

+ 1該死!我遲到了! – 2013-04-29 20:37:57

+1

@SiddharthRout呃,憑藉純粹的機會,我會在短短一段時間內毆打你* :-) – RBarryYoung 2013-04-29 20:40:49

2
'Public _Cost as Double 
'A Typo? 
'Did you mean Public _Coat as Double 

Sub Main() 
    Dim HoundDog As HousePet 
    Dim Coat As String 

    Dim x As HousePet 

    Coat = x._Cost 'Or is it a typo? x._Coat? 
End Sub