PDA

View Full Version : Oxygen Run Time Error



REDEBOLT
11-10-2011, 03:46
I have to admit I'm new to Oxygen.
I'm trying to run the program listed here. (http://www.thinbasic.com/community/showthread.php?9678-Classes-and-Methods&highlight=oxygen+class)


'CLASSES AND METHODS
'

Uses "oxygen","file"
Dim p0,p1 As Long, src As String
Dim dd(100) As Double

src = "

CLASS stat
METHOD Sum() As Double
METHOD average() As Double
/
da(100) As Double
dn As Long
End CLASS

'--------------
methods of stat
'==============
METHOD Sum() As Double
Dim i
For i = 1 To THIS.dn
METHOD += THIS.da(i)
Next
End METHOD
METHOD average() As Double
METHOD = THIS.Sum / THIS.dn
End METHOD
End methods
'====
'TEST
'====
Dim s As stat
s.da=>2,4,6,8,10,12,14,16,18
s.dn=9
Print `Sum: ` str(s.Sum) ` Average: ` str s.average
"

O2_BASIC src

'msgbox 0, o2_view "o2h "+src

If Len(O2_ERROR) Then
MsgBox 0, O2_ERROR : Stop
End If

O2_EXEC




and I get a runtime error.

So I make changes

'CLASSES AND METHODS
'

Uses "oxygen","file"
Dim p0,p1 As Long, src As String
Dim dd(100) As Double

src = "

CLASS stat
METHOD Sum() As Double
METHOD average() As Double
/
da(100) As Double
dn As Long
End CLASS

'--------------
methods of stat
'==============
METHOD Sum() As Double
Dim i
Dim mysum as double ' Added
For i = 1 To THIS.dn
' METHOD += THIS.da(i)
mysum += THIS.da(i) ' Added
Next
METHOD = mysum ' Added
End METHOD
METHOD average() As Double
METHOD = THIS.Sum / THIS.dn
End METHOD
End methods
'====
'TEST
'====
Dim s As stat
s.da=>2,4,6,8,10,12,14,16,18
s.dn=9
Print `Sum: ` str(s.Sum) ` Average: ` str s.average
"

O2_BASIC src

'msgbox 0, o2_view "o2h "+src

If Len(O2_ERROR) Then
MsgBox 0, O2_ERROR : Stop
End If

O2_EXEC



I make changes, but I still get error.

I have TB 1.8.9.0.

Regards,
Bob

Charles Pegge
11-10-2011, 06:01
Hi Bob,

These examples need to be updated. Sorry about that.

There were 2 bugs due to old syntax, but this is how I would express it more clearly now.



'===================
'CLASSES AND METHODS
'===================
'

Uses "oxygen"

dim as string src = "

CLASS stat
da(100) As Double
dn As Long


METHOD Sum() As Double
Dim sys i
For i = 1 To dn
METHOD += da(i)
Next
End METHOD
'
METHOD average() As Double
METHOD = Sum() / dn
End METHOD
'
End CLASS

'====
'TEST
'====
'
Dim s As stat
s.da<=2,4,6,8,10,12,14,16,18
s.dn=9
Print "Sum: " str(s.Sum) " Average: " str (s.average)
"

O2_BASIC src

'msgbox 0, o2_view "o2h "+src

If Len(O2_ERROR) Then
MsgBox 0, O2_ERROR : Stop
End If

O2_EXEC


I will review the other examples as soon as possible.

Charles

kryton9
13-10-2011, 06:07
I should add that in oxygen you can use "return" in a function to return a value instead of "method" as Charles showed. I think return is better as it makes the code less confusing.


METHOD average() As Double
RETURN ( Sum() / dn )
End METHOD