|
-
Nov 2nd, 2000, 01:56 PM
#1
Thread Starter
Fanatic Member
Hi everyone,
I want to ask for your opinion. Below there are two sub procedures. Both does the samething. Only difference is the declaration of the variables. Does it matter if I declare a variable on the fly or is it more appropriate to declare all variables at the beginning?
Method1
Code:
Sub Main()
Dim str_Data As String
str_Data = "Method1"
MsgBox str_Data
Dim int_X As Integer
int_X = 1
MsgBox int_X
End Sub
Method2
Code:
Sub Main()
Dim str_Data As String
Dim int_X As Integer
str_Data = "Method2"
MsgBox str_Data
int_X = 1
MsgBox int_X
End Sub
Chemically Formulated As:
Dr. Nitro
-
Nov 2nd, 2000, 01:58 PM
#2
Fanatic Member
I think it doesn't matter. Before compiling, vb automatically moves the lines to the beginning.
-
Nov 2nd, 2000, 02:03 PM
#3
_______
"A myth is not the succession of individual images,
but an integerated meaningful entity,
reflecting a distinct aspect of the real world."
___ Adolf Jensen
-
Nov 2nd, 2000, 02:05 PM
#4
Fanatic Member
From what I've read, it's standard programming procedure (whatever that means ) to declare all variables at the top of procedures.
All the best.
-
Nov 2nd, 2000, 05:37 PM
#5
Thread Starter
Fanatic Member
Thank you Oetje, HeSaidJoe and OneSource. I have a very unorthodox way of programming and was just concern about the performance of the program.
Thank you again.
Chemically Formulated As:
Dr. Nitro
-
Nov 2nd, 2000, 06:00 PM
#6
Frenzied Member
Nope, it doesn't matter where you declare it.
But it's a good idea to declare them at the top 'cause otherwise you'll get confused wheter you declared it or not.
But like HeSaidJoe said, it's just a matter of preference
But I also like declaring strings and integers like this
Code:
Dim MyStr$ 'Instead of ... As String
Dim MyInt% 'Instead of ... As Integer
'I think it looks neet, simple and professional :)
Jop - validweb.nl
Alcohol doesn't solve any problems, but then again, neither does milk.
-
Nov 2nd, 2000, 06:14 PM
#7
Fanatic Member
Thanks Jop.
I didn't know that variables could be dimmed like that.
-
Nov 2nd, 2000, 06:45 PM
#8
Hyperactive Member
Godammn!
Have you ever tried to work your way through code that is written in an unorthodox way? Couple that with converting legacy VB3 apps to VB6 and the headache potential is huge . . .
-
Nov 2nd, 2000, 07:48 PM
#9
Re: Thanks Jop.
Originally posted by OneSource
I didn't know that variables could be dimmed like that.
I didn't know that variables could be declared like that.
-
Nov 2nd, 2000, 08:10 PM
#10
Fanatic Member
Megatron...
Originally posted by Megatron
Originally posted by OneSource
I didn't know that variables could be dimmed like that.
I didn't know that variables could be declared like that.
What's the big deal? Just a little VB slang!
-
Nov 2nd, 2000, 09:56 PM
#11
Thread Starter
Fanatic Member
Your style does the job Jop but I am use to Hungarian Notation. I think most people at my company use your style of notation.
Thanks everyone!
Chemically Formulated As:
Dr. Nitro
-
Nov 2nd, 2000, 10:00 PM
#12
Fanatic Member
A kind of ReDim?
Hey Nitro, your question made me wonder about the following situation:
Code:
Private Sub Form_Load()
Dim a As Integer
For a = 1 To 10
Dim b As Long
b = b + 1
Next a
Debug.Print b
End Sub
Does anyone know? Does b get dimmed once or ten times in this code?
Just wondering.
-
Nov 3rd, 2000, 03:02 AM
#13
Hyperactive Member
As we all now, vb's garbage collection is not the best when it comes to fairly complicated objects. This is why you should not declare objects 'on the fly' in vb. Declare them in the morning, and clean them up last thing before you go to bed.
Here's a question:
What happens to an objects passed byVal to a function,
when the function loses scope? i.e.
public sub WhereDidMyMemoryGo(Byval oAnyOldObject as object)
'/ some stuff
end sub
You could make this question harder by asking what if the function is in another apartment, thread, or process (who's doing then marshalling;-) ??
td.
"One logical slip and an entire scientific edifice comes tumbling down." - Robert M. Pirsig
[email protected]
"but if Einstein is right and God is in the details, reality requires that we sometimes get religion." - Scott Meyers.
-
Nov 3rd, 2000, 04:16 AM
#14
Hyperactive Member
10X Loop
Hax,
it gets declared 10 times. VB may well manage the memory for this and just reuse the same space, but get into habits like this and the world may stop . . .
-
Nov 3rd, 2000, 05:01 AM
#15
Fanatic Member
Sloppy
If you want some really sloppy code, don't declare them at all and let VB hand you a big fat Variant...
Jop,
only problem with Dim int%, str$ is readability - if you do not know the post fixes it can be confusing - As String is unambiguous.
As a matter of style I always declare my intentions at the start...
Cheers,
Paul.
Not nearly so tired now...
Haven't been around much so be gentle...
-
Nov 3rd, 2000, 05:59 AM
#16
From a style and maintenance point of view, you should declare you variables at the beginning of the procedure with meaningful names, preferably including some form of hungarian notation. Don't use the postfix declaration, cos it is a dog to maintain. Don't call your variables x or a - use something decent.
The only exception for that is if you need to declare an object variable that is memory hungry, or takes while to initialise, and is only used in certain conditions:
eg:
Code:
Private Sub Process()
Dim lstrText as String
Dim llngLoopCount as Long
Dim lobjItem as SomeObject
'Do some stuff
For llngLoopCount = 1 to 100
lstrText = "XXX"
lobjItem = GetItem(llngCount, lstrText)
If lobjItem.Err = 12345 then
'This is a very rare case (ie. the error is rare)
'and needs a special handling mechanism
Dim lobjSpecialObject as SomeLargeObject
lobjSpecialObject.HandleError
Set lobjSpecialObject = Nothing
End If
Next llngCount
End Sub
The above example ONLY declares the SomeLargeObject, which takes a long time to initialise (for whatever reason) if the condition where it is used is required. This means that the process time for the loop when an error doens't happen is reduced, and the memory the SomeLargeObject requires is not used unless absolutely required.
Having said that, I would only recommend using that conditional declaration approach in special cases, and only for large memory or init-time intensive object. I wouldn't use it for any sort of standard data type - they are fast to create and use very little memory.
Even if you are only coding by yourself and not in a team, it's good practice to make sure you code conforms to some standards, cos when you do end up working in a team environment, your life will be that much easier, and you code very easy to maintain, which makes everybody happy. There is nothing worse than having to maintain spaghetti code. I know, I've been there many times. 
- gaffa
(I hate editing for typos)
[Edited by gaffa on 11-03-2000 at 06:03 AM]
-
Nov 3rd, 2000, 06:08 AM
#17
Hyperactive Member
Ermm...
Gaffa,
I was under the impression that VB compiled then ran and so would read all the variable declarations independently of whether they are in an If..Then..Else statement. Consequently the memory would still be in use.
I am willing to be corrected - for as MicroSoft have proved recently 'nobody is infallible'
-
Nov 3rd, 2000, 06:19 AM
#18
To be honest gravy boy, you could be right... I'm not actually as sure now that I think about it. I do seem to recall reading about this technique somewhere, but it is entirely possible I'm wrong. I jsut spoke to another guy I work with, and he's is also under the impression that conditional declarations work. But.......who knows.
Probably worth some further research. I very, very rarely use the technique though...
- gaffa
-
Nov 3rd, 2000, 06:36 AM
#19
Hyperactive Member
Technically it does not matter where you declare a variable.
It is not initialised until the first time it is used.
If you declare a variable in a loop, you are still only declaring it once.
td.
"One logical slip and an entire scientific edifice comes tumbling down." - Robert M. Pirsig
[email protected]
"but if Einstein is right and God is in the details, reality requires that we sometimes get religion." - Scott Meyers.
-
Nov 3rd, 2000, 06:41 AM
#20
Hyperactive Member
-
Nov 3rd, 2000, 06:53 AM
#21
Fanatic Member
Variable lifetimes...
Hi td,
You are partially correct, but - it depends where the variables are - if they are declared in a BAS module, for instance, then memory will be allocated and the variables initialised to their default values on the Application starting up. They will retain memory until the application exits even if they are never used!. Variables in Forms and Class modules share the life of that form or class. Only Dynamic local variables (i.e. declared within a procedure) will not exist until the procedure is run. A Dim will allocate memory and set the value as default - the variable does not have to be used to take up memory.
Cheers,
Paul.
Not nearly so tired now...
Haven't been around much so be gentle...
-
Nov 3rd, 2000, 08:57 AM
#22
Hyperactive Member
Paul,
I know, i was only pointing out about the about the location in a looooooooop.
I take it your a city lad, how's Fortis.
td.
"One logical slip and an entire scientific edifice comes tumbling down." - Robert M. Pirsig
[email protected]
"but if Einstein is right and God is in the details, reality requires that we sometimes get religion." - Scott Meyers.
-
Nov 3rd, 2000, 09:01 AM
#23
Fanatic Member
Hi td,
You could say Fortis is tumblingdown
Where are you?
It is not initialised until the first time it is used.
that made me believe you were not aware of the memory implications of Dim and its default values etc.
Quite right about the loop.
Sorry for dissin' you.
Cheers,
Paul.
Not nearly so tired now...
Haven't been around much so be gentle...
-
Nov 3rd, 2000, 09:08 AM
#24
Hyperactive Member
Well i suppose technically i'm at CSFB, but i think most of the time i'm just off in my own little world...
Oh, and just got back after a few bevvies at lunch. Best time of the day, that post liquid lunch feeling ;-)
td.
"One logical slip and an entire scientific edifice comes tumbling down." - Robert M. Pirsig
[email protected]
"but if Einstein is right and God is in the details, reality requires that we sometimes get religion." - Scott Meyers.
-
Nov 3rd, 2000, 09:11 AM
#25
Hyperactive Member
Wayhay! Same as meself . . . .
I often find that if you go out drinking with the management more gets done in terms of man-management . . .
-
Nov 3rd, 2000, 09:17 AM
#26
Fanatic Member
OK gravyboy and tumblingdown - we obviously cannot pass up the chance to have a career enhancing beer (or seven). However I am assuming that you are down in Canary Wharf - could be tricky.
Send me a mail with some contact details if you fancy some liquid refreshment.
mail me at [email protected]
P.S. Sorry for hijacking your thread Nitro
Cheers (or should that be Bottoms Up!)
Paul.
Not nearly so tired now...
Haven't been around much so be gentle...
-
Nov 3rd, 2000, 09:20 AM
#27
Hyperactive Member
Oh, and just got back after a few bevvies at lunch. Best time of the day, that post liquid lunch feeling ;-)
Unfortunately I have just left the Marble Arch in Manchester and may be a litte late for the beer . . .
-
Nov 3rd, 2000, 09:33 AM
#28
Fanatic Member
Spooky...
gravyboy,
according to your profile you are with Centrica (I had assumed that when you said 'same as meself', you were talking about CSFB not the beer (how stupid can I be....),
however, getting back to the point I used to work for BG before it split into a million pieces...
VB World; British Gas; Beer spooky coincidences, or what...
What (hehehe)
and Yeh I have had a few beers meself...
Cheers,
Paul.
Not nearly so tired now...
Haven't been around much so be gentle...
-
Nov 3rd, 2000, 09:36 AM
#29
Hyperactive Member
"One logical slip and an entire scientific edifice comes tumbling down." - Robert M. Pirsig
[email protected]
"but if Einstein is right and God is in the details, reality requires that we sometimes get religion." - Scott Meyers.
-
Nov 3rd, 2000, 09:37 AM
#30
Hyperactive Member
There's another BG bod around here Gary.Lowe . . . ex BG anyway!
-
Nov 3rd, 2000, 09:38 AM
#31
Hyperactive Member
btw, now beating you by 2 posts hehe.
td
"One logical slip and an entire scientific edifice comes tumbling down." - Robert M. Pirsig
[email protected]
"but if Einstein is right and God is in the details, reality requires that we sometimes get religion." - Scott Meyers.
-
Nov 3rd, 2000, 09:50 AM
#32
Fanatic Member
Not nearly so tired now...
Haven't been around much so be gentle...
-
Nov 3rd, 2000, 09:51 AM
#33
Fanatic Member
Not nearly so tired now...
Haven't been around much so be gentle...
-
Nov 3rd, 2000, 09:51 AM
#34
Fanatic Member
Not nearly so tired now...
Haven't been around much so be gentle...
-
Nov 3rd, 2000, 09:52 AM
#35
Fanatic Member
one more than you. HEHEHEHEHE
Cheers,
Paul.
Not nearly so tired now...
Haven't been around much so be gentle...
-
Nov 3rd, 2000, 09:54 AM
#36
Hyperactive Member
****, beating me.
oh no, your not afterall. would you look at that.
td.
"One logical slip and an entire scientific edifice comes tumbling down." - Robert M. Pirsig
[email protected]
"but if Einstein is right and God is in the details, reality requires that we sometimes get religion." - Scott Meyers.
-
Nov 3rd, 2000, 10:00 AM
#37
Fanatic Member
Sorry to interrupt guys
But I'm just removing the automatic "email notification" option for me for this post.
-
Nov 3rd, 2000, 10:04 AM
#38
Hyperactive Member
sorry mate, we'll stop.
Oi! Paul, pack it in! i said No!
td.
"One logical slip and an entire scientific edifice comes tumbling down." - Robert M. Pirsig
[email protected]
"but if Einstein is right and God is in the details, reality requires that we sometimes get religion." - Scott Meyers.
-
Nov 3rd, 2000, 10:16 AM
#39
Fanatic Member
Not me guv.
I was over in Chit-Chat wondering about CHICKEN.
BTW td - you are sick (can't wait for the beer) 
Paul
P.S. Sorry everybody, we'll stop now.
Not nearly so tired now...
Haven't been around much so be gentle...
-
Nov 3rd, 2000, 11:04 AM
#40
Junior Member
Just some more automatic data type definition characters
I'm not sure if it actually speeds up your project.
Code:
dim something$ 'string
dim something% 'integer
dim something& 'long
dim something! 'single
dim something# 'double
Paul Bousa
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
|