codemorphis.com Forum Index codemorphis.com
Software development: pure and simple.
 
 FAQFAQ   SearchSearch   MemberlistMemberlist   UsergroupsUsergroups   RegisterRegister 
 ProfileProfile   Log in to check your private messagesLog in to check your private messages   Log inLog in 

datatype conversion

 
This forum is locked: you cannot post, reply to, or edit topics.   This topic is locked: you cannot edit posts or make replies.    codemorphis.com Forum Index -> Visual Programming Forum
View previous topic :: View next topic  
Author Message
CodeMorphis



Joined: 02 Dec 2003
Posts: 252

PostPosted: Wed May 25, 2005 10:11 pm    Post subject: Reply with quote

That is an excellent question. Let’s examine how data types are handled in Synopsis.

As you have observed, the “Get UI Component Text” component, which you might be using to query user input on a form, returns the data as a string. This is totally expected since a user input field such as a TextBox, for example, can contain any arbitrary data: it could be a number but it could also be a non-numeric textual string. But your question is: what if I want to interpret and treat the data as an integer or numeric value?

The solution for this is very simple as it involves simply setting a single component property. Let’s look at some program examples to see how it is done.

Consider the following program which you can download here:
http://www.codemorphis.com/articles/ans05/ans_052505/adding_ints.vpd



In this program, we are pushing data from a “Set Variable” component into the “Add item(s)” service of the Array component. After that we call the “Get Item” service to retrieve the data that we just added to the Array and pass it to the Add component. The result of the addition goes into the Console Output component for echoing to the console output area.

Here is the input for the “Set Variable” component:


We are therefore passing an integer value of 100 to the Array. The Array accepts data in whatever format we give it and preserves the incoming data type. So the Array will “remember” that the input of 100 is an integer and will return that data as an integer when requested by the “Get item” service call that follows. If you run the program and then double click on the output data port of the “Get item” service call you will see that the data type is integer.

Now, here are the input arguments for the Add call:


So what we have instructed the Add component to do is to take data through input data port 0 and add a value of 1 to it.

If you run the program, then you see the expected value of 101 in the console output area. Double click the output data port on the Add component and you will see that the resulting data type of the add operation was an integer: since we added two integers together we should get an integer value as a result.

Now, let’s see how the behaviour of our program can be completely change with just a minute modification.

Consider the modified program:
http://www.codemorphis.com/articles/ans05/ans_052505/adding_strs.vpd

It is identical to the first program with just one change: we have changed the data type of the “Set Variable” argument to a string:



Now what happens when we run the program? The Array component will preserve this data type and store a string with the value “100” (not a numeric value but a textual string). Likewise, the “Get item” service call will respect this data type and return a string type. If you run the program and then double click on the output data port of the “Get item” service call then you will see that this time, the retrieved data type is a string.

The string value then goes into the Add component. But wait! Input data port 1 of the Add component is an integer. What does it mean to add a string and an integer? When the input data types are different, the Add component uses data type promotion to resolve this. Looking at the Help documentation for the Add component, we see that:


  • An integer and a floating point combination will produce a floating point result
  • An integer and string combination will produce a string result
  • A floating point and string combination will produce a string result


So the Add component will first convert the integer input of 1 (from input data port 1) to the string “1” and then concatenate the two strings together to get a resulting string of “1001”, which is what you will see in the console output area if you run the program.

In our programs we had control of the input data type for the input value of “101”, i.e. in the first program we made it an integer and in the second program we made it a string. But what if the input supplied to the Array component is a string and we want to add integers?

We’ll make one tiny modification to the second program to accomplish this:
http://www.codemorphis.com/articles/ans05/ans_052505/adding_fixed_ints.vpd

The difference between this program and the second one is that we have modified a property of the Add component:


We have changed the value of the “Fixed argument types” property from the default value of FALSE to TRUE. When this property is FALSE, the incoming data type will set the data type of the input data port. That is why the string value coming from the “Get item” service caused the Add component to receive a string value even though it originally was an integer. By setting the property to TRUE, we are fixing the current data type. This means that the incoming data will now be converted to whatever data type is currently set in the input data port. So if we set this property and then set the type of input data port 0 on the Add component to integer, we can be sure that no matter what the source of input, the Add component will only add integer values and hence, will produce an integer value.

If you run this program, you will see the expected result of 101 in the console output area. Double click the output data port on the “Get item” service call and you will see that the data stored in the Array is still a string but it gets converted to an integer in the Add component. Double click the output data port on the Add component and you will see that the resultant type is integer.

Some Synopsis components have the “Fixed argument types” property if it makes sense to accept various data types but there is also a need to sometimes force input data to a particular type.

Some Synopsis components, such as the Exponential component, only accept one data type and it does not make sense to have this property:


Here we see that the Exponential component only accepts the float type. This makes sense since it is a purely numerical computation that deals with floating point numbers.

Now, our third program used the “Fixed argument type” property to convert the string data stored in the Array to integer type. What if we want to store only integers in the Array? We can use the Variable component to do this:



We set the “Fixed argument types” property on the Variable component to TRUE. Now the data coming from the “Get UI Component Text” component is converted to an integer before going into the Array. To accomplish this task we could have used the “Set Variable” component as well, however, since the Variable component is a data component and has no process ports, the data passes right through it with no need for process control, thereby making it (marginally, although not noticeably) more efficient.

One could ask why the Array component does not simply offer a “Fixed argument types” property as well so that we could do away with the Variable component. The answer has to do with implementation as the Array component offers many different services, for some of which, the property makes no sense. This may likely change in the next major version of Synopsis, with more components possessing the property; in the on-going bid to simplify our programming lives.
Back to top
View user's profile Send private message Send e-mail Visit poster's website
CodeMorphis



Joined: 02 Dec 2003
Posts: 252

PostPosted: Thu May 26, 2005 7:26 am    Post subject: Reply with quote

Probably, a programmable data filter that could be attached to any UI component would be more flexible. We will try to incorporate this in the future design.

Thanks for your post and feedback.
Back to top
View user's profile Send private message Send e-mail Visit poster's website
Display posts from previous:   
This forum is locked: you cannot post, reply to, or edit topics.   This topic is locked: you cannot edit posts or make replies.    codemorphis.com Forum Index -> Visual Programming Forum All times are GMT - 5 Hours
Page 1 of 1

 
Jump to:  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum


Powered by phpBB © 2001, 2005 phpBB Group