Dynamic mapping string to function

  • Thread starter Vemund Halvorsen
  • Start date
V

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
 
G

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
 
B

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
 
V

Vemund Halvorsen

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
 
B

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
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top