By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
457,887 Members | 1,214 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 457,887 IT Pros & Developers. It's quick & easy.

Dynamic mapping string to function

P: n/a
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
Share this Question
Share on Google+
4 Replies


P: n/a
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

P: n/a
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

P: n/a
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

P: n/a
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.