vb.net

2014-09-22 3 views
0

net에 문자열 값이있는 이름으로 변수에 액세스하려고합니다. 이름으로 문자열 값에서 변수에 액세스하려고합니다.vb.net

클래스 유형으로 A1, A2, A3과 같은 클래스 유형의 거의 50 가지 변수를 만들었습니다. 또한 데이터 테이블에 50 개의 항목 목록이 있습니다. 내가 원하는 것은 그

dim dr as datarow 
    for each dr in dt.rows 
    dim newstring as string = dr(0).tostring 
    'Here I have the problem, newstring will return A1 or A2 or A3 and I dont want to use if-then or Select case statements becose 
it will lead me toward hundreds of lines. I want to access newstring.property=something 

    Next 

문제와 같은 접근은 위의 문자열 값을 반환하고 내가 변수를 반환 newstring 인수로 쓰여진 같은 이름을 선언 한 것입니다 경우. newstring이라는 이름을 가진 변수에만 액세스하려고합니다. 도와주세요.

당신은 각 좌석으로 채우기 각 좌석

Private Class Seat 
    Public Property Name As String 
    Public Property IsReserved As Boolean 
    Public Sub New(ByVal name As String) 
     Me.Name = name 
    End Sub 
End Class 

에 대한 목록을하는 클래스를 만들어야합니다

+2

대신 변수로 그들을 만들 수있는 충분한 이유가 있습니까? f를 사용하여 연관 배열 (예 :. 대신 해시 테이블)? –

+0

실제로는 좌석 배치 유형이지만 좌석 번호는 순서가 맞지 않으며 isvacant와 같은 속성을가집니다. 장래에 액세스하려면 예약 번호 등이 있습니다. 다른 쉬운 접근 방법을 제안하면 감사하겠습니다. – Prince

답변

0

'create your seats 
Dim seats As New List(Of Seat) 
seats.Add(New Seat("A3")) 
seats.Add(New Seat("B6")) 
seats.Add(New Seat("A1")) 
'etc. 

그런 다음 당신은 LINQ 식을 사용할 수 있습니다 (이 당신이 원하는 어떤 순서로 할 수 있습니다) 데이터베이스의 이름과 일치하는 자리를 찾으십시오.

'find the seat in the list that matches the name 
Dim dr As DataRow 
For Each dr In dt.rows 
    Dim newstring As String = dr(0).ToString 
    Dim seat = seats.Find(Function(x) x.Name = newstring) 
    seat.Property = Something 
Next 
+0

선생님 고맙습니다. 제 문제를 다시 해결해 주셔서 감사합니다. – Prince