problems with variables in subs

In this category you can exchange your programming questions and solutions.
Post Reply
Fazer
Posts: 30
Joined: Tue Apr 29, 2008 12:43 pm

problems with variables in subs

Post: # 240Post Fazer
Mon May 12, 2008 5:27 pm

I have a problem with variables in subs.
I dont know whats wrong here.
The following program should write a binary number in a label by clicking on button "1" or "2"
to get a binary number e.g. 1001101.
But it doesn`t work correctly!
Why ?


`=======================
`Begin Code
DIM myvalue as string
Myvalue = " "
object myform as form
     Caption = "myprogram "
     Width = 400
     Height = 225
   `Color = &h4F9D00
     object label1 as label
           Caption = "label1"
           Left = 57
           Top = 24
           Width = 112
           Transparent = 1
     end object
     object button1 as button
           Caption = "0"
           Left = 56
           Top = 100
           Width = 49
           TabOrder = 4
           onclick=add0
     end object
   object button2 as button
           Caption = "1"
           Left = 136
           Top = 100
           Width = 49
           TabOrder = 4
           onclick=add1
     end object
end object
myform.showmodal
`===========================
`[Subprograms]
`===========================
Sub add0(myvalue as string)
        myvalue = myvalue  + "0"
       label1.Caption = myvalue
end sub

Sub add1(myvalue as string)
       myvalue = myvalue  + "1"
       label1.Caption = myvalue  
end sub
`End Code
`==========================

PaPi
Posts: 114
Joined: Wed May 16, 2007 11:55 am

problems with variables in subs

Post: # 241Post PaPi
Mon May 12, 2008 7:12 pm

The Myvalue is a "global" variable, not a "parameter" of subroutine, because you can`t  put a variable simultaneously global and local variable.
So the subroutines correctly:

Sub add0()
     Myvalue=Myvalue+"0"
     label1.Caption=Myvalue
end sub

Sub add1()  
     Myvalue=Myvalue+"1"
     label1.Caption=Myvalue
end sub

Note:
Myvalue isn`t binary number, rather a string which "face" of a binary number, you know, aren`t you?

PaPi

Fazer
Posts: 30
Joined: Tue Apr 29, 2008 12:43 pm

problems with variables in subs

Post: # 242Post Fazer
Tue May 13, 2008 6:06 am

[quote author=PaPi link=1210613239/0#1 date=1210619536]The Myvalue is a "global" variable, not a "parameter" of subroutine, because you can`t ?put a variable simultaneously global and local variable.
So the subroutines correctly:

Sub add0()
     Myvalue=Myvalue+"0"
     label1.Caption=Myvalue
end sub

Sub add1() ?
     Myvalue=Myvalue+"1"
     label1.Caption=Myvalue
end sub

Note:
Myvalue isn`t binary number, rather a string which "face" of a binary number, you know, aren`t you?

PaPi[/quote]

Thats all right, I`ve also tried the subroutines above before I explained my problem here in my post.
But this gives an error message:
"error:41 line: 39 object myvalue not declared".
The subroutines doesnt accept the global variable "myvalue"!
But if the variables are declared in the subroutines, they will be local and I will never get my string, which represents a binary number (it could also be a text "abaabab").

But I think there is something wrong with FNXBasic !?


PaPi
Posts: 114
Joined: Wed May 16, 2007 11:55 am

problems with variables in subs

Post: # 243Post PaPi
Tue May 13, 2008 12:49 pm

Yes, I have tried it first too. With same error. But: I`ve make some more other steps:
1. rename all myvalue to mylabel
2. exit from fnxbasic gui (save program in the course of this)
3. start gui: it has compiled and run without error
4. Re(rename) the variable name...     ;-)

And so.
It is as much as saying that the compiler (os the GUI?) has some trouble, really.
Bat the code so can run...

PaPi


Fazer
Posts: 30
Joined: Tue Apr 29, 2008 12:43 pm

problems with variables in subs

Post: # 244Post Fazer
Tue May 13, 2008 1:04 pm

Many thanks for your help!

But this is very strange and enormous complicated! ? :-?

But I will test it!

Marco
Site Admin
Posts: 246
Joined: Sat Sep 15, 2018 8:41 pm

problems with variables in subs

Post: # 245Post Marco
Tue May 13, 2008 3:49 pm

Hi, it is a  `space` bug.
remove the space before the = sign.
and remove the parameters myvalue from the subs, then it works.
best regards

Fazer
Posts: 30
Joined: Tue Apr 29, 2008 12:43 pm

problems with variables in subs

Post: # 246Post Fazer
Tue May 13, 2008 4:12 pm

[quote author=YaBB Administrator link=1210613239/0#5 date=1210693779]Hi, it is a  `space` bug.
remove the space before the = sign.
and remove the parameters myvalue from the subs, then it works.
best regards[/quote]


This solves all my problems! Many thanks! I`m happy now!

Post Reply