Results 1 to 12 of 12

Thread: use a my.setting entry as a parameter of a sub/function or class property

  1. #1

    Thread Starter
    New Member
    Join Date
    Apr 2015
    Posts
    4

    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?

  2. #2
    Frenzied Member dolot's Avatar
    Join Date
    Nov 2007
    Location
    Music city, U.S.A.
    Posts
    1,253

    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

  3. #3

    Thread Starter
    New Member
    Join Date
    Apr 2015
    Posts
    4

    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

  4. #4
    Still learning kebo's Avatar
    Join Date
    Apr 2004
    Location
    Gardnerville,nv
    Posts
    3,757

    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

  5. #5

    Thread Starter
    New Member
    Join Date
    Apr 2015
    Posts
    4

    Re: use a my.setting entry as a parameter of a sub/function or class property

    Thank you Kebo,
    i will check it

  6. #6

    Thread Starter
    New Member
    Join Date
    Apr 2015
    Posts
    4

    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

  7. #7
    Smooth Moperator techgnome's Avatar
    Join Date
    May 2002
    Posts
    34,532

    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
    * I don't respond to private (PM) requests for help. It's not conducive to the general learning of others.*
    * I also don't respond to friend requests. Save a few bits and don't bother. I'll just end up rejecting anyways.*
    * How to get EFFECTIVE help: The Hitchhiker's Guide to Getting Help at VBF - Removing eels from your hovercraft *
    * How to Use Parameters * Create Disconnected ADO Recordset Clones * Set your VB6 ActiveX Compatibility * Get rid of those pesky VB Line Numbers * I swear I saved my data, where'd it run off to??? *

  8. #8
    Still learning kebo's Avatar
    Join Date
    Apr 2004
    Location
    Gardnerville,nv
    Posts
    3,757

    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

  9. #9
    Smooth Moperator techgnome's Avatar
    Join Date
    May 2002
    Posts
    34,532

    Re: use a my.setting entry as a parameter of a sub/function or class property

    Quote Originally Posted by kebo View Post
    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
    * I don't respond to private (PM) requests for help. It's not conducive to the general learning of others.*
    * I also don't respond to friend requests. Save a few bits and don't bother. I'll just end up rejecting anyways.*
    * How to get EFFECTIVE help: The Hitchhiker's Guide to Getting Help at VBF - Removing eels from your hovercraft *
    * How to Use Parameters * Create Disconnected ADO Recordset Clones * Set your VB6 ActiveX Compatibility * Get rid of those pesky VB Line Numbers * I swear I saved my data, where'd it run off to??? *

  10. #10
    Still learning kebo's Avatar
    Join Date
    Apr 2004
    Location
    Gardnerville,nv
    Posts
    3,757

    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

  11. #11
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    110,299

    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:
    1. Public MustInherit Class StringCollectionManager
    2.  
    3.     Protected MustOverride ReadOnly Property Collection As Specialized.StringCollection
    4.  
    5.     Public Sub AddItem(item As String)
    6.         Me.Collection.Add(item)
    7.     End Sub
    8.  
    9. End Class
    10.  
    11.  
    12. Public Class FirstStringCollectionManager
    13.     Inherits StringCollectionManager
    14.  
    15.     Protected Overrides ReadOnly Property Collection As Specialized.StringCollection
    16.         Get
    17.             Return My.Settings.FirstStringCollection
    18.         End Get
    19.     End Property
    20.  
    21. End Class
    22.  
    23.  
    24. Public Class ScondStringCollectionManager
    25.     Inherits StringCollectionManager
    26.  
    27.     Protected Overrides ReadOnly Property Collection As Specialized.StringCollection
    28.         Get
    29.             Return My.Settings.SecondStringCollection
    30.         End Get
    31.     End Property
    32.  
    33. End Class

  12. #12
    Smooth Moperator techgnome's Avatar
    Join Date
    May 2002
    Posts
    34,532

    Re: use a my.setting entry as a parameter of a sub/function or class property

    Quote Originally Posted by kebo View Post
    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
    * I don't respond to private (PM) requests for help. It's not conducive to the general learning of others.*
    * I also don't respond to friend requests. Save a few bits and don't bother. I'll just end up rejecting anyways.*
    * How to get EFFECTIVE help: The Hitchhiker's Guide to Getting Help at VBF - Removing eels from your hovercraft *
    * How to Use Parameters * Create Disconnected ADO Recordset Clones * Set your VB6 ActiveX Compatibility * Get rid of those pesky VB Line Numbers * I swear I saved my data, where'd it run off to??? *

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width