Passing structures of structures into functions/subs

Discussion about the ZBasic language including the System Library. If you're not sure where to post your message, do it here. However, do not make test posts here; that's the purpose of the Sandbox.
Post Reply
ndudman
Posts: 79
Joined: 25 December 2008, 14:00 PM

Passing structures of structures into functions/subs

Post by ndudman »

Hi

In the last 2 lines of the following code, I dont understand what Im doing wrong ? The commented out line (last line) creates the error message which is commented above the it. If I change it to the code above then its fine, but I didnt want to define extra variables and write the extra code :( if I dont have to.

Code: Select all

public function enforceLeadingZero(byval i as Byte) as String
	if &#40;i < 10&#41; then
		enforceLeadingZero = "0" & i
	else 
		enforceLeadingZero = CStr&#40;i&#41;
	end if
end function

public function myTimeToString&#40;byVal t as MyTime, byval showSeconds as Boolean = false&#41; as String
	Dim h as String
	Dim m as String
	Dim s as String
	h = enforceLeadingZero&#40;t.hour&#41;
	m = enforceLeadingZero&#40;t.minute&#41;
	myTimeToString = h & "&#58;" & m 
	if &#40;showSeconds&#41; then
		s = enforceLeadingZero&#40;t.seconds&#41;
		myTimeToString = myTimeToString & "&#58;" & s
	end if	
end function
public function myDateToString&#40;byVal da as MyDate&#41; as String
	Dim y as String
	Dim m as String
	Dim d as String
	y = enforceLeadingZero&#40;CByte&#40;da.year&#41;&#41;
	m = enforceLeadingZero&#40;da.month&#41;
	d = enforceLeadingZero&#40;da.day&#41;
	myDateToString = d & "/" & m & "/" & y 	
end function

public function timeStampToString&#40;byval ts as MyTimeStamp&#41; as String	
	Dim tt as Mytime = ts.ttime
        Dim td as MyDate = ts.tdate
	timeStampToString = myDateToString&#40;td&#41; & " " & myTimeToString&#40;tt&#41;
        ' The following line gives compiler error 
        ' Error&#58; function "myDateToString", type conflict parameter 1, 
        ' formal&#58;Struct MyDate, actual&#58;Structure
        ' timeStampToString = myDateToString&#40;ts.tdate&#41; & " " & myTimeToString&#40;ts.ttime&#41;
end function
This must be a syntax thing or something Im missing
Neil
mikep
Posts: 796
Joined: 24 September 2005, 15:54 PM

Re: Passing structures of structures into functions/subs

Post by mikep »

Can you please post the definitions of MyTimeStamp, MyDate and MyTime as well.
Mike Perks
dkinzer
Site Admin
Posts: 3120
Joined: 03 September 2005, 13:53 PM
Location: Portland, OR

Re: Passing structures of structures into functions/subs

Post by dkinzer »

ndudman wrote:In the last 2 lines of the following code, I dont understand what Im doing wrong ?
The syntax is correct. There is a problem with the way that the compiler processes that case. We'll need to investigate further.
- Don Kinzer
dkinzer
Site Admin
Posts: 3120
Joined: 03 September 2005, 13:53 PM
Location: Portland, OR

Re: Passing structures of structures into functions/subs

Post by dkinzer »

mikep wrote:Can you please post the definitions of MyTimeStamp, MyDate and MyTime as well.
They were in an earlier post.
- Don Kinzer
ndudman
Posts: 79
Joined: 25 December 2008, 14:00 PM

Post by ndudman »

Hi

Ive included all the code this time... its basicly the same as the start of this thread, but with a Main() added.... Although the part of code which wouldnt compile now does compile...the behaviour of the two versions (See comment in last sub of code) does not operate the same... one version displays updated time every second or so, and the second only displays the first time and dosnt increment at all...

Thanks
Neil

p.s I just confirmed I do use Version 2.6.12 of the ZBasic.exe

Code: Select all

public Structure MyDate
      Public year as Integer
      Public month as Byte
      Public day as Byte
End Structure
public Structure MyTime
      Public hour as Byte
      Public minute as Byte
      Public seconds as Byte
End Structure
public Structure MyTimeStamp
	  Public dtInSec as UnsignedLong		' the time in seconds, store if calculated
											' otherwise 0
      Public tdate as MyDate
      Public ttime as MyTime
      Private isCurrent as Boolean			' Dont remember what this was for ?
End Structure

public function enforceLeadingZero&#40;byval i as Byte&#41; as String
   if &#40;i < 10&#41; then
      enforceLeadingZero = "0" & i
   else
      enforceLeadingZero = CStr&#40;i&#41;
   end if
end function

public function myTimeToString&#40;byVal t as MyTime, byval showSeconds as Boolean = false&#41; as String
   Dim h as String
   Dim m as String
   Dim s as String
   h = enforceLeadingZero&#40;t.hour&#41;
   m = enforceLeadingZero&#40;t.minute&#41;
   myTimeToString = h & "&#58;" & m
   if &#40;showSeconds&#41; then
      s = enforceLeadingZero&#40;t.seconds&#41;
      myTimeToString = myTimeToString & "&#58;" & s
   end if   
end function
public function myDateToString&#40;byVal da as MyDate&#41; as String
   Dim y as String
   Dim m as String
   Dim d as String
   y = enforceLeadingZero&#40;CByte&#40;da.year&#41;&#41;
   m = enforceLeadingZero&#40;da.month&#41;
   d = enforceLeadingZero&#40;da.day&#41;
   myDateToString = d & "/" & m & "/" & y    
end function

public function timeStampToString&#40;byval ts as MyTimeStamp, byval showSeconds as Boolean = false&#41; as String   
   Dim tt as Mytime = ts.ttime
        Dim td as MyDate = ts.tdate
   timeStampToString = myDateToString&#40;td&#41; & " " & myTimeToString&#40;tt, showSeconds&#41;
        ' The following line did give a compile error, however with new version it compiles
		'~ However when above 3 lines are commented and this line is UN commented the operation of 
		'~ Program isnt as I would expect... i.e in the Main below the time displayed dosnt change
        timeStampToString = myDateToString&#40;ts.tdate&#41; & " " & myTimeToString&#40;ts.ttime&#41;
end function

'~ -------------------------------------------------------------------
'~ Retrieves the time from zx system
'~ -------------------------------------------------------------------
public sub myGetTimeStamp&#40;ByRef ts as MyTimeStamp&#41;
	'~ Dim year as UnsignedInteger 
	Dim seconds as Single 
	'~ Dim ts as MyTimeStamp
	Call GetTimeStamp&#40;ts.tdate.year, ts.tdate.month, ts.tdate.day, _
					  ts.ttime.hour, ts.ttime.minute, seconds&#41;
	'~ ts.tdate.year = year&#41;
	ts.ttime.seconds = CByte&#40;seconds&#41;
	'~ myGetTimeStamp = ts
end sub	

sub Main&#40;&#41;
	Dim ts as MyTimeStamp
	Do
		myGetTimeStamp&#40;ts&#41;
		Debug.print timeStampToString&#40;ts, true&#41;
		delay&#40;1.0&#41;
	loop
end sub
dkinzer
Site Admin
Posts: 3120
Joined: 03 September 2005, 13:53 PM
Location: Portland, OR

Post by dkinzer »

ndudman wrote:the second only displays the first time and dosnt increment at all...
I believe that it does so because you omitted the showSeconds parameter value. If you change the code as shown below, it seems to work properly.

Code: Select all

timeStampToString = myDateToString&#40;ts.tdate&#41; & " " & myTimeToString&#40;ts.ttime, showSeconds&#41;
- Don Kinzer
dkinzer
Site Admin
Posts: 3120
Joined: 03 September 2005, 13:53 PM
Location: Portland, OR

Post by dkinzer »

Unrelated to the issue you raised in your most recent post, the conversion from year to string in myDateToString() probably won't produce the effect that you want. Instead, I propose this:

Code: Select all

y = enforceLeadingZero&#40;CByte&#40;da.year Mod 100&#41;&#41;
- Don Kinzer
ndudman
Posts: 79
Joined: 25 December 2008, 14:00 PM

Post by ndudman »

Hi Don

Im really sorry about that... I feel such an idiot :)...

thanks for the other suggestion about the year.

Neil
Post Reply