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?
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.....=(
Re: Reference between VB projects - value not retained
Yes Public variables exist for the whole project but not for the sub projects.
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.
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...
Re: Reference between VB projects - value not retained
Exactly why I asked to see the code.
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
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.
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.
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. :)
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.
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.
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)