View previous topic :: View next topic |
Author |
Message |
snipsnip
Joined: 10 Feb 2014 Posts: 74 Location: Melbourne
|
Posted: Tue Feb 24, 2015 9:56 pm Post subject: passing string or value to funtion. |
|
|
Hi all,
I've got a function that I want to accept either a string or value passed to it.
Question is there an easy way to figure out what data type a variable is?
I'm thinking of using overlay and checking for null terminators to identify strings.
I'd rather not though - if there's an easier way.
Cheers
(BASCOM-AVR version : 2.0.7.8 ) |
|
Back to top |
|
|
AdrianJ
Joined: 16 Jan 2006 Posts: 2483 Location: Queensland
|
Posted: Wed Feb 25, 2015 12:05 am Post subject: |
|
|
Checking for a null terminator will often fail. Consider that an integer of value say 65 decimal will be stored in memory as 2 bytes hex 41 00. So it will look exactly like a string of value "A", with its null terminator.
Bascom does not have the VB variable type "Any", or "Variant" for good reason.
Maybe the best you can do is define a parameter as string, and if you want to pass a number, convert it to string first. Seems a waste, since it costs time to do the conversions.
What exactly are you trying to achieve ? _________________ Adrian Jansen
Computer language is a framework for creativity |
|
Back to top |
|
|
snipsnip
Joined: 10 Feb 2014 Posts: 74 Location: Melbourne
|
Posted: Wed Feb 25, 2015 2:01 am Post subject: |
|
|
Hi Adrian,
Thanks for the reply - I'm just trying to offload the "string to val" and "val to string" conversions to
the called functions rather than the main program loop.
Not a big deal, just would be easier - my brain gets muddled trying to keep track of data types
How does the Bascom "time and date" functions do it? they can accept either.
Cheers |
|
Back to top |
|
|
AdrianJ
Joined: 16 Jan 2006 Posts: 2483 Location: Queensland
|
Posted: Wed Feb 25, 2015 3:19 am Post subject: |
|
|
Never looked to see how the Date/Time functions work in the library, but I would guess they parse the input first, looking for the separators ":" or "/" or "-", then figure out what to do with them, or if not present, assume a numeric. Easier when you have a relatively fixed format to look for.
Personally I prefer the relatively 'strong' data typing that Bascom has. At least then you are not guessing what the type is, or how it might be converted. But I come from an ASM background, where its a lot simpler.
My pet hate is that you can do things like Print A in Basic ( and including Bascom ) when you dont know what type of variable A is. Can lead to lots of confusion about the difference between the internal storage and external representation of a value as a string. _________________ Adrian Jansen
Computer language is a framework for creativity |
|
Back to top |
|
|
|