Results 1 to 7 of 7

Thread: Is "Dim" obsolete? Should now use Private for all local vars?

  1. #1

    Thread Starter
    Hyperactive Member
    Join Date
    Aug 2014
    Posts
    313

    Is "Dim" obsolete? Should now use Private for all local vars?

    Is "Dim" obsolete? Should now use Private for all local vars?

  2. #2
    Sinecure devotee
    Join Date
    Aug 2013
    Location
    Southern Tier NY
    Posts
    6,582

    Re: Is "Dim" obsolete? Should now use Private for all local vars?

    I'm not sure why you ask that.
    I assume by local vars you mean variables declared in a Sub or Function.
    If you tried to use "Private" in a Sub or Function when declaring a variable you should get a compilation error. You have to use Dim in Subs and Functions.

    Why would you even need to ask?

    If you're talking about class level variables, then you can use Dim, but what Dim means is ambiguous. You may not know whether the variable is Private or Public by default, so it is best not to use Dim, but to be specific and use Private or Public or Friend, etc.. as the case may be.
    "Anyone can do any amount of work, provided it isn't the work he is supposed to be doing at that moment" Robert Benchley, 1930

  3. #3
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    110,344

    Re: Is "Dim" obsolete? Should now use Private for all local vars?

    Dim is not and will never be obsolete. It is the keyword that indicates that a declared identifier is a variable, just as there are keywords that indicate a property, method (Sub and Function) or event. Dim is used on both local and member variables. On local variables it is always explicit, while on member variables it can be explicit or implicit. A full member variable declaration actually looks like this:
    vb.net Code:
    1. Private Dim someVariable As SomeType
    just as a property declaration looks like this:
    vb.net Code:
    1. Public Property SomeProperty As SomeType
    If you include an access modifier, e.g. Private, then the Dim keyword is optional but it is still implied if you omit it. Older versions of VS used to allow you to included both but recent versions will actively remove the Dim if you provide an access modifier.

    Everything that you can declare has a default access level so you can never specify an access modifier and everything will be the default. For instance, a member variable is Private by default in a class and Public by default in a structure. Do you know what those defaults are? Of course you don't. Very few people do and no one wants to have to remember them. You should ALWAYS be explicit in specifying an access level so all your fields, i.e. member variables, should be declared Private and so VS will always remove the Dim keyword even if you provide it. It is still implied though.

  4. #4

    Thread Starter
    Hyperactive Member
    Join Date
    Aug 2014
    Posts
    313

    Re: Is "Dim" obsolete? Should now use Private for all local vars?

    I rarely create my own objects. So, yea, just for sub's in my Form1 class. They still need Dim. Ok.

  5. #5
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    110,344

    Re: Is "Dim" obsolete? Should now use Private for all local vars?

    Quote Originally Posted by RipVoidWinkle View Post
    I rarely create my own objects.
    That's clearly not true. You create objects all the time. Objects are created at run time. Every String, every form, every data adapter, every DataTable, etc, etc, is an object. Presumably, you actually mean that you rarely define your own classes. Unless your projects are all very simple - possibly even then - you should change that.

  6. #6

    Thread Starter
    Hyperactive Member
    Join Date
    Aug 2014
    Posts
    313

    Re: Is "Dim" obsolete? Should now use Private for all local vars?

    I write simple 1 form apps. All code is in the one class, Form1. And some subs. No user defined objects. So, yea, I need Dim

  7. #7
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    110,344

    Re: Is "Dim" obsolete? Should now use Private for all local vars?

    Quote Originally Posted by RipVoidWinkle View Post
    No user defined objects.
    Again, no user-defined TYPES, not objects. You define types and then each instance of that type that gets created at run time is an object. Form1 is a class. At run time, an instance of that class - an object - is created and that's what you see on the screen.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width