VariantStrings (C++)

From Appmethod Code Examples
Jump to: navigation, search

Description

This example demonstrates the use of the VarToStr, VarToStrDef, VarToWideStr, and VarToWideStrDef methods defined in the Variants unit. VarToStr is equivalent to VarToWideStr and VarToStrDef is equivalent to VarToWideStrDef.

Code

int _tmain(int argc, _TCHAR* argv[])
{
   /* Assign a nonstring value to the variant.*/
   Variant var(10.6);

   /* Print the variant's value and catch the exception. If the variant cannot be
	  converted to a string, print another message. */
   try
   {
	 printf("%ls", VarToStr(var));
   }
   catch(int e)
   {
	 printf("empty string\n");
   }
   /* Use VarToStrDef instead. This method ensures that if LVar is not convertible
	  to string, a default value is used. */
   printf("%ls", VarToStrDef(var, "empty string"));

   /* Print the variant's value and catch the exception. If the variant cannot be
	  converted to a wide string, print another message. */
   try
   {
	 printf("%ls", VarToWideStr(var));
   }
   catch(int e)
   {
	 printf("empty string\n");
   };
   /* Use VarToWideStrDef instead. This method ensures that if LVar is not
	  convertible to wide string, a default value is used. */
	printf("%ls\n", VarToWideStrDef(var, "empty string"));

	getch();
}

Uses