Click here to Skip to main content
15,891,875 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I get an error 438 (Object doesn't support this property or method) on the following If evaluation.

I've already tried "For Each prtr As String" and "For Each prtr As Object"

If if I code "If doav(i).d1" or "If doav(i).d42", etc it goes well...

d1, d42, etc are boolean fields




VB
Public Class ordem
             
        Public d1 As Boolean
        Public d42 As Boolean
        Public d123 As Boolean
        Public d1027 As Boolean
        Public d1089 As Boolean
        Public d2109 As Boolean
        Public d4130 As Boolean
        Public lei As Boolean
End Class


VB
Dim oa1 As New ordem
Dim oa2 As New ordem
Dim oa3 As New ordem
Dim oa4 As New ordem
Dim ports As Object = {"d1", "d42", "d123", "d1027", "d1089", "d2109", "lei", "d4130"}
Dim doav(3) As Object
doav(0) = oa1
doav(1) = oa2
doav(2) = oa3
doav(3) = oa4


VB
For Each prtr As String In ports
    For i As Integer = 0 To A - 1
       If doav(i).prtr = True Then
           do something
       End If
    Next
Next
Posted
Updated 13-Nov-15 2:14am
v2
Comments
Nosfa 13-Nov-15 7:32am    
A is a variable passed from another function ranging betwen 1 and 4
Richard MacCutchan 13-Nov-15 7:58am    
Why are you declaring the array as Object rather than String? And what is doav? And how do you think the expression doav(i)."some string" can be evaluated?

You can't access properties like that. Your code is looking for a property called prtr on doav(i), it isn't going to use the string value of a local variable you've defined called prtr. If I did this;

Dim t as String = MyTextBox.Text


then you'd expect t to have the value of the Text property of MyTextBox. If I did this
Dim Text as String = "Length"
Dim t as String = MyTextBox.Text


the compiler isn't going to work out that Text is also a local variable so totally change how the code is compiled and instead use whatever value is in Text to interpret the property at run-time. and return the Length instead.

If you have the names of properties as strings and you want to access the value of that property on an object you have to use reflection

http://stackoverflow.com/questions/1196991/get-property-value-from-string-using-reflection-in-c-sharp[^]

If you can't work out how to convert that to vb.net then google "read property via reflection vb.net" for other examples.
 
Share this answer
 
v2
Comments
Nosfa 13-Nov-15 9:12am    
thanks
I don't know what you want to achieve, but i guess, that you want to check the value of custom class property by it's name.

Check this example:
VB
Sub Main
	
	Dim ports As String() = {"d1", "x4"}
	
	Dim i As Integer = 0
	Dim myType As Type = GetType(Whatever)
	Dim myPropInfo As PropertyInfo = Nothing

	Try
		For i = 0 To 1
			myPropInfo = myType.GetProperty(ports(i))
			Console.WriteLine("The property '{0}' of 'Whatever' class exists! ", myPropInfo.Name)
		Next 
	Catch ex As NullReferenceException
		Console.WriteLine("The property '{0}' of 'Whatever' class does not exists!", ports(i))
	End Try

End Sub

' class definition
Public Class Whatever
	Private bd1 As Boolean = False
	
	Public Property d1 As Boolean
		Get
			Return bd1
		End Get
		Set (value As Boolean)
			bd1 = value
		End Set
	End Property

End Class


Result:
The property 'd1' of 'Whatever' class exists! 
The property 'x4' of 'Whatever' class does not exists!


To get property value, use this:
VB
Console.WriteLine(myPropInfo.GetValue(w, Reflection.BindingFlags.GetProperty, Nothing, Nothing, Nothing)) 

where w is an instance of Whatever class.
Returns:
False


For further information, please see:
Type.GetProperty[^]
PropertyInfo.GetValue Method (Object, Object())[^]

[EDIT]
Note: answer has been updated accordingly to changes made by OP.

In your case i'd suggest to use Dictionary class[^] as follow:
VB
'declare and initialize a list of dictionaries
Dim doav As List(Of Dictionary(Of String, Boolean)) = New List(Of Dictionary(Of String, Boolean))
    'create single dictionary
Dim myDict As Dictionary(Of String, Boolean) = New Dictionary(Of String, Boolean)
    'add values
myDict.Add("d1", False)
myDict.Add("d42", True)
myDict.Add("d123", False)
myDict.Add("d1027", False)
myDict.Add("d1089", True)
myDict.Add("d2109", False)
myDict.Add("d4130", False)
myDict.Add("lei", True)

    'add dictionary to the list of dictionaries
doav.Add(myDict)

    'define dictionary index
Dim i As Integer = 0
    'get single dictionary object
myDict = doav(i)
'check values for each key
For Each k In myDict.Keys
    Console.WriteLine("The value of '{0}' Key is: {1}", k, myDict(k))
Next

Returns:
The value of 'd1' Key is: False
The value of 'd42' Key is: True
The value of 'd123' Key is: False
The value of 'd1027' Key is: False
The value of 'd1089' Key is: True
The value of 'd2109' Key is: False
The value of 'd4130' Key is: False
The value of 'lei' Key is: True

[/EDIT]
 
Share this answer
 
v2

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900