CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 12 of 12

Thread: Why use Dim???

  1. #1
    Join Date
    Sep 2000
    Posts
    200

    Why use Dim???

    Why not always use Public or Private instead?

    Thanks for your input!


  2. #2
    Join Date
    Apr 2000
    Location
    South Carolina,USA
    Posts
    2,210

    Re: Why use Dim???

    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

  3. #3
    Join Date
    Sep 2000
    Posts
    200

    Re: Why use Dim???

    John, EXCELLENT explanation.
    Thanks for your help.

    John R.


  4. #4
    Join Date
    Dec 1999
    Location
    Dublin, Ireland
    Posts
    1,173

    Re: Why use Dim???

    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
    '--8<-----------------------------------------
    NEW -The printer usage monitoring application
    '--8<------------------------------------------

  5. #5
    Join Date
    Sep 2000
    Posts
    200

    Re: Why use Dim???

    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


  6. #6
    Join Date
    Dec 1999
    Location
    Dublin, Ireland
    Posts
    1,173

    Re: Why use Dim???

    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
    '--8<-----------------------------------------
    NEW -The printer usage monitoring application
    '--8<------------------------------------------

  7. #7
    Join Date
    Sep 2000
    Posts
    200

    Re: Why use Dim???

    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


  8. #8
    Join Date
    Dec 1999
    Location
    Dublin, Ireland
    Posts
    1,173

    Re: Why use Dim???

    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
    '--8<-----------------------------------------
    NEW -The printer usage monitoring application
    '--8<------------------------------------------

  9. #9
    Join Date
    Sep 2000
    Posts
    200

    Re: Why use Dim???

    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.


  10. #10
    Join Date
    Jan 2000
    Location
    Olen, Belgium
    Posts
    2,477

    Re: Why use Dim???

    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
    [email protected]

    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
    Tom Cannaerts
    email: [email protected]
    www.tom.be (dutch site)

  11. #11
    Join Date
    Apr 2001
    Location
    Wisconsin, USA
    Posts
    150

    Re: Why use Dim???

    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


  12. #12
    Join Date
    Aug 2001
    Location
    Australia
    Posts
    1

    Re: Why use Dim???

    Except you must use Dim in subs and functions.
    Private and Public provide scope above this level.


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

Featured