Results 1 to 13 of 13

Thread: Reference between VB projects - value not retained

  1. #1

    Thread Starter
    New Member
    Join Date
    Feb 2008
    Posts
    8

    Reference between VB projects - value not retained

    Using VB6.

    I have a main project with a module which contains various Global objects like so:

    Global objOle1 As Object
    Global objOle2 As Object
    Global objOle3 As Object
    Global objOle4 As Object
    Global objOle5 As Object

    Then I have another sub-project that has a reference to this main project. However when I try to use these objects in my sub - project the values aren't retained from the main project. However, I can see the reference works as the objects appear in the Intellisense thing. Can anyone explain why?

  2. #2

    Thread Starter
    New Member
    Join Date
    Feb 2008
    Posts
    8

    Re: Reference between VB projects - value not retained

    found my problem isn't even between projects but between functions in the same project.
    i made a global module with Public declarations now like so:
    Public userName As String
    Public usrPwd As String
    Public owcResult As Integer
    Public owcAvail As Boolean
    Public currChart As String
    I have a Public Sub init function, just takes the variables and gives it some initial values. However, trying to exec any other function afterwards and I check the values they are lost. They only exist within the init function. I thought global/public variables exists within the life of the project?
    Iam really lost why this happens.....=(

  3. #3
    Banned randem's Avatar
    Join Date
    Oct 2002
    Location
    Maui, Hawaii
    Posts
    11,385

    Re: Reference between VB projects - value not retained

    Yes Public variables exist for the whole project but not for the sub projects.

  4. #4
    PowerPoster
    Join Date
    Jul 2006
    Location
    Maldon, Essex. UK
    Posts
    6,334

    Re: Reference between VB projects - value not retained

    Could you please post the init module, the module where the declarations are made, and one of the functions that experience the problem.

  5. #5
    Banned randem's Avatar
    Join Date
    Oct 2002
    Location
    Maui, Hawaii
    Posts
    11,385

    Re: Reference between VB projects - value not retained

    Quote Originally Posted by Doogle
    Could you please post the init module, the module where the declarations are made, and one of the functions that experience the problem.
    You can't pass variables that way...

  6. #6
    PowerPoster
    Join Date
    Jul 2006
    Location
    Maldon, Essex. UK
    Posts
    6,334

    Re: Reference between VB projects - value not retained

    Exactly why I asked to see the code.

  7. #7

    Thread Starter
    New Member
    Join Date
    Feb 2008
    Posts
    8

    Re: Reference between VB projects - value not retained

    Quote Originally Posted by randem
    You can't pass variables that way...
    hmm even between functions in the same project?

    the code is all quite alot but basically its like this:

    Module:
    Public usrPwd As String
    Public owcResult As Integer
    Public owcAvail As Boolean
    Public currChart As String

    Main project:
    Public Sub Form_Init()

    Set all the variables..bla blah

    msgBox all the variables and works fine

    End Sub

    Public Sub button_Click()

    msg box all variables, and nothing.
    strings are empty.integer is zero. boolean is flipped

    End Sub

  8. #8
    Banned randem's Avatar
    Join Date
    Oct 2002
    Location
    Maui, Hawaii
    Posts
    11,385

    Re: Reference between VB projects - value not retained

    Between functions in the same module yes, but not wise to do so. Between projects, Forget about it!!! You will have to post code you are clearing them somehow.

  9. #9

    Thread Starter
    New Member
    Join Date
    Feb 2008
    Posts
    8

    Re: Reference between VB projects - value not retained

    Quote Originally Posted by randem
    Between functions in the same module yes, but not wise to do so. Between projects, Forget about it!!! You will have to post code you are clearing them somehow.
    Code won't fit and I am not 'clearing' it accidentally. There is no other references to these variables so I am not setting its values anywhere else except upon initialization.

  10. #10
    Banned randem's Avatar
    Join Date
    Oct 2002
    Location
    Maui, Hawaii
    Posts
    11,385

    Re: Reference between VB projects - value not retained

    You are doing something that nobody else who uses VB regularly is doing. Code always fits if you zip it first then post it.

  11. #11
    Banned randem's Avatar
    Join Date
    Oct 2002
    Location
    Maui, Hawaii
    Posts
    11,385

    Re: Reference between VB projects - value not retained

    You could also run your project thru the debugger and add a watch on the variable so that the debugger stops when the value changes.

  12. #12

    Thread Starter
    New Member
    Join Date
    Feb 2008
    Posts
    8

    Re: Reference between VB projects - value not retained

    tried the debugger, but the thing is I know when the value changes/resets. Just Don't know why. Anything executed after the INIT function basically. I'll try to condense the code and post it up.
    In the meantime,do you think the way I am declaring or passing the variable is wrong? is there some design flaw I am missing? Can you suggest any other method?
    I just want to be able to reuse variable results from one function to another (within the same project ...for now)

    I am by no means a VB expert so thanks in advance for your feedback and assistance.

  13. #13
    PowerPoster
    Join Date
    Jul 2006
    Location
    Maldon, Essex. UK
    Posts
    6,334

    Re: Reference between VB projects - value not retained

    With respect to Global variables, some will say it's a matter of style and others a matter of Standards and good practice. I personally believe it's good practice to minimise the number of global variables in a Project and also to minimse the scope of each variable. I can't remember the last time I used the 'Public' statement, or a Module come to that.

    As you have found out, it's easy to 'lose track' of who's done what to which variable where and when, and having too many globals makes code more difficult to understand and maintain.

    Given that you can pass variables as arguments to and from Subroutines and Functions, both by value or by reference, often there's no real need to make them global. It might mean a bit more analysis and design, and tapping a few more keys now but the time / trouble / cost savings in the future are well worth having (IMHO)

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