Classes & For Each Loop

  • Thread starter Thread starter D Miller
  • Start date Start date
D

D Miller

Here is today's challange...


I would like to scan though the properties of the class and take actions
based on the name of the property.


Something Like

Dim Work as new Test
dim Item as ????

for each Item in Work
select case Item
case "FName"
....
case "LName"
....
end select
Next




That is the general idea of what I am trying to accomplish, but I obviouly
need the syntax... Is this possible and if so how?

Thanks,
David



Public Class Test

Private mFName as string
Private mLName as string.

Property FName as string
get
return mFName
end get
set (byval Value as string)
mFName = value
end set
end property

Property LName as string
get
return mLName
end get
set (byval Value as string)
mMName = value
end set
end property
end class
 
I would like to scan though the properties of the class and take actions
based on the name of the property.

You need to look into Reflection. This is the name that .NET gives to
"examining .NET". Check out the PropertyInfo class in the System.Reflection
namespace, and then take a look at the GetProperties method of the Type
class.
 
Hi,

Test class

Public Class MyTestClass

Dim mstrA As String
Dim mstrB As String
Dim mintC As Integer

Public Property A() As String
Get
Return mstrA
End Get
Set(ByVal Value As String)
mstrA = Value
End Set
End Property

Public Property B() As String
Get
Return mstrB
End Get
Set(ByVal Value As String)
mstrB = Value
End Set
End Property

Public Property C() As Integer
Get
Return mintC
End Get
Set(ByVal Value As Integer)
mintC = Value
End Set
End Property
End Class

To get properties from class

Dim t As Type = GetType(MyTestClass)
For Each pi As Reflection.PropertyInfo In t.GetProperties
Debug.WriteLine(String.Format("{0} {1}", pi.Name,
pi.PropertyType.FullName))
Next

Ken
-----------------------------

D Miller said:
You don't happen to have an example somewhere do you?



"Jeff Johnson [MVP:VB]" <HYPERLINK
"mailto:[email protected]"(e-mail address removed)> wrote in message
"D Miller" <HYPERLINK
"mailto:[email protected]"(e-mail address removed)> wrote in message


You need to look into Reflection. This is the name that .NET gives to
"examining .NET". Check out the PropertyInfo class in the
System.Reflection

namespace, and then take a look at the GetProperties method of the
Type
class.
 
Thanks for the help, this solves half the problem of giving me the
properties in the class. Is there some way I can also get the value of the
property?

Thanks


Ken Tucker said:
Hi,

Test class

Public Class MyTestClass

Dim mstrA As String
Dim mstrB As String
Dim mintC As Integer

Public Property A() As String
Get
Return mstrA
End Get
Set(ByVal Value As String)
mstrA = Value
End Set
End Property

Public Property B() As String
Get
Return mstrB
End Get
Set(ByVal Value As String)
mstrB = Value
End Set
End Property

Public Property C() As Integer
Get
Return mintC
End Get
Set(ByVal Value As Integer)
mintC = Value
End Set
End Property
End Class

To get properties from class

Dim t As Type = GetType(MyTestClass)
For Each pi As Reflection.PropertyInfo In t.GetProperties
Debug.WriteLine(String.Format("{0} {1}", pi.Name,
pi.PropertyType.FullName))
Next

Ken
-----------------------------

D Miller said:
You don't happen to have an example somewhere do you?



"Jeff Johnson [MVP:VB]" <HYPERLINK
"mailto:[email protected]"(e-mail address removed)> wrote in message
"D Miller" <HYPERLINK
"mailto:[email protected]"(e-mail address removed)> wrote in message
I would like to scan though the properties of the class and take
actions
based on the name of the property.
You need to look into Reflection. This is the name that .NET gives to
"examining .NET". Check out the PropertyInfo class in the
System.Reflection

namespace, and then take a look at the GetProperties method of the
Type
class.
 
Maybe I should better describe what I'm trying to accomplish to. What I
would like to do is scan through the properties of several different classes
and writing the values to a database depending on the piticular property
that is being read.



D Miller said:
Thanks for the help, this solves half the problem of giving me the
properties in the class. Is there some way I can also get the value of the
property?

Thanks


Ken Tucker said:
Hi,

Test class

Public Class MyTestClass

Dim mstrA As String
Dim mstrB As String
Dim mintC As Integer

Public Property A() As String
Get
Return mstrA
End Get
Set(ByVal Value As String)
mstrA = Value
End Set
End Property

Public Property B() As String
Get
Return mstrB
End Get
Set(ByVal Value As String)
mstrB = Value
End Set
End Property

Public Property C() As Integer
Get
Return mintC
End Get
Set(ByVal Value As Integer)
mintC = Value
End Set
End Property
End Class

To get properties from class

Dim t As Type = GetType(MyTestClass)
For Each pi As Reflection.PropertyInfo In t.GetProperties
Debug.WriteLine(String.Format("{0} {1}", pi.Name,
pi.PropertyType.FullName))
Next

Ken
-----------------------------

D Miller said:
You don't happen to have an example somewhere do you?



"Jeff Johnson [MVP:VB]" <HYPERLINK
"mailto:[email protected]"(e-mail address removed)> wrote in message


"D Miller" <HYPERLINK
"mailto:[email protected]"(e-mail address removed)> wrote in message


I would like to scan though the properties of the class and take
actions
based on the name of the property.


You need to look into Reflection. This is the name that .NET gives to
"examining .NET". Check out the PropertyInfo class in the

System.Reflection

namespace, and then take a look at the GetProperties method of the
Type
class.
 
Hi,


Dim c As New MyTestClass
c.A = "a"
c.B = "B"
c.C = 100

Dim t As Type = c.GetType
For Each pi As Reflection.PropertyInfo In t.GetProperties
Debug.WriteLine(String.Format("{0} {1} {2}", pi.Name, _
pi.PropertyType.FullName, pi.GetValue(c, Nothing)))
Next

Ken
---------------------

D Miller said:
Maybe I should better describe what I'm trying to accomplish to. What I

would like to do is scan through the properties of several different
classes
and writing the values to a database depending on the piticular property

that is being read.



"D Miller" <HYPERLINK
"mailto:[email protected]"(e-mail address removed)> wrote in message
Thanks for the help, this solves half the problem of giving me the
properties in the class. Is there some way I can also get the value
of
the

property?

Thanks


"Ken Tucker [MVP]" <HYPERLINK
"mailto:[email protected]"(e-mail address removed)> wrote in message
Hi,

Test class

Public Class MyTestClass

Dim mstrA As String
Dim mstrB As String
Dim mintC As Integer

Public Property A() As String
Get
Return mstrA
End Get
Set(ByVal Value As String)
mstrA = Value
End Set
End Property

Public Property B() As String
Get
Return mstrB
End Get
Set(ByVal Value As String)
mstrB = Value
End Set
End Property

Public Property C() As Integer
Get
Return mintC
End Get
Set(ByVal Value As Integer)
mintC = Value
End Set
End Property
End Class

To get properties from class

Dim t As Type = GetType(MyTestClass)
For Each pi As Reflection.PropertyInfo In t.GetProperties
Debug.WriteLine(String.Format("{0} {1}", pi.Name,
pi.PropertyType.FullName))
Next

Ken
-----------------------------

"D Miller" <HYPERLINK
"mailto:[email protected]"(e-mail address removed)> wrote in message

You don't happen to have an example somewhere do you?



"Jeff Johnson [MVP:VB]" <HYPERLINK
"mailto:[email protected]"HYPERLINK
"mailto:[email protected]"(e-mail address removed)> wrote in message


"D Miller" <HYPERLINK
"mailto:[email protected]"HYPERLINK
"mailto:[email protected]"(e-mail address removed)> wrote in
I would like to scan though the properties of the class and
take
actions
based on the name of the property.


You need to look into Reflection. This is the name that .NET
gives
to
"examining .NET". Check out the PropertyInfo class in the

System.Reflection

namespace, and then take a look at the GetProperties method of
the
Type
class.




--
Outgoing mail is certified Virus Free.
Checked by AVG Anti-Virus (http://www.grisoft.com).
Version: 7.0.230 / Virus Database: 263.3.9 - Release Date: 7/2/2004
 
I figured there was an easy way.

Thank you.
David

Ken Tucker said:
Hi,


Dim c As New MyTestClass
c.A = "a"
c.B = "B"
c.C = 100

Dim t As Type = c.GetType
For Each pi As Reflection.PropertyInfo In t.GetProperties
Debug.WriteLine(String.Format("{0} {1} {2}", pi.Name, _
pi.PropertyType.FullName, pi.GetValue(c, Nothing)))
Next

Ken
---------------------

D Miller said:
Maybe I should better describe what I'm trying to accomplish to. What I

would like to do is scan through the properties of several different
classes
and writing the values to a database depending on the piticular property

that is being read.



"D Miller" <HYPERLINK
"mailto:[email protected]"(e-mail address removed)> wrote in message
Thanks for the help, this solves half the problem of giving me the
properties in the class. Is there some way I can also get the value
of
the

property?

Thanks


"Ken Tucker [MVP]" <HYPERLINK
"mailto:[email protected]"(e-mail address removed)> wrote in message
news:%[email protected]...
Hi,

Test class

Public Class MyTestClass

Dim mstrA As String
Dim mstrB As String
Dim mintC As Integer

Public Property A() As String
Get
Return mstrA
End Get
Set(ByVal Value As String)
mstrA = Value
End Set
End Property

Public Property B() As String
Get
Return mstrB
End Get
Set(ByVal Value As String)
mstrB = Value
End Set
End Property

Public Property C() As Integer
Get
Return mintC
End Get
Set(ByVal Value As Integer)
mintC = Value
End Set
End Property
End Class

To get properties from class

Dim t As Type = GetType(MyTestClass)
For Each pi As Reflection.PropertyInfo In t.GetProperties
Debug.WriteLine(String.Format("{0} {1}", pi.Name,
pi.PropertyType.FullName))
Next

Ken
-----------------------------

"D Miller" <HYPERLINK
"mailto:[email protected]"(e-mail address removed)> wrote in message
news:o[email protected]:
You don't happen to have an example somewhere do you?



"Jeff Johnson [MVP:VB]" <HYPERLINK
"mailto:[email protected]"HYPERLINK
"mailto:[email protected]"(e-mail address removed)> wrote in message
"D Miller" <HYPERLINK
"mailto:[email protected]"HYPERLINK
"mailto:[email protected]"(e-mail address removed)> wrote in


I would like to scan though the properties of the class and
take
actions
based on the name of the property.
You need to look into Reflection. This is the name that .NET
gives
to

"examining .NET". Check out the PropertyInfo class in the


namespace, and then take a look at the GetProperties method of
the
Type
class.
 
Back
Top