CKeM

So I've looked up Callbyname and it seems to be the function I want but I have a question concerning it.

I have a bunch of string variables, we'll say (for example, in reality there would be more than three):

dim crunch as string = "Crispy food!"

dim bite as string = "Seperating the food!"

dim chew as string = "processing the food!"

And then I have a textbox1 and a button. I want to be able to type in the textbox1 whatever variable I want and for the program to spit out the contained string when I hit the button.

For this example I want to display it in a message box.

Typically you use Callbyname to for the ProcName as a string like this:

MsgBox(CallByName(chew, textbox1.text, CallType.Get))

And the person types in "ToString"

But in my case I'm wanting to use the ObjectRef as the string

MsgBox(CallByName(textbox1.text, "ToString", CallType.Get)) <--doesn't work

Unfortunately it only returns the value I type in instead of the variables string. So if I type in "chew" it returns "chew" instead of "processing the food!" I understand the reason is that it's interpreting it as:

MsgBox(CallByName("chew", "ToString", CallType.Get))

Which isn't what I need.

I should also add that this program is only for my personal use, so I don't have to worry about typing something wrong in (fault protection).

Is there a way to use refer the to the variable object from the textbox with callbyname

thanks

Ck



Re: Visual Basic Express Edition callbyname question

Solitaire

You seem to be very confused and are using the wrong kind of code.

A Select Case block is the way to go. You were trying to use multiple variables when you only need a single variable with multiple values. This is probably what you want to do:

Code Snippet

Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click

Dim mystr As String = TextBox1.Text

MessageBox.Show(CallByName(mystr))

End Sub

Function CallByName(ByVal mystr As String) As String

Select Case mystr

Case "bite"

Return "Separating the food!"

Case "crunch"

Return "Crispy food!"

Case "chew"

Return "Processing the food"

Case Else

Return ""

End Select

End Function






Re: Visual Basic Express Edition callbyname question

Tall Dude

Code Snippet

Public Class Form1

' The following code will return 'Crunch'

' in a messagebox when 'Crispy_Food' is typed

' in textbox1.

Dim _Crispy_Food As String = "Crunch"

Public Property Crispy_Food()

Get

Return _Crispy_Food

End Get

Set(ByVal value)

_Crispy_Food = value

End Set

End Property

Private Sub Button1_Click(ByVal sender As System.Object, _

ByVal e As System.EventArgs) Handles Button1.Click

Try

MsgBox(CallByName(Me, TextBox1.Text.ToString, CallType.Get))

Catch ex As Exception

MsgBox("Property not found")

End Try

End Sub

End Class






Re: Visual Basic Express Edition callbyname question

ssta

I prefer to use a collection (actually I'd rather use a database) for this type of thing. Here's what the code might look like:

Code Snippet

Private _Food As New System.Collections.Specialized.StringDictionary


Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load

_Food.Add("chew", "processing the food")

_Food.Add("blend", "mix the food")

End Sub


Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click

MsgBox(_Food("blend") & Environment.NewLine & _Food("chew"))

End





This technique works great for strings but can work equally well when you are dealing with a more complex object in your collection.


Not as efficient as the code above.





Re: Visual Basic Express Edition callbyname question

CKeM

Those look like good examples. I'll have to run it through the grinder to see if it fits my needs (I've been working through other code all day).

Thanks everyone, I really appreciate it.

CK