Setting properties in unknown classes

M

Michael Maes

Hi,

What would be the best way to Set a property of a class which you don't know at design-time which that would be?
The classes haven't got the same base-class, but they're all "extended" with the same properties, methods, ....
In fact it are classes inheriting from Typed DataSets.
I need to set "MyProperty" on tdsExtended1, tdsExtended2, tdsExtended3, .... while itterating the "DataSetCollection"

Of course the best approach would be to have a BaseClass "MyTypedDataSet" and then implement the TypedDataSets Generated by VisualStudio in it, but I don't think that's possible.

I think I have to use something like:
Dim pi As PropertyInfo = t.GetProperty([PropertyName])

If not pi Is Nothing Then

pi.SetValue(Nothing, True, Nothing)

End If


But I can't get it work. (I guess it's the first parameter)

TIA,


Michael
 
G

Guest

Michael

I think the problem lies in the SetValue, is the GetProperty working

Try: theClass.GetType.GetProperty(pi.Name).SetValue(theClass, objNewValue, Nothing

Hope it helps
Chris

----- Michael Maes wrote: ----

Hi

What would be the best way to Set a property of a class which you don't know at design-time which that would be
The classes haven't got the same base-class, but they're all "extended" with the same properties, methods, ...
In fact it are classes inheriting from Typed DataSets
I need to set "MyProperty" on tdsExtended1, tdsExtended2, tdsExtended3, .... while itterating the "DataSetCollection

Of course the best approach would be to have a BaseClass "MyTypedDataSet" and then implement the TypedDataSets Generated by VisualStudio in it, but I don't think that's possible

I think I have to use something like
Dim pi As PropertyInfo = t.GetProperty([PropertyName]

If not pi Is Nothing The

pi.SetValue(Nothing, True, Nothing

End I


But I can't get it work. (I guess it's the first parameter

TIA


Michae
 
M

Michael Maes

Hi Chris,

No, the GetProperty isn't working eigther.
Your tip doesn't work neighter.

the sub is part of an event in the ComponentChangeService-Class

I beleive the issue is getting the class.

here's the code:
Private Sub OnComponentAdded(ByVal sender As Object, ByVal ce As
ComponentEventArgs)

Try

Dim ot As Object = ce.Component

Dim t As Type = ot.GetType

Dim pi As PropertyInfo

pi = t.GetProperty("PropertyName")

pi.SetValue(t, False, Nothing)

' Extra code .....



Catch ex As Exception

Finally

MsgBox(res)

End Try

End Sub



Chris Podmore said:
Michael,

I think the problem lies in the SetValue, is the GetProperty working?

Try: theClass.GetType.GetProperty(pi.Name).SetValue(theClass, objNewValue, Nothing)

Hope it helps.
Chris.

----- Michael Maes wrote: -----

Hi,

What would be the best way to Set a property of a class which you
don't know at design-time which that would be?
The classes haven't got the same base-class, but they're all
"extended" with the same properties, methods, ....
In fact it are classes inheriting from Typed DataSets.
I need to set "MyProperty" on tdsExtended1, tdsExtended2,
tdsExtended3, .... while itterating the "DataSetCollection"
Of course the best approach would be to have a BaseClass
"MyTypedDataSet" and then implement the TypedDataSets Generated by
VisualStudio in it, but I don't think that's possible.
I think I have to use something like:
Dim pi As PropertyInfo = t.GetProperty([PropertyName])

If not pi Is Nothing Then

pi.SetValue(Nothing, True, Nothing)

End If


But I can't get it work. (I guess it's the first parameter)

TIA,


Michael
 
G

Guest

Michael

Try the following stepping through the code to see if you are getting the correct results. Is ot being set correctly to start with
Let me know how you get on

Chris

Dim ot As Object = ce.Component '<=== Does ot equal the class you expect
Dim pp() As PropertyInfo = ot.GetType.GetProperties(
Dim p As PropertyInf
Dim blnFound As Boolean = Fals
For Each p In p
If p.Name.ToLower = "PropertyName" The
blnFound = Tru
Exit Fo
End I
Nex
If blnFound The
ot.GetType.GetProperty(p.Name).SetValue(ot, objNewValue, Nothing
End I
 
M

Michael Maes

// I don't know if this might be important: I use this in design-time (it's
a kind of ComponentManager) //

This is what I've done (have to use msgbox because debugger doesn't work in
design-mode)
The class looks fine to me.
Dim ot As Object = ce.Component '<=== Does ot equal the class you expect?

Dim pp() As PropertyInfo = ot.GetType.GetProperties()

Dim p As PropertyInfo

Dim blnFound As Boolean = False

Dim ppp As String = ""

For Each p In pp

ppp += p.Name.ToString + vbCrLf

If p.Name.ToLower = "text" Then

blnFound = True

Exit For

End If

Next

MsgBox(ppp)

If blnFound Then

MsgBox("Changing property-value")

ot.GetType.GetProperty(p.Name).SetValue(ot, "TEST", Nothing)

Else

MsgBox("Property-value not found")

End If

Exit Sub

If I add a button (or a textbox, ...) to the form during design-time the
first msgbox show the Properties. The second says "Changing property-value".

But the value remains "Button1, TextBox1, ..."



I've commented out all other events "just in case"....

Michael



Chris Podmore said:
Michael,

Try the following stepping through the code to see if you are getting the
correct results. Is ot being set correctly to start with?
 
M

Michael Maes

OK, I've put the same sub in the OnRename-Event.
When I then rename a control, it does change the property.

So I guess that the ComponentChangeService OnAdded-Event gets fired just
before the Component is accessible.
I could put the code in the renaming-event and "rename" the control when
added. But then it happens also everytime a component gets renamed in the
designer. Maybe I could add a flag in the Add-Event.

Gona do some tests......



Chris Podmore said:
Michael,

Try the following stepping through the code to see if you are getting the
correct results. Is ot being set correctly to start with?
 
M

Michael Maes

You have to stay focussed here:

I did the testing with the "Text-Property" for ease (you see it change
right-away).
Just didn't think about VS setting the Text to the Designer-Name when adding
a control.
Dim ot As Object = ce.Component

Dim p As PropertyInfo

p = ot.GetType.GetProperty("Tag")

p.SetValue(ot, ce.Component.Site.Name + "_Added", Nothing)


I've got it working now a wanted (I guess after these quick tests)
Thanks for your help Chris,

Kind regards,

Michael


Chris Podmore said:
Michael,

Try the following stepping through the code to see if you are getting the
correct results. Is ot being set correctly to start with?
 
H

Herfried K. Wagner [MVP]

* "Michael Maes said:
What would be the best way to Set a property of a class which you don't know at design-time which that would be?

Turn 'Option Strict' off, then simply type 'foo.TheProperty = 22'.
'foo' can be dimmed as 'Object'.
 
M

Michael Maes

Hello Herfried,

That could be an option, but I don't (like to) use it because Coding "Option
Strict Off" is verry prone to Runtime-Errors.
I prefer to work arround these problems, however difficult that may be.

Regards,

Michael


know at design-time which that would be?
 
H

Herfried K. Wagner [MVP]

Michael!

* "Michael Maes said:
That could be an option, but I don't (like to) use it because Coding "Option
Strict Off" is verry prone to Runtime-Errors.

Mhm... I turn it off only when I am sure that there are no runtime
errors and turning it off prevents me from writing a lot of "unreadable"
code.
I prefer to work arround these problems, however difficult that may be.

;-)
 
M

Michael Maes

I didn't think you ever had runtime-errors ;-)

No seriously, ..., you're a far better programmer then I am. I'm also (a
bit) dislestic, so I like to use VS-aids like Option Strict, Intellisense,
... (makes me feel 'more secure')

But you're also right: Coding arround "Option Strict On" is sometimes quite
"Jurasic".

All the best,

Michael
 
G

Guest

Michael

Glad you have something working

Chris

----- Michael Maes wrote: ----

You have to stay focussed here

I did the testing with the "Text-Property" for ease (you see it chang
right-away)
Just didn't think about VS setting the Text to the Designer-Name when addin
a control
Dim ot As Object = ce.Componen

Dim p As PropertyInf

p = ot.GetType.GetProperty("Tag"

p.SetValue(ot, ce.Component.Site.Name + "_Added", Nothing


I've got it working now a wanted (I guess after these quick tests
Thanks for your help Chris

Kind regards

Michae


Chris Podmore said:
correct results. Is ot being set correctly to start with
 
M

Michael Maes

Yeah, me too.
Thanks Chris,

Michael

Chris Podmore said:
Michael,

Glad you have something working.

Chris.

----- Michael Maes wrote: -----

You have to stay focussed here:

I did the testing with the "Text-Property" for ease (you see it change
right-away).
Just didn't think about VS setting the Text to the Designer-Name when adding
a control.
Dim ot As Object = ce.Component

Dim p As PropertyInfo

p = ot.GetType.GetProperty("Tag")

p.SetValue(ot, ce.Component.Site.Name + "_Added", Nothing)


I've got it working now a wanted (I guess after these quick tests)
Thanks for your help Chris,

Kind regards,

Michael


getting the
correct results. Is ot being set correctly to start with?
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top