Another class question - data not going into class

netchicken

Active Member
Licensed User
Longtime User
I am returning data from weatherunderground.com. I want to pass it to a weather class and get back the changed values (primarily for a practice exercise). However for the life of me I can't get the data into the class.

My data comes into my program as a map and works OK

B4X:
 textstuff = m.Get("temp_c") & " " & m.Get("wind_string")

Happily textstuff returns the temp.

However if I pass it to the Weather class in the same sub nothing happens.
B4X:
Dim myweather As weather

myweather.Initialize( m.Get("temp_c"), m.Get("relative_humidity"), m.Get("wind_dir"), m.Get("wind_kph") , m.Get("windchill_c") , m.Get("feelslike_c") , m.Get("precip_today_string") ,m.Get("icon_url"))

lblweather.text  = myweather.temp_c &" " & myweather.wind_dir

The weather Class

B4X:
Sub Class_Globals
   'internal _private variables
   Private m_temp_C As String
   Private m_relative_humidity As String
   Private m_wind_dir As String
   Private m_wind_kph As String
   Private m_windchill_c As String
   Private m_feelslike_c As String
   Private m_precip_today_string As String
   Private m_icon_url As String

Private m_tempold As String
End Sub

'Initializes the object. You can add parameters to this method if needed. 'data goes in through temp_C and relative_humidity to _temp_C and _relative_humidity

Sub Initialize(temp_c As String,relative_humidity As String, wind_dir As String,wind_kph As String,windchill_c As String,feelslike_c As String,precip_today_string As String,icon_url As String)

m_temp_C = temp_c
Log("1 " & m_temp_C)
m_relative_humidity = relative_humidity
m_wind_dir = wind_dir
m_wind_kph = wind_kph
m_windchill_c = windchill_c
m_feelslike_c = feelslike_c
m_precip_today_string = precip_today_string
m_icon_url = icon_url

m_tempold = temp_c
End Sub

Sub temp_c
Dim temp As String
temp = m_temp_C &" C"
Return temp
End Sub

Sub tempchange
'nothing
End Sub

Sub relative_humidity
Return m_relative_humidity
End Sub

Sub wind_dir
Return m_wind_dir
End Sub

Sub wind_kph
Return m_wind_kph
End Sub

Sub windchill_c
Return m_windchill_c
End Sub

Sub feelslike_c
Return m_feelslike_c
End Sub

Sub precip_today_string
Return m_precip_today_string
End Sub
Sub icon_url
Return m_icon_url
End Sub

It all looks OK for me I can't see what is missing. It like something isn't connected. it doesn't give any errors. Just sits there .. mute ....
The last class I tried to make had the same problem.

It seems pretty straightforward.
 

Attachments

  • weather_class.zip
    47.4 KB · Views: 207
Last edited:

Ricky D

Well-Known Member
Licensed User
Longtime User
I'm running it through debug

B4X:
Sub Initialize(temp_c As String,relative_humidity As String, wind_dir As String,wind_kph As String,windchill_c As String,feelslike_c As String,precip_today_string As String,icon_url As String)

m_temp_C = temp_c

is jumping to the temp_c sub. Is it supposed to do that?

regards, Ricky
 
Upvote 0

netchicken

Active Member
Licensed User
Longtime User
I Hope so :)
I don't get any errors.

The temp_c sub is being called from this line back in the MAIN module myweather.temp_c with the class seen in this line below.

lblingredients.text = myweather.temp_c & " " & myweather.wind_dir


It does seem to be working, as it gets Log2 then Log3. in teh Sub temp_c.

But it gets Log1 last which is in the initialise sub. i would think that would work first with the data coming in through the myweather.Initialize in Main, to the Initialize sub in the class, passing the values through the code.
 
Upvote 0

Ricky D

Well-Known Member
Licensed User
Longtime User
m_temp_C will not be initialized to temp_c because the sub doesn't return anything

I'm not sure what the logic should be

regards, Ricky
 
Upvote 0

netchicken

Active Member
Licensed User
Longtime User
I agree the data doesn't seem to be going into the class.

I could be wrong it I see it like this.

Make a new instance of the class, and pass the data in to it in the Main Module

B4X:
Dim myweather As weather
myweather.Initialize( m.Get("temp_c"), m.Get("relative_humidity"), m.Get("wind_dir"), m.Get("wind_kph") , m.Get("windchill_c") , m.Get("feelslike_c") , m.Get("precip_today_string") ,m.Get("icon_url"))

Data goes into the class through Sub Initialize and then is passed to the private variables M_temp_C etc.

B4X:
Sub Initialize(temp_c As String,relative_humidity As String, wind_dir As String,wind_kph As String,windchill_c As String,feelslike_c As String,precip_today_string As String,icon_url As String)

m_temp_C = temp_c
Log("1 " & m_temp_C)
m_relative_humidity = relative_humidity
m_wind_dir = wind_dir
m_wind_kph = wind_kph
m_windchill_c = windchill_c
m_feelslike_c = feelslike_c
m_precip_today_string = precip_today_string
m_icon_url = icon_url
End Sub

The private variables get stuff done to them in the class subs
B4X:
Sub temp_c
Dim temp As String
Log("2" & m_temp_C)
temp = m_temp_C &" C "
Log("3 " & temp)
Return temp
End Sub

And you call the class sub back in the main with myweather.temp_c which should hold the value.

At least, thats my idea :)
 
Last edited:
Upvote 0

Ricky D

Well-Known Member
Licensed User
Longtime User
why don't you do this then :

B4X:
Sub Initialize(temp_c As String,relative_humidity As String, wind_dir As String,wind_kph As String,windchill_c As String,feelslike_c As String,precip_today_string As String,icon_url As String)

m_temp_C = temp_c

Log("1 " & m_temp_C)
m_relative_humidity = relative_humidity
m_wind_dir = wind_dir
m_wind_kph = wind_kph
m_windchill_c = windchill_c
m_feelslike_c = feelslike_c
m_precip_today_string = precip_today_string
m_icon_url = icon_url

m_tempold = temp_c
End Sub

'then have this

Public Sub getTemp_C As String
    Dim temp As String
    Log("2" & m_temp_C)
'Select m_temp_C
'Case <0
    temp = m_temp_C &" C - cold"
'Case <10
'm_temp_C = m_temp_C &"C - not so cold"
'Case Else
'm_temp_C = m_temp_C &"C - Go Running!"
'End Select

    Log("3 " & temp)

    Return temp
End Sub

I think your code isn't working because you have the sub name same as the argument to your Initialize sub and also the sub doesn't return anything the way it's defined

B4X:
Sub temp_c

would have to be

B4X:
Sub temp_c As String

But I do highly recommend not to have internal subs the same name as incoming parameters as unexpected results will happen. Your internal m_temp_C will never be stored internally because you are calling the sub that references it.

Actually your code will never work as you want it to if the sub name is the same as the parameter name in Initialize.

regards, Ricky
 
Upvote 0

netchicken

Active Member
Licensed User
Longtime User
Yes!!!!

Thanks, you were right, all I had to go was rename my subs getTemp_C etc as you suggested. It works now!

Thanks for that, now I can move on with it :)
 
Upvote 0

raytronixsystems

Active Member
Licensed User
Longtime User
You had declared the variables inside your class as "Private". These can only be accessed by subroutines inside of your class module and nowhere else. Declare a set of get/set Public Subroutines inside your class to retrieve or set values inside of your class from the outside. This works very well and makes all data accessible from the outside.


example:

B4X:
' class TestClass

Class module
Sub Class_Globals

       Private TestVar As String  ' this variable is private to the class
       Public NUMBVARS As Int: NUMBVARS = 2 ' a constant
EndSub

'Initializes the object. You can add parameters to this method if needed.
Public Sub Initialize
   '
   ' put any initialization code in here:
          '
   'End of subroutine Initialize
   '
End Sub

' methods here accessible by components (modules) of your app..
'
Public SetTestVar(Data As String) ' this stores class data...
'
       TestVar = Data    ' set class data to value specified by caller
'
End Sub

Public GetTestVar As String ' this retrieves class data...
'
       return TestVar      ' return class data to caller
'
End Sub
' -----------------------------------------------------------------------
' code in Activity.Main....
'
' using the Class in another module as follows...
Sub TestSub
      Dim tc AS TestClass
      Dim MyVar As String
      Dim MyConstant As int
      '
      tc.Initialize              ' must be done before any method called!
                                   ' apparently this sets all data in the class to zero 
                                   ' except the constant I declared in the class.
                                   ' not sure whether it's because the constant is  
                                   ' Public or preinitialized. I'm looking into this.
      '
      tc.SetTestVar("This is a test")   ' set data inside my class
      '
      MyVar = tc.GetTestVar               ' retrieve data from my class

       '
       ' Access a constant in the class directly since it is Public
       '
       MyConstant = tc.NUMBVARS          ' retrieve a Public class constant
       '
End Sub

I hope this helps you out. I have not tried this code in the IDE but just put it here based upon memory. I might have a few typo's but looks fine at first glance here.

Regards,
Ray
 
Upvote 0
Top