Computing.Net > Forums > Programming > VB - structuring and variables

Computer Problems? Computing.Net has over 1,000,000 posts about all things technology related! Over 90% answered within 24 hours! Click here to start participating now! Also, be sure to check out the New User Guide.

VB - structuring and variables

Reply to Message Icon

Name: newmanjim
Date: December 13, 2008 at 00:52:08 Pacific
OS: WXP
CPU/Ram: 2.9mhz/2g
Product: Gateway / NOT SURE
Comment:

Message: I've always written pretty structured code in other languages but I've also always had a problem doing so in VB. When I write a SELECT-CASE or IF-THEN-ELSE I'd like to use CALL to branch to the code to be executed. When I do this I lose whatever is calculated in variables. I know the problem has something to do with how the variables are defined. If anyone can shed some light on what I'm doing wrong I would appreciate it. I've never figured this out and my code is really hard to read without better structure.



Sponsored Link
Ads by Google

Response Number 1
Name: StuartS
Date: December 13, 2008 at 03:11:37 Pacific
Reply:

Are you using VB6 or VB.Net because there is a huge difference in the way variables are passed.

In VB6 they are passed by reference by default which means that keeping the value is automatic unless you pass by value using By Val.

In VB.Net everything changed and variables are passed by value by default to bring it more into line with the C++ calling convention.

If you wish to retain the value and use any values calculated in the function, you need to pass the variable by reference and not leave it at the default by value. The Help files should tell you how to do that.

Stuart


0

Response Number 2
Name: newmanjim
Date: December 14, 2008 at 01:02:10 Pacific
Reply:

Could the problem be that I've used DIM to declare the variables instead of PUBLIC?


0

Response Number 3
Name: StuartS
Date: December 14, 2008 at 03:04:28 Pacific
Reply:

Depends on how you are using the variables. If you are passing the variables as parameters in the CALL statement it makes no difference how the variables are declared.

However if you are using the same variables in the main code as well as the function then it will make a difference. However, PUBLIC variables should only be used if the variables is referenced from outside the module it is declared in, otherwise use DIM and pass them as a parameter.

Stuart


0

Response Number 4
Name: newmanjim
Date: December 14, 2008 at 22:02:57 Pacific
Reply:

I'm not passing the variables with the CALL statements. I'm using CALL to branch.

I went back and changed the DIM statements to PUBLIC and was able to structure the program and have data passed in variables from one subroutine to another. I'm just wondering why you wouldn't want to do this? Passing with CALL seems to be more prevalent.


0

Response Number 5
Name: StuartS
Date: December 15, 2008 at 05:29:34 Pacific
Reply:

Using the PUBLIC declaration takes up more memory than just keeping the variables local. Once used PUBLIC variables remain in memory throughout the life of the module that uses them. Local variables are destroyed and the memory freed when the function is terminated. Passing variables with the CALL statement is more prevalent because it is the correct way of doing it.

When you get onto more complex programmes keeping variables Local as much as possible makes it far easier to debug and maintain and a lot easier for someone else to understand you code.

PUBLIC, as with GLOBAL variables, can get you into a terrible mess at times and should only be used when there is no other option.

Stuart


0

Related Posts

See More



Sponsored Link
Ads by Google
Reply to Message Icon






Post Locked

This post is quite old and has been locked from receiving new replies. Please create a new posting instead.


Go to Programming Forum Home


Sponsored links

Ads by Google


Results for: VB - structuring and variables

Question Regarding VB.net and C# www.computing.net/answers/programming/question-regarding-vbnet-and-c/3580.html

Question on structures www.computing.net/answers/programming/question-on-structures/7400.html

Questions on structures www.computing.net/answers/programming/questions-on-structures-/7378.html