Initialisation of Variables

Reply

Join Date: Mar 2008
Posts: 324
Reputation: sonia sardana has a little shameless behaviour in the past 
Solved Threads: 7
sonia sardana sonia sardana is offline Offline
Posting Whiz

Initialisation of Variables

 
0
  #1
Apr 4th, 2008
Dim i as integer=10
dim j as integer=20

In the above two statements, y the error is coming...How can we initilase the values to the variables???
Reply With Quote Quick reply to this message  
Join Date: May 2007
Posts: 4,439
Reputation: Ezzaral has much to be proud of Ezzaral has much to be proud of Ezzaral has much to be proud of Ezzaral has much to be proud of Ezzaral has much to be proud of Ezzaral has much to be proud of Ezzaral has much to be proud of Ezzaral has much to be proud of Ezzaral has much to be proud of Ezzaral has much to be proud of 
Solved Threads: 510
Moderator
Featured Poster
Ezzaral's Avatar
Ezzaral Ezzaral is offline Offline
Industrious Poster

Re: Initialisation of Variables

 
0
  #2
Apr 4th, 2008
Visual Basic 4 / 5 / 6 Syntax (Toggle Plain Text)
  1. Dim i as integer
  2. i=10
Reply With Quote Quick reply to this message  
Join Date: Mar 2008
Posts: 324
Reputation: sonia sardana has a little shameless behaviour in the past 
Solved Threads: 7
sonia sardana sonia sardana is offline Offline
Posting Whiz

Re: Initialisation of Variables

 
0
  #3
Apr 4th, 2008
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
Last edited by sonia sardana; Apr 4th, 2008 at 4:20 pm.
Reply With Quote Quick reply to this message  
Join Date: Nov 2007
Posts: 2,641
Reputation: Jx_Man is a glorious beacon of light Jx_Man is a glorious beacon of light Jx_Man is a glorious beacon of light Jx_Man is a glorious beacon of light Jx_Man is a glorious beacon of light Jx_Man is a glorious beacon of light 
Solved Threads: 245
Jx_Man's Avatar
Jx_Man Jx_Man is offline Offline
Posting Maven

Re: Initialisation of Variables

 
0
  #4
Apr 5th, 2008
you can declare x and j in outside procedure or event but the assignment must be in procedure or event.
ex :
Visual Basic 4 / 5 / 6 Syntax (Toggle Plain Text)
  1. Dim i,j as integer
  2. Private Sub Command1_Click()
  3. i = 10
  4. j = 10
  5. Print x + y
  6. End Sub
Never tried = Never Know
So, Please do something before post your thread.
* PM Asking will be ignored *
Reply With Quote Quick reply to this message  
Join Date: May 2007
Posts: 537
Reputation: choudhuryshouvi is an unknown quantity at this point 
Solved Threads: 49
choudhuryshouvi's Avatar
choudhuryshouvi choudhuryshouvi is offline Offline
Posting Pro

Re: Initialisation of Variables

 
0
  #5
Apr 5th, 2008
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
Shouvik_The_Expert_Coder
Have a problem? Don't worry just give me a call and I'll fix it for you.
Reply With Quote Quick reply to this message  
Join Date: Nov 2007
Posts: 2,641
Reputation: Jx_Man is a glorious beacon of light Jx_Man is a glorious beacon of light Jx_Man is a glorious beacon of light Jx_Man is a glorious beacon of light Jx_Man is a glorious beacon of light Jx_Man is a glorious beacon of light 
Solved Threads: 245
Jx_Man's Avatar
Jx_Man Jx_Man is offline Offline
Posting Maven

Re: Initialisation of Variables

 
0
  #6
Apr 5th, 2008
sorry i wrong :
Visual Basic 4 / 5 / 6 Syntax (Toggle Plain Text)
  1. Dim x,y as integer
  2. Private Sub Command1_Click()
  3. x = 10
  4. y = 10
  5. Print x + y
  6. End Sub
Never tried = Never Know
So, Please do something before post your thread.
* PM Asking will be ignored *
Reply With Quote Quick reply to this message  
Join Date: Mar 2008
Posts: 324
Reputation: sonia sardana has a little shameless behaviour in the past 
Solved Threads: 7
sonia sardana sonia sardana is offline Offline
Posting Whiz

Re: Initialisation of Variables

 
0
  #7
Apr 5th, 2008
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
Last edited by sonia sardana; Apr 5th, 2008 at 3:08 am.
Reply With Quote Quick reply to this message  
Join Date: Feb 2008
Posts: 122
Reputation: cometburn is an unknown quantity at this point 
Solved Threads: 19
cometburn's Avatar
cometburn cometburn is offline Offline
Junior Poster

Re: Initialisation of Variables

 
0
  #8
Apr 5th, 2008
  1. Dim Total As Integer
  2.  
  3.  
  4. Private Function sum(a, b)
  5. Dim c As Integer
  6. sum = a + b
  7.  
  8. End Function
  9.  
  10. Private Sub Command1_Click()
  11. Dim a, b As Integer
  12. a = 1
  13. b = 2
  14. Total = sum(a, b)
  15. MsgBox Total
  16. End Sub
Proud to be Philippine made...
Reply With Quote Quick reply to this message  
Join Date: Mar 2008
Posts: 324
Reputation: sonia sardana has a little shameless behaviour in the past 
Solved Threads: 7
sonia sardana sonia sardana is offline Offline
Posting Whiz

Re: Initialisation of Variables

 
0
  #9
Apr 5th, 2008
Private Sub Command1_Click()
Dim a, b As Integer
a = 3
b = 2
total = sum(a, b)
MsgBox (total)
End Sub

Private Function sum(a, b)
sum = a + bEnd Function

Prog. is working,But i have doubt the text, marked as red is not correct
Last edited by sonia sardana; Apr 5th, 2008 at 8:12 am.
Reply With Quote Quick reply to this message  
Join Date: Nov 2006
Posts: 848
Reputation: QVeen72 is on a distinguished road 
Solved Threads: 120
QVeen72's Avatar
QVeen72 QVeen72 is offline Offline
Practically a Posting Shark

Re: Initialisation of Variables

 
0
  #10
Apr 5th, 2008
Hi,

A Function Declaration in VB6 is as :

  1. Private Function sum(a, b) As Double
  2. sum = a + b
  3. End Function

Return is the same as Function Name..("Sum" in above case)

Regards
Veena
Reply With Quote Quick reply to this message  
Reply

This thread is more than three months old.
Perhaps start a new thread instead?
Message:


Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC