|
-
Mar 4th, 2006, 12:42 PM
#1
Thread Starter
Frenzied Member
[RESOLVED] Pass Object to Function
Hi, I'm trying to pass an object to a function so that I can use the same function multiple times.
For example, this is what I have now:
VB Code:
'In Form
Call funMain(grid1)
'In Module
Function funMain(optional obj)
frmMain.grid1.TextMatrix(5,5) = "Hello World"
End Function
I would like to change the module code to:
VB Code:
frmMain.obj.TextMatrix(5,5) = "Hello World"
and have it update whichever grid is passed to it. Is this possible in VB?
thanks,
Dimava
NXSupport - Your one-stop source for computer help
-
Mar 4th, 2006, 12:52 PM
#2
Re: Pass Object to Function
You can do it whis way. The parameter shouldn't be optional, you have to pass it always. Don't use the Form name in funMain, it will use the object, just that. Also make that function Public and add it to a module. Also, if you're not going to return a value from that function, it should be a sub, not a function.
VB Code:
Call funMain(grid1)
'In Module
Public Sub mySub(obj As Object)
obj.TextMatrix(5,5) = "Hello World"
End Function
It would be even better if you pass more parameters, like row, col and data:
VB Code:
Public Sub mySub(obj As Object, pRow as long, pCol as Long, pText as String)
obj.TextMatrix(pRow,pCol) = pText
End Function
As you're using TextMatrix property, this will work if you pass a MSFlexgrid or MSHFlexgrid as the object, other controls offcourse will fire an error.
Last edited by jcis; Mar 4th, 2006 at 12:56 PM.
-
Mar 4th, 2006, 12:56 PM
#3
Thread Starter
Frenzied Member
Re: Pass Object to Function
Thanks, that definately works the way I want it to. The parameter was only optional because I had the whole function written and I was playing around with the abstraction. Also good point with making it a sub instead of a function.
Edit: I'm actually passing 2 grids to the function with the column names, I just used this as a small example, the full function has over 200 lines of code.
thanks,
Dimava
NXSupport - Your one-stop source for computer help
-
Mar 4th, 2006, 12:56 PM
#4
Re: Pass Object to Function
It would be more efficient to do
Public sub mySub(obj As MSFlexgrid)
-
Mar 4th, 2006, 01:00 PM
#5
Re: Pass Object to Function
 Originally Posted by MartinLiss
It would be more efficient to do
Public sub mySub(obj As MSFlexgrid)
So true, this way he can keep intellisense inside that sub, when using the MSFlexgrid, this is not possible when the control is declared As Object.
Maybe the only reason for use Object, would be to pass different sort of controls, but this is not the case.
-
Mar 4th, 2006, 01:06 PM
#6
Thread Starter
Frenzied Member
Re: [RESOLVED] Pass Object to Function
Thanks, I'll use it as an MSFlexgrid, it definately makes sense
NXSupport - Your one-stop source for computer help
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|