使用变量作为类的字段的错误(Object不支持此属性或方法) [英] error using variable as field of class (Object doesn't support this property or method)

查看:111
本文介绍了使用变量作为类的字段的错误(Object不支持此属性或方法)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我收到错误438(对象不支持此属性或方法)以下如果评估。



我已经尝试过For Each prtr作为字符串和为每个prtr作为对象



如果我编码如果doav(i).d1或如果doav(i).d42等等它很顺利...



d1,d42等是布尔字段









 公共  Class  ordem 

公共 d1 作为 布尔
公开 d42 作为 布尔
公开 d123 作为 布尔
公开 d1 027 作为 布尔
公开 d1089 作为 布尔
公开 d2109 作为 布尔
公开 d4130 作为 布尔
公开 lei As Boolean
End Class





  Dim  oa1  As   ordem 
Dim oa2 As N ew ordem
Dim oa3 As ordem
Dim oa4 As ordem
Dim ports 作为 Object = { d1 d42 d123 d1027 d1089 d2109 lei < span class =code-string> d4130}
< span class =code-keyword> Dim doav( 3 as 对象
doav( 0 )= oa1
doav( 1 )= oa2
doav( 2 )= oa3
doav( 3 )= oa4





  For  每个 prtr 作为 字符串 端口
对于 i As 整数 = 0 A - < span class =code-digit> 1
如果 doav(i).prtr = 然后
执行某事
结束 如果
下一步
下一步

解决方案

您无法访问此类属性。你的代码在doav(i)上寻找一个名为prtr的属性,它不会使用你定义的名为prtr的局部变量的字符串值。如果我这样做了;



 Dim t as String = MyTextBox.Text 





然后你希望得到MyTextBox的Text属性的值。如果我这样做

 Dim Text as String =Length
Dim t as String = MyTextBox.Text





编译器不会发现Text也是一个局部变量,因此完全改变了代码的编译方式,而是使用Text中的任何值来解释运行时的属性 - 时间。而是返回长度。



如果您将属性的名称作为字符串,并且您想要在对象上访问该属性的值,则必须使用反射< br $> b $ b

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



如果你无法解决如何将其转换为vb.net然后谷歌通过反射vb.net读取属性的其他例子。


我不知道你想要实现什么,但我想,你想通过它的名字来检查自定义类属性的值。



查看这个例子:

  Sub  Mai n 

Dim ports As String ()= { d1 x4}

Dim i 作为 整数 = 0
Dim myType As Type = GetType (随便)
Dim myPropInfo As PropertyInfo = Nothing

尝试
对于 i = 0 1
myPropInfo = myType.GetProperty(ports ( i))
Console.WriteLine( 'Whatever'类的属性'{0}'存在! ,myPropInfo.Name)
下一步
Catch ex < span class =code-keyword> As NullReferenceException
Console.WriteLine( 该属性Whatever类的{0}不存在!,ports(i))
结束 尝试

结束 Sub

' 类定义
公开 无论
私有 bd1 正如 布尔 = 错误

公共 属性 d1 As 布尔
获取
返回 bd1
结束 获取
设置(值作为 布尔
bd1 =值
结束 设置
结束 属性

结束 Class





结果:

'Whatever'类的属性'd1'存在! 
'Whatever'类的属性'x4'不存在!





要获取属性值,请使用:

 Console.WriteLine(myPropInfo.GetValue(w,Reflection.BindingFlags.GetProperty, Nothing  Nothing  Nothing ))



其中 w 是一个的实例,无论类。

返回:

错误





如需了解更多信息,请参阅:

Type.GetProperty [ ^ ]

PropertyInfo.GetValue Method(Object,Object()) [ ^ ]





注意:答案已根据OP的变化进行了相应更新。



在您的情况下,我建议使用词典类 [ ^ ]如下:

 ' 声明并初始化词典列表 
Dim doav 作为列表( 字典( 字符串布尔))= 列表( 字典( <温泉n class =code-keyword> String , Boolean ))
' 创建单个字典
Dim myDict As 字典( 字符串布尔)= 字典( 字符串布尔
' 添加值
myDict.Add( d1错误
myDict.Add( d42 True
myDict.Add( d123错误
myDict.Add( d1027 False
myDict.Add( d1089 True
myDict.Add( d2109 False
myDict.Add( d4130 False
myDict.Add( lei True

' 将词典添加到词典列表
doav.Add(myDict)

' 定义字典索引
Dim i 作为 整数 = 0
' 获取单个字典对象
myDict = doav(i)
' 检查每个键的值
对于 每个 k myDict.Keys
Console.WriteLine(< span class =code-string>
{0}键的值为:{1},k,myDict(k) )
下一步



退货:

< pre lang =text>'d1'键的值是:False
'd42'键的值是:True
'd123'键的值是:False
值'd1027'键是:False
'd1089'键的值是:True
'd2109'键的值是:False
'd4130'键的值是: F A lse
'lei'Key的值为:True



[/ EDIT]


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




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



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



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

解决方案

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.


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:

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:

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:

'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]


这篇关于使用变量作为类的字段的错误(Object不支持此属性或方法)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆