Results 1 to 8 of 8

Thread: Using global variables

  1. #1

    Thread Starter
    Addicted Member
    Join Date
    Aug 2005
    Posts
    222

    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

  2. #2
    VB Addict Pradeep1210's Avatar
    Join Date
    Apr 2004
    Location
    Inside the CPU...
    Posts
    6,614

    Re: Using global variables

    How are you declaring the variable? And how are you reading the values from it?
    Pradeep, Microsoft MVP (Visual Basic)
    Please appreciate posts that have helped you by clicking icon on the left of the post.
    "A problem well stated is a problem half solved." — Charles F. Kettering

    Read articles on My Blog101 LINQ SamplesJSON ValidatorXML Schema Validator"How Do I" videos on MSDNVB.NET and C# ComparisonGood Coding PracticesVBForums Reputation SaverString EnumSuper Simple Tetris Game


    (2010-2013)
    NB: I do not answer coding questions via PM. If you want my help, then make a post and PM me it's link. If I can help, trust me I will...

  3. #3

    Thread Starter
    Addicted Member
    Join Date
    Aug 2005
    Posts
    222

    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

  4. #4
    VB Addict Pradeep1210's Avatar
    Join Date
    Apr 2004
    Location
    Inside the CPU...
    Posts
    6,614

    Re: Using global variables

    Quote Originally Posted by levent View Post

    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
    Pradeep, Microsoft MVP (Visual Basic)
    Please appreciate posts that have helped you by clicking icon on the left of the post.
    "A problem well stated is a problem half solved." — Charles F. Kettering

    Read articles on My Blog101 LINQ SamplesJSON ValidatorXML Schema Validator"How Do I" videos on MSDNVB.NET and C# ComparisonGood Coding PracticesVBForums Reputation SaverString EnumSuper Simple Tetris Game


    (2010-2013)
    NB: I do not answer coding questions via PM. If you want my help, then make a post and PM me it's link. If I can help, trust me I will...

  5. #5

    Thread Starter
    Addicted Member
    Join Date
    Aug 2005
    Posts
    222

    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

  6. #6
    Super Moderator si_the_geek's Avatar
    Join Date
    Jul 2002
    Location
    Bristol, UK
    Posts
    41,974

    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.

  7. #7

    Thread Starter
    Addicted Member
    Join Date
    Aug 2005
    Posts
    222

    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.

  8. #8
    Super Moderator si_the_geek's Avatar
    Join Date
    Jul 2002
    Location
    Bristol, UK
    Posts
    41,974

    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
  •  



Click Here to Expand Forum to Full Width