Sybase, Inc. Navigational Bar

Powersoft Tools

Tip #3 - Converting between commonly used types

When dealing with the various commonly used Power++ types such as WString or WLong, there often arise situations in which there is a need to convert from one type to another. For example problems can arise when trying to pass a WString to a method that accepts a WLong. The following is meant to answer most common issues when dealing with various classes, and perhaps give the adept programmer an idea or two.

  1. Converting with Constructors

    Consider the code snippit at right. In the last line of code, we are passing a WChar * object to MyMethod,which accepts a WString. This is not an illegal operation, contrary to first impressions, because WString has a constructor defined which accepts a WChar * type; this constructor is documented in the Component Library Reference.

    When C++ runs into this type of situation, it automatically looks for a constructor which accepts only one parameter, and if found will invoke it using the source object as the argument, which in this case is a WChar *. This is important because it makes the assumption that all one-parameter constructors are conversion constructors. This requires caution when creating a one-parameter constructor which is not intended to be used as a conversion constructor.

  2. Converting with Methods

    When a constructor is not available, Power++ classes quite often provide methods to achieve the required conversion. For example, by using the ConvertToInt and ConvertToLong methods for WString, a WString object may be converted into either a WInt or WLong, respectively. To find the methods provided by an Power++ class, use the online Component Library Reference, and search for the class name. Listed are all of the methods provided by the class, in addition to a description and all of its constructors, properties and operators.



Click here to see Tip#4

Return to Power++ Home Page.


to top of page

Copyright © 1999 Sybase, Inc. All Rights Reserved.
Privacy Policy
Legal