declare sub ValidateLeadingSign(Digit as string,MinusExists as Byte)
dim shared as string TxtLine
'Validate unairy + or - sign for number
sub ValidateLeadingSign(_
Digit as string,_
Byval MinusExists as Byte)
Digit = "no negative"
if TxtLine = "-" then
print "before MinusExists = ";MinusExists
MinusExists = not MinusExists
print "after MinusExists = ";MinusExists
Digit = "Found negative"
end if
end sub
Dim Digit as string
Dim MinusExists as Byte
MinusExists = 1
TxtLine = "-"
ValidateLeadingSign(Digit,MinusExists)
Print "value of MinusExists =";MinusExists
If MinusExists then
Print "error a floating point hex number MUST be unsigned."
end if
print Digit
end
The way the code is currently written MinusExists changes its value in the subroutine but never passes it back.
I tried to do it by changing the "byval" to "byref" but get a type mismatch error. How do I change my code to pass back
MinusExists with its value changed? The only other way I got it to work was to have MinusExists be a shared variable.
type _
ValidatedSign
declare constructor( _
byref as const string, _
byval as byte )
as string _
digit
as byte _
minusExists
end type
constructor _
ValidatedSign( _
byref aDigit as const string, _
byval aMinusExists as byte )
digit => aDigit
minusExists => aMinusExists
end constructor
'Validate unairy + or - sign for number
function ValidateLeadingSign( _
byref txtLine as string, _
byval minusExists as byte ) _
as ValidatedSign
dim as string _
digit = "no negative"
if txtLine = "-" then
print "before MinusExists = ";minusExists
minusExists = not minusExists
print "after MinusExists = ";minusExists
digit = "Found negative"
end if
return( ValidatedSign( digit, minusExists ) )
end function
dim as string _
TxtLine = "-"
dim as byte _
MinusExists => 1
var _
result => ValidateLeadingSign( TxtLine, MinusExists )
Print "value of MinusExists =";MinusExists
If result.MinusExists then
Print "error a floating point hex number MUST be unsigned."
end if
print result.Digit
sleep()
StillLearning wrote:
The way the code is currently written MinusExists changes its value in the subroutine but never passes it back. I tried to do it by changing the "byval" to "byref" but get a type mismatch error.
To answer your initial question, this above is the right solution, but you must do the change both in the declaration and in the definition of the subroutine:
declare sub ValidateLeadingSign(Digit as string,byref MinusExists as Byte)
dim shared as string TxtLine
'Validate unairy + or - sign for number
sub ValidateLeadingSign(_
Digit as string,_
byref MinusExists as Byte)
Digit = "no negative"
if TxtLine = "-" then
print "before MinusExists = ";MinusExists
MinusExists = not MinusExists
print "after MinusExists = ";MinusExists
Digit = "Found negative"
end if
end sub
Dim Digit as string
Dim MinusExists as Byte
MinusExists = 1
TxtLine = "-"
ValidateLeadingSign(Digit,MinusExists)
Print "value of MinusExists =";MinusExists
If MinusExists then
Print "error a floating point hex number MUST be unsigned."
end if
print Digit
end
type test
as long number_1
as long number_2
end type
function example1 (param as long ) as test
dim my_udt as test
my_udt.number_1 = param
my_udt.number_2 = param*2
return my_udt
end function
' you can do this
print "example 1"
print example1( 5 ).number_1
print example1( 5 ).number_2
'or you can do this
print "example 2"
dim my_type as test
my_type = example1(10)
print my_type.number_1
print my_type.number_2
'or you can make an array of the type.
print "example 3"
dim my_type_2(1 to 2) as test
my_type_2(1) = example1(10)
my_type_2(2) = example1(3)
print my_type_2(1).number_1
print my_type_2(1).number_2
print my_type_2(2).number_1
print my_type_2(2).number_2
sleep
Also you can send a type into a function and return it like so.
type test
as long number_1
as long number_2
end type
function example2( my_udt as test ) as test
my_udt.number_2 = my_udt.number_1*2
return my_udt
end function
print "example 5"
dim my_type_3 as test
my_type_3.number_1 = 5
my_type_3 = example2(my_type_3)
print my_type_3.number_2
'or
print "example 6"
print example2(Type(8,0)).number_2
sleep