I’m working in VB.net and I want to create a generic ToString.
is there a way to do a “For Each” in “Me”.
Sample
Public Property Var1() As String {... get ... set ...}
Public Property Var2() As String {... get ... set ...}
Public Property Var2() As String {... get ... set ...}
Public Overrides Function ToString() As String
dim str as stringbuilder
for each item in Me
str.append(item.tostring())
next
return str
End Function
The reason of my question its because some of my Variables can be null (nothing).
So with a generic like this, I can do
if item isnot nothing then
str.append(item.tostring())
end if
maybe theres a better way???
tank you
This can be achieved using reflection with
GetFields. Here you go:If you want to display properties use
GetProperties