|
-
Aug 13th, 2004, 07:52 AM
#1
Thread Starter
Addicted Member
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.
-
Aug 13th, 2004, 07:57 AM
#2
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
-
Aug 13th, 2004, 07:59 AM
#3
Hyperactive Member
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
-
Aug 13th, 2004, 08:04 AM
#4
Thread Starter
Addicted Member
Thanks
but can you exsplian in more detail please leinad31 a good analogy
-
Aug 13th, 2004, 08:10 AM
#5
Thread Starter
Addicted Member
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.
-
Aug 13th, 2004, 08:18 AM
#6
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.
-
Aug 13th, 2004, 08:18 AM
#7
Addicted Member
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
-------------------------------
-
Aug 13th, 2004, 08:22 AM
#8
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:
Dim strText as String
Dim lngValue as Long
strText="50"
lngValue = 10
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
-
Aug 13th, 2004, 08:36 AM
#9
Thread Starter
Addicted Member
HAHA
Yer see what you can learn when you think its a stupid question
Thanks people
-
Aug 13th, 2004, 08:38 AM
#10
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
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|