Click to See Complete Forum and Search --> : Why use Dim???


John Reynolds
July 10th, 2001, 03:51 PM
Why not always use Public or Private instead?

Thanks for your input!

John G Duffy
July 10th, 2001, 04:36 PM
I suspect DIM is a holdover from old Basic which had no Private or Public statements.
MSDN help says the prefered method for declaring variables are Private or Public for clarity.
HOwever, if you look at the Public Statement there is a subtle difference in that defining a Public variable in a Class block declares it as Private to that class.

John G

John Reynolds
July 10th, 2001, 04:55 PM
John, EXCELLENT explanation.
Thanks for your help.

John R.

Clearcode
July 11th, 2001, 02:46 AM
In our next revision of our codingstandards for VB document we are considering an edict explcitly stating that Dim and Global should not be used - with Public and Private used instead.
These latter seem to make the developer more aware of the logical scope of each variable and they are also used with Property Get/Set/Let and Enumerated types which makes for better consistency of code.

I'd be interested to hear anybody else'sopinions on that.

-------------------------------------------------
Ex. Datis: Duncan Jones
Merrion Computing Ltd
http://www.merrioncomputing.com

John Reynolds
July 11th, 2001, 09:05 AM
I never realized it before, but when instantiating an instance of a class using . . .

Dim MyClass as new Class1



Dim must appear. Public or Private will not work.

John

Clearcode
July 11th, 2001, 11:37 AM
Again, I prefer not to declare and instantiate a variable at the same time. M$ seem to agree - this is no longer supported in VB.Net so it is a good habit to not do the above.

HTH,
Duncan

-------------------------------------------------
Ex. Datis: Duncan Jones
Merrion Computing Ltd
http://www.merrioncomputing.com

John Reynolds
July 11th, 2001, 11:43 AM
Thanks,Duncan. Just curious, are you currently using VB.NET? If so, will you convert all apps and eventually completely abandon VB6?

Thanks again.
John

Clearcode
July 11th, 2001, 12:42 PM
I'm not using Vb.Net in anger - i.e. nothing released from the company in .Net as yet.

The thinking is a sort of different horses suit different courses approach which means having VB5/6 and .Net development (and knowledge) concurrently.

This is proving to be quite a bind as the learning curve between VB5/6 and VB.Net is huge and you also need to unlearn your .Net stuff when you go back to working in VB5/6. The biggest pain at the moment is the difference between properties.

'vb 5/6 property
public property get Id() as Long
Id = mid
End property
public property let Id(byval lIn as Long)
mid = lIn
End property




Versus VB.Net Beta 1 code:

public property Id as Long
get
Return mid
End get
let
mid = Value
End let
End property




At the end of the day we will sell (i.e code in) whatever our customers are buying so we need to work on both sides of the learning curve at once....

HTH,
Duncan

-------------------------------------------------
Ex. Datis: Duncan Jones
Merrion Computing Ltd
http://www.merrioncomputing.com

John Reynolds
July 11th, 2001, 12:53 PM
Thanks, I understand. It would sure be nice to be able to meet all demands with one developement system. I guess that is wishful thinking. I personally would prefer one BIG learning curve than 2 (or several, i.e., C++, etc) parallel or different ones.

Duncan, thanks for your explanation.

Cakkie
July 12th, 2001, 01:08 AM
Hmm, that is supported in .Net, that's even one of the new stuff added to VB, instanciation when declared, even able to give parameters to the constructor of that class. The syntax looks only a little different:

Dim myClass as Class1 = new Class1(Someparameter)
' Or, since everything is an object in .Net
Dim myString as string = new string("Testing")
' Or even this, because string is somewhat special
Dim myString as string = "Testing"




Now, about that Dim vs Private Stuff,
Public, Private and Global can only be used in the general declaration section of a module, so it can't be used in a sub or function.
Static on the other hand, can only be used withing a sub or function
Dim can be used everywhere, but I would advice you to use dim only within a sub or function, and everywhere else (that is general declaration section, private and public, improving readability when figuring out the scope of an object/variable

Tom Cannaerts
slisse@planetinternet.be

Programming today is a race between software engineers striving to build bigger and better idot-proof programs, and the universe trying to produce bigger and better idiots. So far, the universe is winning -- Rich Cook

Spectre5000
July 12th, 2001, 09:30 AM
I have to say that, in my opinion, you should use the Dim statement only when a variable is
local to a subroutine, and that variable is not used by any other subroutines. Then you can define
those variables inside each specific subroutine. If the variable is to be used by more than one
subroutine, define them as Public. And classes (as stated in a previous response) define their public
variables as private. This is just my opinion, and this is how I was taught to define variables in
VB 6, so this is a personal preference.

You should always remember to code the way that feels right for you. Develop your own coding style, and
stick with it. No two people code exactly the same, and you should do what you feel is the best way
of doing things.

Spectre5000

Ski99
August 2nd, 2001, 09:17 PM
Except you must use Dim in subs and functions.
Private and Public provide scope above this level.