473,486 Members | 2,424 Online
Bytes | Software Development & Data Engineering Community
Create Post

Home Posts Topics Members FAQ

Setting properties in unknown classes

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

Nov 20 '05 #1
12 1386
Michael

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

Try: theClass.GetType.GetProperty(pi.Name).SetValue(the Class, 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

Nov 20 '05 #2
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" <an*******@discussions.microsoft.com> wrote in message
news:6B**********************************@microsof t.com...
Michael,

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

Try: theClass.GetType.GetProperty(pi.Name).SetValue(the Class, 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

Nov 20 '05 #3
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
Nov 20 '05 #4
// 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" <an*******@discussions.microsoft.com> wrote in message
news:F0**********************************@microsof t.com...
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 PropertyInfo
Dim blnFound As Boolean = False
For Each p In pp
If p.Name.ToLower = "PropertyName" Then
blnFound = True
Exit For
End If
Next
If blnFound Then
ot.GetType.GetProperty(p.Name).SetValue(ot, objNewValue, Nothing)
End If

Nov 20 '05 #5
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" <an*******@discussions.microsoft.com> wrote in message
news:F0**********************************@microsof t.com...
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 PropertyInfo
Dim blnFound As Boolean = False
For Each p In pp
If p.Name.ToLower = "PropertyName" Then
blnFound = True
Exit For
End If
Next
If blnFound Then
ot.GetType.GetProperty(p.Name).SetValue(ot, objNewValue, Nothing)
End If

Nov 20 '05 #6
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" <an*******@discussions.microsoft.com> wrote in message
news:F0**********************************@microsof t.com...
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 PropertyInfo
Dim blnFound As Boolean = False
For Each p In pp
If p.Name.ToLower = "PropertyName" Then
blnFound = True
Exit For
End If
Next
If blnFound Then
ot.GetType.GetProperty(p.Name).SetValue(ot, objNewValue, Nothing)
End If

Nov 20 '05 #7
* "Michael Maes" <mi*****@merlot.com> scripsit:
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'.

--
Herfried K. Wagner [MVP]
<URL:http://dotnet.mvps.org/>
Nov 20 '05 #8
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
"Herfried K. Wagner [MVP]" <hi***************@gmx.at> wrote in message
news:c6************@ID-208219.news.uni-berlin.de...
* "Michael Maes" <mi*****@merlot.com> scripsit:
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'.

--
Herfried K. Wagner [MVP]
<URL:http://dotnet.mvps.org/>

Nov 20 '05 #9
Michael!

* "Michael Maes" <mi*****@merlot.com> scripsit:
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.


;-)

--
Herfried K. Wagner [MVP]
<URL:http://dotnet.mvps.org/>
Nov 20 '05 #10
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

"Herfried K. Wagner [MVP]" <hi***************@gmx.at> wrote in message
news:c6************@ID-208219.news.uni-berlin.de...
Michael!

* "Michael Maes" <mi*****@merlot.com> scripsit:
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.


;-)

--
Herfried K. Wagner [MVP]
<URL:http://dotnet.mvps.org/>

Nov 20 '05 #11
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" <an*******@discussions.microsoft.com> wrote in messag
news:F0**********************************@microsof t.com..
Michael
Try the following stepping through the code to see if you are getting th
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

Nov 20 '05 #12
Yeah, me too.
Thanks Chris,

Michael

"Chris Podmore" <an*******@discussions.microsoft.com> wrote in message
news:A0**********************************@microsof t.com...
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
"Chris Podmore" <an*******@discussions.microsoft.com> wrote in message news:F0**********************************@microsof t.com...
> 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 PropertyInfo
> Dim blnFound As Boolean = False
> For Each p In pp
> If p.Name.ToLower = "PropertyName" Then
> blnFound = True
> Exit For
> End If
> Next
> If blnFound Then
> ot.GetType.GetProperty(p.Name).SetValue(ot, objNewValue, Nothing) > End If
>>

Nov 20 '05 #13

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

6
6953
by: Peter Krikelis | last post by:
Hi All, I am having a problem setting up input mode for serial communications. (Sorry about the long code post). The following code is what I use to set up my comm port.
1
2639
by: Danko Greiner | last post by:
Thanx Willy, this was very helpful. But i also need (and want to know) how to do this from code. Can you plase give me right topic in MSDN? is there good example? Thanx p.s. this is...
2
1617
by: Don | last post by:
I'm asking this for a friend of mine, so forgive me if I'm getting some of the terminology wrong (I don't have any experience with ASP.NET). I've got an ASP application that has some classes that...
1
2404
by: Jeff Smith | last post by:
Can I load custom web user controls dynamically and access the properties and methods without having to explicitly define custom control types (example 2 below). I have custom web control named...
1
1972
by: Frank Yamrick | last post by:
I am trying to program an application that requires a large number of screnes that are very similar in the respect that all the bottons and labels interact with each other with the same...
8
11240
by: Jeff | last post by:
ASP.NET 2.0 I'm wondering how to set the color of a visited HyperLinkField (the link text) in a GridView?? Here is the markup of the HyperLinkField I have problems with: <asp:HyperLinkField...
41
2822
by: Jim | last post by:
Hi guys, I have an object which represents an "item" in a CMS "component" where an "item" in the most basic form just a field, and a "component" is effectively a table. "item" objects can be...
8
1437
by: Andrzej Lipski | last post by:
I am new to dotnet and I'll tried searching Google for a solution to my problem. I am hoping that it is possible to do, or am I going down a dead end? I have a User class that has known...
3
2108
by: Jeremy | last post by:
I've created a serializable class and put attributes around all the properties that should be serialized. I return the class from a web service, but my problem is that the wsdl for the web service...
0
7100
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
6964
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
7175
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
7330
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
5434
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
3070
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The...
0
3070
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
598
muto222
php
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
0
262
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.