Euclidean algorithm not working.

alexwekell

Member
Licensed User
Longtime User
B4X:
#Region  Project Attributes 
   #ApplicationLabel: Radians/Degree Calculator
   #VersionCode: 1
   #VersionName: 
   'SupportedOrientations possible values: unspecified, landscape or portrait.
   #SupportedOrientations: unspecified
   #CanInstallToExternalStorage: False
#End Region

#Region  Activity Attributes 
   #FullScreen: False
   #IncludeTitle: True
#End Region

Sub Process_Globals
   'These global variables will be declared once when the application starts.
   'These variables can be accessed from all modules.


End Sub

Sub Globals
   'These global variables will be redeclared each time the activity is created.
   'These variables can only be accessed from this module.
Dim radresultnum As Label
Dim radresultden As Label
Dim radval As Int 
Dim label5 As Label
Dim label4 As Label
Dim radbox As EditText
Dim radbox2 As EditText
Dim degvalinput As EditText
   Dim WebView1 As WebView
End Sub


Sub Activity_Create(FirstTime As Boolean)
   'Do not forget to load the layout file created with the visual designer. For example:
   'Activity.LoadLayout("Layout1")
Activity.LoadLayout("Layout1")
Activity.AddMenuItem("Email Developer","Email")
Activity.AddMenuItem("Share App Via Facebook","Share")
Activity.AddMenuItem("About","change")


End Sub

Sub email_click
Dim Message As Email
Message.To.Add("")

StartActivity(Message.GetIntent)

End Sub
Sub change_click
StartActivity(layout2)
End Sub
Sub share_click
Dim Intent1 As Intent
Intent1.Initialize2("http://www.facebook.com/sharer.php?u=http://www.google.com", 0)
StartActivity(Intent1)
End Sub

Sub Activity_Pause (UserClosed As Boolean)
End Sub


Sub Activity_Resume
End Sub

Sub Button1_Click
Try
Dim degressvalend As Float
degressvalend =  (radbox.Text*180)/radbox2.Text
label5.text = degressvalend & "°"
Catch 
Msgbox ("You forgot to enter a value or you've entered something that is not a number.","Error")
End Try
End Sub
Sub Button2_Click
Dim DEG As Float
DEG = degvalinput.Text
label4.Text = (DEG*0.01745) & " Radians"
   ' Get the initial denominator: 1 * (10 ^ decimal portion length)
        Dim denom As Int = 180

        ' Get the initial numerator: integer portion of the number
        Dim numer As Int = degvalinput.Text

        ' Use the Euclidean algorithm to find the gcd
        Dim a As Int = degvalinput.Text
        Dim b As Int = 180
        Dim t As Int = 0 ' t is a value holder
      

        ' Euclidean algorithm
       Do While b <> 0
            t = b
            b = a Mod b
            a = t
        Loop

        'Get whole part of the number
        Dim Whole As String

        ' Return our answer
      Dim numerfinal As Int
      Dim demonfinal As Int
        numerfinal = (numer / a)
      demonfinal = (denom / a)
      
Dim radresultnum As Label
Dim radresultden As Label
radresultnum.Text = numerfinal
radresultden.Text = demonfinal
      Dim label8 As Label
label8.Text = t
      

End Sub

My code is above and when I actually run the program the labels I get an error saying the object must first be initialized. :confused: (the process is under "Sub Button2_Click")
 
Last edited:

NJDude

Expert
Licensed User
Longtime User
What's in "Layout1"?, I'm asking because at the bottom of the code you are creating Labels but not adding them to the Activity, if you have those labels defined on your layout then you should NOT Dim them again.
 
Upvote 0

alexwekell

Member
Licensed User
Longtime User
Here is my layout, however is I don't dim the labels as labels, then it says undeclared variable.

Edit, woops, I see what you were talking about.
 

Attachments

  • My Bastact.JPG
    My Bastact.JPG
    35.3 KB · Views: 176
Upvote 0

stevel05

Expert
Licensed User
Longtime User
Your problems appear to be here:

B4X:
Dim radresultnum As Label
Dim radresultden As Label
radresultnum.Text = numerfinal
radresultden.Text = demonfinal
       Dim label8 As Label
label8.Text = t

You have redimmed 2 global variables and dimmed a new one Label8, which is not in your Global Variables.

If the labels are on your layout, you should only Dim them in Globals, If they are not on your layout, they should still only be dimmed in Globals, but un addition you need to add then to the Activity or Panel in code, something like:

B4X:
Activity.AddView(lblName,.....)


EDIT: Beaten too it.
 
Upvote 0

alexwekell

Member
Licensed User
Longtime User
I figured I wouldn't start a new thread for another simple question, and forgive my double post, but how do I keep values when the app is rotated? As it stands, when I rotate it it switches to the landscape variant all of the values inputted and results disappear.

B4X:
#Region  Project Attributes 
   #ApplicationLabel: Radians/Degree Calculator
   #VersionCode: 1
   #VersionName: 
   'SupportedOrientations possible values: unspecified, landscape or portrait.
   #SupportedOrientations: unspecified
   #CanInstallToExternalStorage: False
#End Region

#Region  Activity Attributes 
   #FullScreen: False
   #IncludeTitle: True
#End Region

Sub Process_Globals
   'These global variables will be declared once when the application starts.
   'These variables can be accessed from all modules.


End Sub

Sub Globals
   'These global variables will be redeclared each time the activity is created.
   'These variables can only be accessed from this module.
Dim radresultnum As Label
Dim radresultden As Label
Dim radval As Int 
Dim label5 As Label
Dim label4 As Label
Dim radbox As EditText
Dim radbox2 As EditText
Dim degvalinput As EditText
   Dim WebView1 As WebView
End Sub


Sub Activity_Create(FirstTime As Boolean)
   'Do not forget to load the layout file created with the visual designer. For example:
   'Activity.LoadLayout("Layout1")
Activity.LoadLayout("Layout1")
Activity.AddMenuItem("Email Developer","Email")
Activity.AddMenuItem("Share App Via Facebook","Share")
Activity.AddMenuItem("About","change")


End Sub

Sub email_click
Dim Message As Email
Message.To.Add("[email protected]")

StartActivity(Message.GetIntent)

End Sub
Sub change_click
StartActivity(layout2)
End Sub
Sub share_click
Dim Intent1 As Intent
Intent1.Initialize2("http://www.facebook.com/sharer.php?u=http://www.google.com", 0)
StartActivity(Intent1)
End Sub

Sub Activity_Pause (UserClosed As Boolean)
End Sub


Sub Activity_Resume
End Sub

Sub Button1_Click
Try
Dim degressvalend As Float
degressvalend =  (radbox.Text*180)/radbox2.Text
label5.text = degressvalend & "°"5
Catch 
Msgbox ("You forgot to enter a value or you've entered something that is not a number.","Error")
End Try
End Sub
Sub Button2_Click
Dim DEG As Float
DEG = degvalinput.Text
label4.Text = (DEG*0.01745) & " Radians"
   ' Get the initial denominator: 1 * (10 ^ decimal portion length)
        Dim denom As Int = 180

        ' Get the initial numerator: integer portion of the number
        Dim numer As Int = degvalinput.Text

        ' Use the Euclidean algorithm to find the gcd
        Dim a As Int = degvalinput.Text
        Dim b As Int = 180
        Dim t As Int = 0 ' t is a value holder
      

        ' Euclidean algorithm
       Do While b <> 0
            t = b
            b = a Mod b
            a = t
        Loop

        'Get whole part of the number
        Dim Whole As String

        ' Return our answer
      Dim numerfinal As Int
      Dim demonfinal As Int
        numerfinal = (numer / a)
      demonfinal = (denom / a)
      
radresultnum.Text = numerfinal
radresultden.Text = demonfinal
      

End Sub
 
Upvote 0

stevel05

Expert
Licensed User
Longtime User
Have a look for the Activities and Lifecycle tutorial. You need to copy the data to Process_Global variables in Activity_Pause and put them back in Activity_Resume. Or write them to a file if you want to persist through restarts. See the tutorial and Key/Value store for options.

-------------------
Sent via Tapatalk
 
Last edited:
Upvote 0
Top