2016-06-29 2 views
0

일부 VBA 코드를 VB.Net으로 변환하려고 시도하고 일부 클래스 문제가 끊깁니다. 나는 메인 클래스를 가지고있다 : clsComputer. 하위 클래스 clsHardDrive가 있습니다. 컴퓨터가 1 개 이상의 하드 드라이브를 가질 수 있기 때문에, Get 그리고 Set 속성은 clsComputer에서 다음과 같이 :VB.Net - 클래스의 하위 클래스 사용

Private pHardDrive(8) As clsHardDrive 

Public Property Get HardDrive(index As Integer) As clsHardDrive 
    If pHardDrive(index) Is Nothing Then Set pHardDrive(index) = New clsHardDrive 
    Set HardDrive = pHardDrive(index) 
End Property 
Public Property Set HardDrive(index As Integer, value As clsHardDrive) 
    pHardDrive(index) = value 
End Property 

이 날 코드에서 이런 일을 수행 할 수 있습니다

objComp.HardDrive(0).Size = "500" 
objComp.HardDrive(1).Size = "1000" 

을 그러나, 나는 이것을 VB.Net로 변환하는 방법을 모른다. 나는이 시도 :

Property HardDrive As HDD 
    Get (ByVal index As Integer) 
     Return pHardDrive(index) 
    End Get 
    Set (ByVal index As Integer, value As HDD)   
     pHardDrive(index) = value 
    End Set 
End Property 

를하지만 컴파일 오류가 있습니다 : 해당 오류를 검색하는 것은 매우 도움이되지 않습니다 Property without a 'ReadOnly' or 'WriteOnly' specifier must provide both a 'Get' and a 'Set'. (BC30124)

을 : To correct this error make sure you include both a Get procedure and a Set procedure between the Property statement and the End Property statement. I이 모두 가져 오기 및 설정, 그리고 나는 그들이 제대로 종료됩니다 생각합니다. 다른 클래스 내에서 하나의 클래스를 사용하는 것에 대한 예제도 검색했지만 유용한 것은 찾지 못했습니다.

VBA에있는 VB.Net에서 동일한 기능을 어떻게 얻을 수 있습니까? 예를 들어, 하나의 컴퓨터 오브젝트가 많은 하드 드라이브 오브젝트를 가질 수 있도록 HardDrive의 인스턴스를 작성하려면 어떻게해야합니까?

+0

은 여기 예를 살펴 https://msdn.microsoft.com/en-us/library/e8ae41a4.aspx –

+0

도현 되세요! 그 페이지에서 # 2 : '속성이 매개 변수를 사용하는 경우 프로 시저의 이름을 가진 Property 키워드 다음에 괄호 안의 매개 변수 목록을 따르십시오.'따라서 'Property HardDrive (Byval 인덱스 As Integer) As HDD'와 컴파일! 대답으로 게시하십시오. :) – Tim

+0

Reckon Cody는 노력을 기울였습니다. –

답변

2

고전적인 COM 기반 VB와 마찬가지로 VB.NET의 인덱스 속성 still exist.

방금 ​​틀린 구문이 있습니다. 인덱싱 된 속성은 실제로 매개 변수화 된 속성의 특별한 경우이므로 index은 개별 속성 GetSet 문이 아닌 전체 속성에 대한 매개 변수로 사용됩니다.

Public Class Computer 

    Private m_hardDrives(8) As HDD 

    Public Property HardDrive(ByVal index As Integer) As HDD 
     Get 
      Return m_hardDrives(index) 
     End Get 
     Set(ByVal value As HDD) 
      m_hardDrives(index) = value 
     End Set 
    End Property 

End Class 
관련 문제