|
-
Jul 24th, 2009, 01:58 PM
#1
Thread Starter
Addicted Member
Using global variables
Hi;
I make some calculation with ocx but can't show calculation result (array) in the normal form. I tried to use public variable declaration but it doesn't work.
I also tried to save results to the global variables which i declare in normal projects.
Thank you
-
Jul 24th, 2009, 02:07 PM
#2
Re: Using global variables
How are you declaring the variable? And how are you reading the values from it?
-
Jul 24th, 2009, 02:15 PM
#3
Thread Starter
Addicted Member
Re: Using global variables
Thanks for your interest,
In Ocx
Code:
Dim MyArr(4) as double
Public Sub Calculate(x as Double, y as Double)
for i=1 to 5
MyArr(i)=x+y
Arr(i)=MyArr(i) 'Global array
next i
End Sub
In Project
InModule
Code:
Public Arr() as double
In Form
Code:
Private Sub Command1_Click()
redim Arr(5)
Call MyOcx.Calculate(3,4)
msgbox Arr(3)
End Sub
-
Jul 24th, 2009, 02:20 PM
#4
Re: Using global variables
 Originally Posted by levent
In Form
Code:
Private Sub Command1_Click()
redim Arr(5)
Call MyOcx.Calculate(3,4)
msgbox Arr(3)
End Sub
If you do a REDIM only, you lose whatever is there currently in the array.
Instead try REDIM PRESERVE
Code:
Redim Preserve Arr(5) As Double
-
Jul 24th, 2009, 02:45 PM
#5
Thread Starter
Addicted Member
Re: Using global variables
Maybe we lost all data in Arr() array after REDIM but we calculate again
Code:
redim Arr(5)
Call MyOcx.Calculate(3,4)
Meanwhile your offer doesn't work
Thank you
-
Jul 24th, 2009, 03:37 PM
#6
Re: Using global variables
The OCX is not aware of the variables in the other project, so the Arr variable it is using is actually an entirely different variable.
If you haven't declared Arr in the OCX, you should be getting an error (but if not, see the article What is Option Explicit, and why should I use it? from our Classic VB FAQs (in the FAQ forum)).
There are various ways you could pass the values back, but as they are the results of the Calculate routine the method that makes most sense is for the Calculate routine to return them - by converting it to a function, eg:
Code:
Public Function Calculate(x as Double, y as Double) As Double()
Dim MyArr(5) as double
Dim i as Integer
for i=1 to 5
MyArr(i)=x+y
next i
Calculate = MyArr 'return the value to the caller
End Sub
Unfortunately as the return value is an array, you cannot assign it directly to a variable declared as an array, so this is the closest you can do:
Code:
Private Sub Command1_Click()
Dim Arr as Variant
Arr = MyOcx.Calculate(3,4)
msgbox Arr(3)
End Sub
An alternative method (which would allow you to declare Arr as an array of the correct data type) would be to keep the routine as a Sub, and pass Arr as a ByRef parameter.
-
Jul 25th, 2009, 10:42 AM
#7
Thread Starter
Addicted Member
Re: Using global variables
Thanks for your explanation, si_the_geek.
Could you offer different method to make results public. Because i make some matrix operation therefore arrays has big dimesions.
-
Jul 25th, 2009, 02:41 PM
#8
Re: Using global variables
The results shouldn't be Public, as that allows problems to creep in (such as accidental re-use of the variable which can have conflicts with this usage). They should, in one way or another, be returned by the routine.
In most cases you should use the return value of the function (like VB's built in functions do), but with a large array you may notice a speed loss and/or memory increase due to the use of variants. As such, using a ByRef parameter (which is actually the default) as I described would probably be the best option:
Code:
Public Sub Calculate(x as Double, y as Double, ByRef Arr() as Double)
Dim i as Integer
for i=1 to 5
Arr(i)=x+y
next i
End Sub
Code:
Private Sub Command1_Click()
Dim Arr(5) As Double
MyOcx.Calculate 3, 4, Arr
msgbox Arr(3)
End Sub
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
|