silly, probably meaninigless question

  • Thread starter Thread starter Matt
  • Start date Start date
M

Matt

I've asked this question to some developers that are much more experienced
than I, and gotten different answers --- and I can't find anything about it
in the documentation.

Dim vs. Private as a variable declaration

What is the difference between the two (I strongly suspect there is none)
 
for global variable declarations, to the compiler i dont think theres a
different. when you declare a variable like so

Dim int As Integer

its automatically set to

Private Dim int As Integer

when you specify Private, i think it just helps with code readability....for
instance i always declare global vatriables Private unless i need them
Public, and only use Dim for local variables....
 
Matt,
It depends on the type of type you are using Dim in. Its also easy enough to
test:

Consider the following:

Friend Class TestClass

Dim Variable As String

End Class

Friend Module TestModule

Dim Variable As String

End Module

Friend Structure TestStructure

Dim Variable As String

End Structure

Variable is private in the TestClass & TestModule, however it is public in
the Structure.

Rather then relay on what visibility Dim may or may not be. I normally
always use Private, Friend, Protected, Protected Friend, or Public when
defining fields in my types.

--
Hope this helps
Jay B. Harlow [MVP - Outlook]
..NET Application Architect, Enthusiast, & Evangelist
T.S. Bradley - http://www.tsbradley.net


| I've asked this question to some developers that are much more experienced
| than I, and gotten different answers --- and I can't find anything about
it
| in the documentation.
|
| Dim vs. Private as a variable declaration
|
| What is the difference between the two (I strongly suspect there is none)
|
|
 
Matt,

In my idea is the Dim a complete ancient not needed program word in VBNet.

However it has a lot of emmotions by classic VB programmers now using VBNet.
Probably from their old discussions about this with old C programmers. If
you ask them why to use it, than forever the only real anser they give at
the end is, because I find it so nice looking.

To prove that

"For I as integer" is more than enough to declare the "I"
The "as" word does the trick not the dim and very nice and in my opinion
much nicer than in C derived languages as C#..

The Dim is nowhere needed in the current syntax from VBNet in my idea.

The Private, Public, Friend declaration tells something about the scope
(where can I use something) from a variable. Private means only globaly in
the current class. Friend means everywhere in the current assembly and
Public means everywhere.

If Dim is used or not used as in the sample "For I as integer", than it is
forever a private to the level where it is declared.

(Before you think that, I don't want that Dim is removed from VBNet, however
that it is made optional, so that you can decide self to use it or not and
stay compatible with older programs).

I hope this helps,

Cor
 
Hi Matt,

Thank you for posting.

I think there're three main differences between Dim and Private.

1. Elements you can declare with Dim are different from those with Private.

Dim is only used to declare variables. However, Private can be used for
Interface, Class, Structure, Structure members, Procedure, Property, Member
variables,Constant, Enumeration, Event, External declaration and Delegate.

2. Declaration context within which you can use Dim and Private are
different.

Dim can be used in a procedure or function, however Private can't. For
example, suppose there's a procedure named MyProcedure. We could use Dim
to declare the variable in MyProcedure, but we couldn's use Private to
declare it.

Sub MyProcedure()
Dim localVar As String

' we couldn't use Private to declare a variable in a procedure
' the following code will cause an error
Private localVar1 As String

End Sub

3. Elements declared with Dim has the default access level. Elements
declared with Private are always private.

For example, if you use Dim to declare a varible in a Class, the variable
is private because in VB.NET the default access level of member variable in
class is private.

On the contrary, if you use Dim to declare a variable in a Structure, the
variable is public because in VB.NET the default access level of member
variable in structure is public.

Hope this helps.
If you have anything unclear, please feel free to let me know.


Sincerely,
Linda Liu
Microsoft Online Community Support

==================================================
Get notification to my posts through email? Please refer to
http://msdn.microsoft.com/subscriptions/managednewsgroups/default.aspx#notif
ications.

Note: The MSDN Managed Newsgroup support offering is for non-urgent issues
where an initial response from the community or a Microsoft Support
Engineer within 1 business day is acceptable. Please note that each follow
up response may take approximately 2 business days as the support
professional working with you may need further investigation to reach the
most efficient resolution. The offering is not appropriate for situations
that require urgent, real-time or phone-based interactions or complex
project analysis and dump analysis issues. Issues of this nature are best
handled working with a dedicated Microsoft Support Engineer by contacting
Microsoft Customer Support Services (CSS) at
http://msdn.microsoft.com/subscriptions/support/default.aspx.
==================================================

This posting is provided "AS IS" with no warranties, and confers no rights.
 
Hi,

Reading the message from Linda,
If Dim is used or not used as in the sample "For I as integer", than it is
forever a private to the level where it is declared.

See for that the message from Jay, I wrote this to fast.

Cor
 
Linda,

In my opinion are you describing the differences between Private and Dim
while the question was between Dim and Private.

Maybe silly but the same as what is the difference between a duckbill and a
mammal or the difference between a mammal and a duckbill.

Maybe am I in my Germanic (not German) way of using the English language
wrong in this, but in my opinion is there a difference. (English is a member
of the Germanic languages)

The first answer is in my idea that a duckbill does not have direct living
child's but for the rest it is (almost) the same. The second answer is much
wider, in the family of mammals are all kind of types. Some mammals eat only
meat, some only vegetables, some can fly etc. etc.

However even more, the question was:
Dim vs. Private as a variable declaration

What is the difference between the two (I strongly suspect there is none)

Just what I was thinking about reading your answer.

However, I saw that I had made a mistake in my text so thank you for making
me attent on that.

Cor
 
Hi Matt,

I am closely monitoring the newsgroup and I am contacting you to check the
issue status.

If you have anything unclear, please feel free to post in the newsgroup and
we will follow up.

Thank you for using our MSDN Managed Newsgroup Support Service!


Sincerely,
Linda Liu
Microsoft Online Community Support
 
Back
Top