|
-
Feb 12th, 2008, 08:28 PM
#1
Thread Starter
New Member
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?
-
Feb 13th, 2008, 12:43 AM
#2
Thread Starter
New Member
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.....=(
-
Feb 13th, 2008, 01:57 AM
#3
Re: Reference between VB projects - value not retained
Yes Public variables exist for the whole project but not for the sub projects.
-
Feb 13th, 2008, 02:00 AM
#4
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.
-
Feb 13th, 2008, 02:02 AM
#5
Re: Reference between VB projects - value not retained
 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...
-
Feb 13th, 2008, 02:04 AM
#6
Re: Reference between VB projects - value not retained
Exactly why I asked to see the code.
-
Feb 13th, 2008, 02:11 AM
#7
Thread Starter
New Member
Re: Reference between VB projects - value not retained
 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
-
Feb 13th, 2008, 02:15 AM
#8
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.
-
Feb 13th, 2008, 02:37 AM
#9
Thread Starter
New Member
Re: Reference between VB projects - value not retained
 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.
-
Feb 13th, 2008, 02:39 AM
#10
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.
-
Feb 13th, 2008, 02:43 AM
#11
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.
-
Feb 13th, 2008, 03:13 AM
#12
Thread Starter
New Member
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.
-
Feb 13th, 2008, 03:37 AM
#13
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
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|