-
Apr 27th, 2015, 09:47 AM
#1
Thread Starter
New Member
use a my.setting entry as a parameter of a sub/function or class property
how can i use a my.setting entry as a parameter of a subroutine/function or as a property value of a class.
I have several system.collections.specialized.stringcollection storing different Information
e.g. presets for Listboxes, Information about Customergroups etc.
I want to use one base class what handles the add, verify, edit and remove of those settings.
This base class have not to know the name of the setting.
A new class what inherits the base class has to do the rest of the specialized job for the differnent setting
each setting will get a own new class, something in this way.
the derived classes will call sub's of the base class and will pass the setting name as sub parameter to the base
class sub to do the appropiate job.
I did't find any parameter type with what I can transfer a setting as a parameter or property
the my.mysetting will have a explicite setting name what the base class do not have at coding time
it first will get it at runtime.
Has anyone a Idea what can help me?
-
Apr 27th, 2015, 10:11 AM
#2
Re: use a my.setting entry as a parameter of a sub/function or class property
I'm not exactly sure what you need, but My.Settings has an Item property that takes a string argument as the name of the property to be searched for. Maybe that could be of use to you. You could just pass a string with the name of the property and use My.Settings.Item("{PropertyName}") to access the property you want.
I always add to the reputation of those whose posts are helpful, and even occasionally to those whose posts aren't helpful but who obviously put forth a valiant effort. That is, when the system will allow it.
My war with a browser-redirect trojan
-
Apr 28th, 2015, 05:30 AM
#3
Thread Starter
New Member
Re: use a my.setting entry as a parameter of a sub/function or class property
Thanks dolot,
this I know. What I want to know is how to use the Property.PropertyName as a variable in a e.g. subroutine
My be this pseudocode will give you a idea of what I whant to know.
Public Class BaseClass ' A WindowformsClass
public sub subDoSomethingWithaProperty(byVal _propertyName as ??????)
_propertyName="AnyValue"
end sub
end Class ' Baseclass End
Question: What type has the ???????? to be, that I can handle a setting in coding time with out knowing what settingname
later will be used.
Public class aDerivedClassOfBaseClass 'a WindowsForms Class what is derived from the BaseClass Windowsform
inherits BaseClass
BaseClass.subDoSomethingWithaProperty(my.settings.mySettingName)
end class
Public class anotherDerivedClassOfBaseClass 'a WindowsForms Class what is derived from the BaseClass Windowsform
inherits BaseClass
BaseClass.subDoSomethingWithaProperty(my.settings.myOtherSettingName)
end class
-
Apr 28th, 2015, 06:29 AM
#4
Re: use a my.setting entry as a parameter of a sub/function or class property
It sounds like you want to pass a property name to a routine then use the property. If so, take a look at the following
Code:
Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
Dim pName As String = GetPropertyName(Function() My.Settings.propertyA)
subDoSomethingWithaProperty(pName, "a new value")
End Sub
Public Function GetPropertyName(Of T)(ByVal expression As Expressions.Expression(Of Func(Of T))) As String
Dim memberExpression As Expressions.MemberExpression = DirectCast(expression.Body, Expressions.MemberExpression)
Return memberExpression.Member.Name
End Function
Public Sub subDoSomethingWithaProperty(ByVal _propertyName As String, value As Object)
Dim mySettingsType = GetType(My.MySettings)
Dim pI As Reflection.PropertyInfo = mySettingsType.GetProperty(_propertyName, Reflection.BindingFlags.Public Or Reflection.BindingFlags.Instance Or Reflection.BindingFlags.IgnoreCase)
'set the value
pI.SetValue(My.Settings, Conversion.CTypeDynamic(value, pI.PropertyType))
'read the value
Dim newValue As Object = pI.GetValue(My.Settings)
Dim s As String = String.Format("My.Settings.{0} = {1}", _propertyName, newValue.ToString)
MessageBox.Show(s)
End Sub
Kevin
Process control doesn't give you good quality, it gives you consistent quality.
Good quality comes from consistently doing the right things.
Vague general questions have vague general answers. A $100 donation is required for me to help you if you PM me asking for help. Instructions for donating to one of our local charities will be provided.
______________________________ Last edited by kebo : Now. Reason: superfluous typo's
-
Apr 28th, 2015, 07:31 AM
#5
Thread Starter
New Member
Re: use a my.setting entry as a parameter of a sub/function or class property
Thank you Kebo,
i will check it
-
Apr 30th, 2015, 09:01 AM
#6
Thread Starter
New Member
Re: use a my.setting entry as a parameter of a sub/function or class property
Hi Kebo,
thanks a lott, your example works well in my environment.
This is one or two layers above my knowledge of vb.net. So I had read a lot about this reflecdtion.propertyinfo and expressions.memberexpression stuff
it was so frustrating to learn what I dont know and the other side so exiting to find out that there is still so much to learn
-
Apr 30th, 2015, 09:53 AM
#7
Re: use a my.setting entry as a parameter of a sub/function or class property
Sounds like over engineering to me... if you know the name of the setting you want, why not just pass it?
-tg
-
Apr 30th, 2015, 10:35 AM
#8
Re: use a my.setting entry as a parameter of a sub/function or class property
The best part about over engineering is when you realized that you have, then get that good ole gooey feeling inside knowing you have done something soooo complex in a super simple way. Its kind of like a self inflicted munchausen syndrome by proxy
Process control doesn't give you good quality, it gives you consistent quality.
Good quality comes from consistently doing the right things.
Vague general questions have vague general answers. A $100 donation is required for me to help you if you PM me asking for help. Instructions for donating to one of our local charities will be provided.
______________________________ Last edited by kebo : Now. Reason: superfluous typo's
-
Apr 30th, 2015, 10:59 AM
#9
Re: use a my.setting entry as a parameter of a sub/function or class property
Originally Posted by kebo
The best part about over engineering is when you realized that you have, then get that good ole gooey feeling inside knowing you have done something soooo complex in a super simple way. Its kind of like a self inflicted munchausen syndrome by proxy
Over engineering is the opposite -- when you've taken something which should be simple and over engineer it into something complex.
How is this better (and granted this maybe a matter of opinion)
Code:
DoSomeThing(GetSomeProp("UserName"))
When this will suffice:
Code:
DoSomeThing(My.Settings.UserName)
That's what I don't get in these cases... it's adding complexity where it's not needed.
I see this more often with database methods... people try to make all these generic subs/functions which work for super simple things, but the second you start a join or do something complex, their simple methods fall apart.
Now, i'm all for trying things out "just to see" -- i do that too... but most of teh time I wouldn't then use it in production.
-tg
-
Apr 30th, 2015, 07:45 PM
#10
Re: use a my.setting entry as a parameter of a sub/function or class property
something which should be simple and over engineer it into something complex.
Much like my over engineered post, that was missing only a couple of words and resulted in a meaning complete opposite of my intent. I'm good at over engineering. I like engineering so much I just can't get enough sometimes.
Anyway, yea I agree in most cases accessing a property by getting its name first is a bit like i = Cint(1.Tostring). However accessing property like that does indeed have some very useful and powerful uses.
/me goes back to building virtual Rube Goldberg machines.
Process control doesn't give you good quality, it gives you consistent quality.
Good quality comes from consistently doing the right things.
Vague general questions have vague general answers. A $100 donation is required for me to help you if you PM me asking for help. Instructions for donating to one of our local charities will be provided.
______________________________ Last edited by kebo : Now. Reason: superfluous typo's
-
Apr 30th, 2015, 08:16 PM
#11
Re: use a my.setting entry as a parameter of a sub/function or class property
If the point here is to create a base class that contains functionality to manipulate a StringCollection and then one derived class per StringCollection in My.Settings, you could go this way:
vb.net Code:
Public MustInherit Class StringCollectionManager Protected MustOverride ReadOnly Property Collection As Specialized.StringCollection Public Sub AddItem(item As String) Me.Collection.Add(item) End Sub End Class Public Class FirstStringCollectionManager Inherits StringCollectionManager Protected Overrides ReadOnly Property Collection As Specialized.StringCollection Get Return My.Settings.FirstStringCollection End Get End Property End Class Public Class ScondStringCollectionManager Inherits StringCollectionManager Protected Overrides ReadOnly Property Collection As Specialized.StringCollection Get Return My.Settings.SecondStringCollection End Get End Property End Class
Last edited by jmcilhinney; Apr 30th, 2015 at 08:22 PM.
-
Apr 30th, 2015, 08:24 PM
#12
Re: use a my.setting entry as a parameter of a sub/function or class property
Originally Posted by kebo
However accessing property like that does indeed have some very useful and powerful uses.
True, but i usually find that those are special and specific times... this case doesn't sound like it, but eh, what do I know? I'm just a tired old curmudgeon.
-tg
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|