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???

Dim i as integer
i=10

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

you can declare x and j in outside procedure or event but the assignment must be in procedure or event.
ex :

Dim i,j as integer
Private Sub Command1_Click()
i = 10
j = 10
Print x + y
End Sub

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

sorry i wrong :

Dim x,y as integer
Private Sub Command1_Click()
x = 10
y = 10
Print x + y
End Sub

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

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

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

Hi,

A Function Declaration in VB6 is as :

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

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

Ok...
your program is running without any error that is because you are using Implicit Declaration.

Look into my previous reply. In Implicit declaration vb automatically recognizes all variable of variant data types. so,
in this case if u use a var. without declaring it or you use it after declaration the result will be same. i.e.,

i=10
j=20
Print i+j
''will display 30

or ,

Dim i as Integer,j as Integer

i=10
j=20
Print i+j
''will also display 30

there will be no diff.

Now just add this line Option Explicit in your form's general declaration and then fire the code. now you will notice the difference. Instead of running the compiler will give you the following error :-

Compile Error:
Variable not defined

now if you click on the ok/debug button, it will highlight the line i=20 inside the command2_click() event. the error will occur because the variable i has been declared inside command3_click() event, so its scope is local. to use it outside the command3_click() event you must declare the same globally i.e., in your forms' general declaration section. right after the line Option Explicit.

And your other question,

there is no Return keyword available in vb6. as it is an object based language so to write any function which supposed to return some value it must be assigned to the function itself and this should be the last statement in your function body.

consider this function,(it will accept a character and no. of copies to be printed, then it will create the final string, store inside in a local/auto var. and then finally store it to the function).

Private Function Replicate(Byval source_str as String,byval no_copy as Integer) as String
Dim result as String

result=String(source_str,no_copy)
Replicate=result
End Function

now look into the last line, Replicate=result , where Replicate is the function name and it will return the value store inside the result var. when the same will be called from an outside procedure. it is same as return result in other languages like c++,c# or vb.net.

hope this will again clear your doubts.

regards
Shouvik

sorry i wrong :

Dim x,y as integer
Private Sub Command1_Click()
x = 10
y = 10
Print x + y
End Sub

hi sonia, i was tried my code and it giving a result 20 and not an error.

hi sonia, u just want to add 2 variable with value. don't make it harder.
i was tried jx_man code and its working nice, shouvik and cometburn code working too. and always try to appreciated others comment.

Thank you very much for saying this -

always try to appreciated others comment.

regards
Shouvik

hi sonia, u just want to add 2 variable with value. don't make it harder.
i was tried jx_man code and its working nice, shouvik and cometburn code working too. and always try to appreciated others comment.

thanks friend...:)

HI, it doesn't like that i will not appreciate Other Comments. I will Yaar. Sorry From My Side. I do not want to hurt Anybody or prove that he is wrong. i just give my Opinion what I knw.

SORRY AGAIN.

I do not write dat there is error in Jx_man Code, i write , hey these are small errors even a fresher like me will be able to find it out. No Sorry. see Below--

cz He write Initially
dim i as integer
dim j as integer
i=10
j=20
print x+ y

& he is saying sorry cz he write X+ Y. dats d reason.

you didn't see my corrected post before.
check post #6 -> i was posted for my corrected code.

Dim x,y as integer
Private Sub Command1_Click()
x = 10
y = 10
Print x + y
End Sub

Hi sonia, what about my reply?
did you read it?

there i answered all those which you have noticed and told in my previous reply.

here no one is trying to find your faults. we are all here to give you proper suggestions, tricks,tips and thats all. which reply will be good for you and which is not it completely depends upto you. there is no question about egoism.

just ask your questions,problems whatever you have and we all here will try to help you by the best.

so finally give me a feedback here whether have you got answer to all of your questions which you replied for Me and Jx_Man.

regards
Shouvik

hi JX_man dear friend, Again Sorry. Pls reply,if it is fine?


Now about my problems--
If I check the checkbox Require variable declaration, It means that variables are necessary to declare. Right or Wrong?.. If we don't declare then the error comes.

Second Probs In Vb.Net we write the statement option explicit off,before the Module statement, Till Now i do not know where is the module statement in VB, we have to write it own the Module Statement.

Third thing, where to write option explicit on in the code.

Fourth Thing,by default option explicit is on in Vb.net? & in Vb it is off??? Right or wrong?

1. if u didn't use option explicit you don't have to declare that variable
and vb will execute that without any error.
ex : u assign A with checkbox caption without a declared before
A = Check1.Caption -> this operation executed by vb with none error
but if you set option explicit on u must declare it cause u will get error.
ex : use previous sample you need delcare A = dim A as String
One think, Every variable needed initialitation / declare.
It is a good programming to declare all variable.
2. what you means module statment?
in vb.net we write option explicit off in top of all code same like in vb 6
3. in top of all code write "Option Explicit"
4. yes i think. to set on in vb 6 just add "option explicit".

If I check the checkbox Require variable declaration, It means that variables are necessary to declare. Right or Wrong?.. If we don't declare then the error comes.

Yes, It is a VB IDE setting. It is Form/ module wise. After Checking this in Tools option, when a new Form is added, First Line in the code window of new form is "Option Explicit", if unchecked, and a new form is added, the code window of the new form will not have "Option Explicit". Setting of this does not have any effect on Project properties..

In Vb.Net we write the statement option explicit off,before the Module statement, Till Now i do not know where is the module statement in VB, we have to write it own the Module Statement.
where to write option explicit on

First Line in the Code Window, called as Form Level or General declaration of the form, or on the top of the form you need to write "Option Explicit"

by default option explicit is on in Vb.net? & in Vb it is off??? Right or wrong?

Yes, Immediately after Installing VB6, if you open a Fresh project, "Option Explicit" is Off.

Regards
Veena

Hey, can anybody tell me that as in Vb.net ,Identifiers are there public ,private,protected.
Variables declared as public can be accessed otuside the modules.

In Vb,these are also valid or not.

vb doesn't support object orientation. that's why you can't use protected here. other identifiers private and public can be used here like the way you use in vb.net.

public members can be accessed outside the module (both standard and form modules). even you can access an event of a form module from another form if the same is declared as public. keep in mind all events/sub-routines/functions in vb are declared Private in default.

regards
Shouvik

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.