Home All Groups Group Topic Archive Search About

Dynamic mapping string to function

Author
6 Oct 2006 7:54 AM
Vemund Halvorsen
Hello,

What is the best way of mapping a string to function/sub? See ex bellow -
probably not the best way - especially when it comes to maintenance ;)

Does anybody have a better way of doing this?


Ex,

class A
  Private mA as Integer 

  Public Sub SetFunc(byval func as string, byval value as integer)
    Select Case value.ToUpper
      Case "funcA".ToUpper
        'do funcA
        mA = value
      Case "funcB".ToUpper
        'do funcB
    end select
  end Sub

  Public Function GetA() as Integer
    return mA
  end function
end class

class Test
  Dim myA as new A
  myA.SetFunc("funcA", 42)

  Dim tmp as integer
  tmp = myA.GetA()

end class

Author
6 Oct 2006 6:37 PM
GhostInAK
Hello Vemund,

This question is asked repeatedly in the NG.  Each time I am forced to question
the sanity of the questioner.  In 15 years of programming I've only ever
run across 2 problems that required this kind of solution.  So in answer
to your question I ask: What are you trying to do?

-Boo

Show quoteHide quote
> Hello,
>
> What is the best way of mapping a string to function/sub? See ex
> bellow - probably not the best way - especially when it comes to
> maintenance ;)
>
> Does anybody have a better way of doing this?
>
> Ex,
>
> class A
> Private mA as Integer
> Public Sub SetFunc(byval func as string, byval value as integer)
> Select Case value.ToUpper
> Case "funcA".ToUpper
> 'do funcA
> mA = value
> Case "funcB".ToUpper
> 'do funcB
> end select
> end Sub
> Public Function GetA() as Integer
> return mA
> end function
> end class
> class Test
> Dim myA as new A
> myA.SetFunc("funcA", 42)
> Dim tmp as integer
> tmp = myA.GetA()
> end class
>
Author
6 Oct 2006 10:15 PM
Brian Gideon
Vemund,

You could use a Hashtable or Dictionary to store string-delegate pairs.
But, like Boo, I also question the reasoning behind doing something
like that.

Brian

On Oct 6, 2:54 am, Vemund Halvorsen <j***@mail.com> wrote:
Show quoteHide quote
> Hello,
>
> What is the best way of mapping a string to function/sub? See ex bellow -
> probably not the best way - especially when it comes to maintenance ;)
>
> Does anybody have a better way of doing this?
>
> Ex,
>
> class A
>   Private mA as Integer 
>
>   Public Sub SetFunc(byval func as string, byval value as integer)
>     Select Case value.ToUpper
>       Case "funcA".ToUpper
>         'do funcA
>         mA = value
>       Case "funcB".ToUpper
>         'do funcB
>     end select
>   end Sub
>
>   Public Function GetA() as Integer
>     return mA
>   end function
> end class
>
> class Test
>   Dim myA as new A
>   myA.SetFunc("funcA", 42)
>
>   Dim tmp as integer
>   tmp = myA.GetA()
>
> end class
Author
9 Oct 2006 1:37 PM
Vemund Halvorsen
On 6 Oct 2006 15:15:33 -0700, Brian Gideon wrote:

> Vemund,
>
> You could use a Hashtable or Dictionary to store string-delegate pairs.
>  But, like Boo, I also question the reasoning behind doing something
> like that.
>
> Brian
>

Im trying to set configuration properties using a ini file as storage. I
know that XML is a better (to store config settings), but due to historical
reasons ini is the storage used.

So what is the best way of applying default values to (and maybe from) an
object to an ini file?

/Vemund
Author
10 Oct 2006 10:40 PM
Brian Gideon
Vermund,

I would store them in a Hashtable or Dictionary as string-value pairs.
The property name would be the key into the collection.  Just read the
file line-by-line and starting adding the properties to the collection.
Similarly, enumerator the collection and write the contents back to a
file line-by-line.

Brian

On Oct 9, 8:37 am, Vemund Halvorsen <j***@mail.com> wrote:
Show quoteHide quote
> Brian Im trying to set configuration properties using a ini file as storage. I
> know that XML is a better (to store config settings), but due to historical
> reasons ini is the storage used.
>
> So what is the best way of applying default values to (and maybe from) an
> object to an ini file?
>
> /Vemund