| | |
Initialisation of Variables
![]() |
•
•
Join Date: Mar 2008
Posts: 324
Reputation:
Solved Threads: 7
hey tell me one more thing,Its necessary to declare dim i as integer within any procedure ex, Form_Load,Button_Click. We cannot declare it outside the procedure.
Like
Dim x As Integer
x = 10
Dim y As Integer
y = 10
Private Sub Command1_Click()
Print x + y
End Sub
Like
Dim x As Integer
x = 10
Dim y As Integer
y = 10
Private Sub Command1_Click()
Print x + y
End Sub
Last edited by sonia sardana; Apr 4th, 2008 at 4:20 pm.
you can declare x and j in outside procedure or event but the assignment must be in procedure or event.
ex :
ex :
Visual Basic 4 / 5 / 6 Syntax (Toggle Plain Text)
Dim i,j as integer Private Sub Command1_Click() i = 10 j = 10 Print x + y End Sub
Never tried = Never Know
So, Please do something before post your thread.
* PM Asking will be ignored *
So, Please do something before post your thread.
* PM Asking will be ignored *
you can also use a variable without declaring the same. but for this you must turn off "Required Variable Declaration" option.
it is always recommended that you use variables after explicitly declaring them. this will help boosting up your program process by preventing memory wastage.
one more thing, you can declare a variable in anywhere inside your code. depending on the declaration section the variable's scope will be determined. if you use it in outside procedure or in general declaration it will treated as a shared or global variable and can be accessed from any procedure present in the same module. now if you declare a variable inside any procedure its scope will be limited to that procedure only where you have declared it. In order to initialize a variable you must follow the way Jx_Man said. and one more thing, the syntax you have used here is applicable in VB.Net not in VB6.
now for example,
declare a variable in form's general declaration section
Dim i as Integer
now the above var. can be accessed from any procedure like,
Private Sub Command1_Click()
i=10
Msgbox i
End Sub
Private Sub Command2_Click()
i=i+val(trim(Text1.text))
Msgbox i
End Sub
now declare another var. c inside a procedure like,
Private Function Sum(a as Integer,b as Integer)
Dim c as Integer
c=a+b
Sum=c
End Sub
now call this from an event like,
Private Sub Command3_Click()
Dim result as Integer
result=Sum(10,20)
Msgbox result
c=100 ''error
Msgbox c ''error
End Sub
now the last two lines inside the above procedure will give an error like "Variable not defined". this is because you have declared the var. C inside the function Sum, so it cannot be accessible from Command3_Click() event. to use it from Command3_Click() event you must declare it explicitly inside it.
hope this will clear your doubts regarding this issue.
regards
Shouvik
it is always recommended that you use variables after explicitly declaring them. this will help boosting up your program process by preventing memory wastage.
one more thing, you can declare a variable in anywhere inside your code. depending on the declaration section the variable's scope will be determined. if you use it in outside procedure or in general declaration it will treated as a shared or global variable and can be accessed from any procedure present in the same module. now if you declare a variable inside any procedure its scope will be limited to that procedure only where you have declared it. In order to initialize a variable you must follow the way Jx_Man said. and one more thing, the syntax you have used here is applicable in VB.Net not in VB6.
now for example,
declare a variable in form's general declaration section
Dim i as Integer
now the above var. can be accessed from any procedure like,
Private Sub Command1_Click()
i=10
Msgbox i
End Sub
Private Sub Command2_Click()
i=i+val(trim(Text1.text))
Msgbox i
End Sub
now declare another var. c inside a procedure like,
Private Function Sum(a as Integer,b as Integer)
Dim c as Integer
c=a+b
Sum=c
End Sub
now call this from an event like,
Private Sub Command3_Click()
Dim result as Integer
result=Sum(10,20)
Msgbox result
c=100 ''error
Msgbox c ''error
End Sub
now the last two lines inside the above procedure will give an error like "Variable not defined". this is because you have declared the var. C inside the function Sum, so it cannot be accessible from Command3_Click() event. to use it from Command3_Click() event you must declare it explicitly inside it.
hope this will clear your doubts regarding this issue.
regards
Shouvik
Shouvik_The_Expert_Coder
Have a problem? Don't worry just give me a call and I'll fix it for you.
Have a problem? Don't worry just give me a call and I'll fix it for you.
sorry i wrong :
Visual Basic 4 / 5 / 6 Syntax (Toggle Plain Text)
Dim x,y as integer Private Sub Command1_Click() x = 10 y = 10 Print x + y End Sub
Never tried = Never Know
So, Please do something before post your thread.
* PM Asking will be ignored *
So, Please do something before post your thread.
* PM Asking will be ignored *
•
•
Join Date: Mar 2008
Posts: 324
Reputation:
Solved Threads: 7
Hey Jx_Man, hey these are small errors even a fresher like me will be able to find it out. No Sorry.
Hey Choudhary See the prog below--
Private Sub Command2_Click()
i = 20
j = 10
Print i - j
End Sub
Private Sub Command3_Click()
Dim i As Integer
Dim j As Integer
i = 10
j = 20
Print i + j
End Sub
No error is coming in the above prog,as u said if u declare the variables inside the procedure they are local to the procedure.
And also no error is coming in ur prog(Last two lines).
Second thing, By Default Option Explicit is off in Vb, & on in VB.Net.
Therefore even if i n not declaring the variables,noo error is coming see below
Private Sub Command3_Click()
i = 10
j = 20
Print i + j
End Sub
Plz check variables defined inside the procedure are local to the procedure or not & REply.
FUNCTIONs--
I want to ask there is no return statement in VB, then how we do the foll. prog-
Private Sub Command1_Click()
Dim sum As Integer
sum = Add(10, 20)
Print sum
End Sub
Private Function sum(a As Integer, b As Integer)
Dim c As Integer
c = a + b
return c
End Function
Hey Choudhary See the prog below--
Private Sub Command2_Click()
i = 20
j = 10
Print i - j
End Sub
Private Sub Command3_Click()
Dim i As Integer
Dim j As Integer
i = 10
j = 20
Print i + j
End Sub
No error is coming in the above prog,as u said if u declare the variables inside the procedure they are local to the procedure.
And also no error is coming in ur prog(Last two lines).
Second thing, By Default Option Explicit is off in Vb, & on in VB.Net.
Therefore even if i n not declaring the variables,noo error is coming see below
Private Sub Command3_Click()
i = 10
j = 20
Print i + j
End Sub
Plz check variables defined inside the procedure are local to the procedure or not & REply.
FUNCTIONs--
I want to ask there is no return statement in VB, then how we do the foll. prog-
Private Sub Command1_Click()
Dim sum As Integer
sum = Add(10, 20)
Print sum
End Sub
Private Function sum(a As Integer, b As Integer)
Dim c As Integer
c = a + b
return c
End Function
Last edited by sonia sardana; Apr 5th, 2008 at 3:08 am.
vb Syntax (Toggle Plain Text)
Dim Total As Integer Private Function sum(a, b) Dim c As Integer sum = a + b End Function Private Sub Command1_Click() Dim a, b As Integer a = 1 b = 2 Total = sum(a, b) MsgBox Total End Sub
Proud to be Philippine made...
Hi,
A Function Declaration in VB6 is as :
Return is the same as Function Name..("Sum" in above case)
Regards
Veena
A Function Declaration in VB6 is as :
vb Syntax (Toggle Plain Text)
Private Function sum(a, b) As Double sum = a + b End Function
Return is the same as Function Name..("Sum" in above case)
Regards
Veena
![]() |
Similar Threads
- problem about array (C)
- numFactors - Counting Factors/Outputting Factors (Java)
- ":" other than inheritance? (C++)
- how to move to the second line in C++ .txt file reading? (C++)
- first cannot conver std::string to const char, now runtime error! (C++)
- C problen with Files (C)
- What means "best practice"? (Java)
Other Threads in the Visual Basic 4 / 5 / 6 Forum
- Previous Thread: Calling c++ dll in vb
- Next Thread: Regarding Database-Reply early-Urgent
| Thread Tools | Search this Thread |
* 6 429 2007 access activex add age application basic beginner birth bmp calculator cd cells.find click client code college component connection connectionproblemusingvb6usingoledb copy creat ctrl+f data database datareport date delete dissertations dissertationthesis dissertationtopic edit error excel excelmacro file filename form hardware header iamthwee image inboxinvb internetfiledownload keypress label listbox listview liveperson login looping machine microsoft movingranges number objectinsert open oracle password prime program prompt range-objects readfile reading record refresh remotesqlserverdatabase report save search sendbyte sites sort sql sql2008 sqlserver subroutine tags textbox time urldownloadtofile vb vb6 vb6.0 vba visual visualbasic visualbasic6 web window windows






