can someone give me an example of using the callbyname function? plus give me a little information on what it does exactly? thanks from ahead!
Printable View
can someone give me an example of using the callbyname function? plus give me a little information on what it does exactly? thanks from ahead!
It calls methods, or sets/retrieves property values by the name of the method/property. Here's an example...
VB Code:
MsgBox CallByName(Text1, "Text", VbGet)
It will return the value of the Text property. To set it...
VB Code:
Call CallByName(Text1,"Text",vbLet, "Some Text")
:)
seec77 ,
Here is some more information. I have never used this but here's some info out of one of my reference books.
The CallByName function allows you to use a string to specify a property or metod at run time.
My intellsense says:
CallByName(Object As Object, ProcName As String, CallType As VBCall Type, Args() As Variant)
The Programmer's Guide says:
Result = CallByName(Object, ProcedureName, CallType, Arguments())
The first argument to CallByName takes the name of the object that you want to act upon. The second argument ProcedureName, takes a string containing the name and method or property procedure to be used. The CallType takes a constant representing the type of procedure to invoke: a method (vbMethod), a property let (vbLet), a property get (vbGet), or a property set (vbSet). The final argument is optional, it takes a variant array containing any arguments to the procedure.
Suppose this was a server app, called MathServer, that you wished to add a new SquareRoot funcion to. Your app has two textbox controls: Text1 has the expression to be evaluated; Text2 is where you enter the name of the function. The following code should then work in the click event of a command button to invoke the SquareRoot function on the expresion in Text1:
Private Sub Command1_Click()
Text1.Text = CallByName(MathServer, Text2.Text, vbmetod, Text1.Text)
End Sub
If you enter "64/4" in Text1 and "SquareRoot" in Text2, which should return "4 " in Text1. If you enter an invalid string in Text2, or if the string contains the name of a Property instead of a method, or if the method has an additional required argument, you will get a run-time error. So you will need to add robust error handling to anticipate these or any other errors that might occur.
You will need to wiegh its usefulness against performance problems. CallByName is a little slower than late-bound calls and if you call it repeatedly, say from inside a loop it could have a severe effect on your apps performance.
Whew.... hope this helps.
great job! but one more thing is on my mind... say i want to do a remote control prog, and i want to send another computer a command to do...
say i am the remote comp trying to send another comp a function telling it to do a msgbox saying "hello"...
so something like this:
this is the computer receiving the data:VB Code:
Private Sub Form_Load() winsock1.senddata("msgbox("+chr(34)+"hello"+chr(34)+")") 'what it does here is send the other computer the string msgbox("hello") through a winsock! End Sub
VB Code:
Private Sub Winsock1_Receivedata(bytesLong as integer) Dim Temp as String winsock1.getdata Temp 'here i want it to execute the temp string he got from the other remote comp End Sub
You are trying to do a compile time operation at run time. Won't work. You'll have to write your own parser to decode the sent string and your own logic to interpret (execute) it. The compiler (which is what VB uses to parse such stuff) is LONG GONE by the time the string is received and so is not available to parse it and turn it into executable code.
Solution...
Send something like:
ON the sender program put something like this
Private Sub Form_Load()
Winsock1.Senddata "<MSG>Hello people"
End Sub
And on the receiver put somthing like this...
Private Sub Winsock1_Receivedata(bytesLong as integer)
Dim Temp as String
Winsock1.Getdata Temp
If Left(Temp,5) = "<MSG>" Then
Msgbox Mid$(Temp, 6)
End If
End Sub
Aight...let me explain...
What you're doing is sending a string with the tag <MSG>followed by what you want it to send. When the receiver picks it up it will get the whole string including the tag. So what you do is separate the tag from the rest of the string to be displayed (also know as parsing a string)
That's what the Left(Temp,5) function does. It takes the first 5 characters in the variable Temp and if they are equal to <MSG> then it know that anything after the tag is displayed in a message.
So it then parses the string with the Mid$(Temp,6) and what this does is returns the rest of the string starting from the 6th character (the next character after the tag)...
Is that simple enough for you? Aight...good luck then
Protocol, do you know that you can surround your code with vbcode tags so that you don't have to manually color keywords?
So this: [vbcode]Private Sub Form_Load()[/Highlight]
Will become:
VB Code:
Private Sub Form_Load()
Just thought I'd let you know. :)
lol...
THAT I did not know...
I'm so accustomed to the predefined bbcode scripts, that I didn't think of searching for that one...
Tanx Hobo
Preciate it.
i don't really understand in all that stuff... do you know if there is a parser that can execute strings at run-time?? and by the way thanks protocol but i allready thought of that...!Quote:
Originally posted by phinds
You are trying to do a compile time operation at run time. Won't work. You'll have to write your own parser to decode the sent string and your own logic to interpret (execute) it. The compiler (which is what VB uses to parse such stuff) is LONG GONE by the time the string is received and so is not available to parse it and turn it into executable code.
Use the Script Control. Take a look at my last posts here.Quote:
Originally posted by seec77
i don't really understand in all that stuff... do you know if there is a parser that can execute strings at run-time?? and by the way thanks protocol but i allready thought of that...!