468,734 Members | 2,365 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 468,734 developers. It's quick & easy.

Dynamic mapping string to function

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
Oct 6 '06 #1
4 998
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
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

Oct 6 '06 #2
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.comwrote:
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
Oct 6 '06 #3
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
Oct 9 '06 #4
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.comwrote:
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
Oct 10 '06 #5

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

4 posts views Thread by Scott Lyons | last post: by
reply views Thread by Bill | last post: by
8 posts views Thread by tonFrere | last post: by
1 post views Thread by CARIGAR | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.