Results 1 to 10 of 10

Thread: Significance of dim[Resolved]

  1. #1

    Thread Starter
    Addicted Member
    Join Date
    Jul 2004
    Location
    uk liverpool
    Posts
    238

    Significance of dim[Resolved]

    What is the significance of dim a
    a=shell



    When they both do the same ,also when either of the 2 run IE goes to the task bar id like it to open max..?

    Code

    Example 1

    Private Sub Form_Load()
    Shell ("C:\Program FILES\Internet Explorer\IEXPLORE.EXE")

    End Sub
    .................................................................................................... .....
    Example 2

    Private Sub Form_Load()
    Dim a

    a = Shell("C:\Program FILES\Internet Explorer\IEXPLORE.EXE")

    End Sub


    Thanks all

    Paul
    Last edited by whothis; Aug 13th, 2004 at 08:34 AM.

  2. #2
    PowerPoster
    Join Date
    Nov 2002
    Location
    Manila
    Posts
    7,629
    It is significant if you want to do something with the function call's return value.

    If you want to use the function call's return value then you have to store it to an appropriate data type/object, so you need to declare that such as at procedure level with Dim

  3. #3
    Hyperactive Member Jlarini's Avatar
    Join Date
    Jan 2002
    Location
    São Paulo, Brazil
    Posts
    263
    If you have "Option Explicit" active, if you don't dim(ensinate) the variable, you'll got errors...

    You haven't "Option Explicit" active, VB will ignore the variable not defined and "create one" (sometimes...)


    I highly recommend you use "Option Explicit", cos if you create variables without it, only God knows what hell will happen...

    JL
    nothing is impossible, it's sometimes very hard to do!

    If your thread is solved... Please edit it and add [Resolved] or [Solved] on it!

    If you like Marine aquarium, feel free to PM me.

    Sorry my bad English

    God bless Parksie!

  4. #4

    Thread Starter
    Addicted Member
    Join Date
    Jul 2004
    Location
    uk liverpool
    Posts
    238

    Thanks

    but can you exsplian in more detail please leinad31 a good analogy

  5. #5

    Thread Starter
    Addicted Member
    Join Date
    Jul 2004
    Location
    uk liverpool
    Posts
    238

    Excuse my ingorance

    But i need to read a lot more ,one question opens up another and another thiers no straight anwser to anything .the first year of vb must be the hardest.

  6. #6
    Super Moderator si_the_geek's Avatar
    Join Date
    Jul 2002
    Location
    Bristol, UK
    Posts
    41,974
    Shell is a function, which means that along with doing some work (in this case running a program) it returns a value.

    I would assume that the value basically says whether or not the program was started successfully.

    In the first code (just Shell(...)) the return value is ignored, in the second version (a = Shell(...)) the value is stored, but then nothing happens with it.


    Basically, the first version (without using a variable) is what you should be using.

  7. #7
    Addicted Member
    Join Date
    Aug 2002
    Location
    Luton, UK
    Posts
    178
    Dim is short for "Dimension" and is used to "Declare" variables - usually at the top of a program. This reserves memory space at the beginning of the program so that VB does not have to waste time searching all the way through to find it.

    Apart from speed, here are some benefits :-
    1. If you use a standardised spelling method with capital letters you can type all code in lower case and if it does not change to upper/lower case you know you have not spelled it correctly. Saves a lot of debugging errors.

    2. If you define a data type eg. Dim MyVariable as String and later use something like MyVariable = 100 then VB changes the number to a string (text). In this case it will produce more helpful error messages if we try to do something with it that relates to numbers.

    The only thing that Option Explicit does is force us to declare variables. It has no other effect if we choose not to use it. Like everyhing else here, this is more useful when working with large programs with a lot of code, and is designed to prevent us making errors, or, if we do make them, make the code easier to debug.

    Hope this helps.
    Regards
    BrianB
    -------------------------------

  8. #8
    MS SQL Powerposter szlamany's Avatar
    Join Date
    Mar 2004
    Location
    Connecticut
    Posts
    18,263
    This is really, really simple.

    DIM creates a VARIABLE with a specific datatype.

    There are many, many datatypes available. Strings, integers (of all sizes), float - the list goes on.

    Using a VARIABLE that was not previous DIM'd will make that variable a VARIANT. VARIANTS are sloppy programming - they are large and can lead to problems.

    If you are going to be a successful programmer you must be fully aware of every datatype that every variable is in your program - thus the suggestion to use OPTION EXPLICIT at the top of your code - so that you don't forget to DIM a variable.

    Now, with that said - look at this example.

    VB Code:
    1. Dim strText as String
    2. Dim lngValue as Long
    3.  
    4. strText="50"
    5.  
    6. lngValue = 10
    7.  
    8. lngValue = lngValue * CLng(strText)

    Notice I have two variables - a string and a long.

    I put a number - 50 - into the string.

    I put a number - 10 - into the long.

    When I do the multiplication, I wrap the string in the CLng() function - this function takes the "number" in the string and turns it into a LONG value for the multiplication.

    This is a really good programming practice to understand. Do not allow VB to coerce your datatypes into matching datatypes in your expressions and calculations - this will lead to bugs and odd results.

    HTH

  9. #9

    Thread Starter
    Addicted Member
    Join Date
    Jul 2004
    Location
    uk liverpool
    Posts
    238

    HAHA

    Yer see what you can learn when you think its a stupid question

    Thanks people

  10. #10
    PowerPoster
    Join Date
    Nov 2002
    Location
    Manila
    Posts
    7,629
    One use of the return value is for error handling... lets assume that the called function (can be any value returning function) doesnt generate an error message to the user. Instead it returns an error value (non zero) or zero if successful. It would be then up to you to read the error value and handle it, eg if reutn value is 1 then msgbox "this crap happened", if 2 then msgbox "Could have worked if you did this...", if 3 then msgbox "fatal error... throw away your computer", etc.

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