Show/Hide Toolbars

XSharp

The -vo2 option directs the compiler to initialize all variables and fields of type STRING (System.String) and all elements of DIM ... AS STRING arrays to an empty string (/vo2[+|-]

Syntax

-vo2[+ | -]

Arguments

+ | - Specifying +, or just -vo2, directs the compiler to initialize all variables and fields of type STRING and all elements of DIM ... AS STRING arrays to an empty string (String.Empty).

Remarks

This option is off by default, and all locals, globals, fields and array elements of type STRING have an initial value of NULL, which is the default initial value for any local, global, field or array element that contains a reference type.

 

Generally you will initialize a string variable to a specific value before it is used, and initializing it to an empty string would incur unnecessary overhead. In addition, it is inconsistent with the behavior of all other reference types, which have an initial value of NULL. However, this may break existing Visual Objects code that relies on Visual Object's behavior of initializing string variables to a string with zero characters.

 

When this option is not used, you may test for an empty string variable, field or array element by comparing it against NULL. When this option is enabled, you may test for an empty string variable, field or array element by comparing it against "" or String.Empty, or testing that its length equals zero. System.String.IsNullOrEmpty() may also be used to test whether a string variable contains NULL or a valid string with zero characters.

 

Also note that the predefined constant NULL_STRING is normally equivalent to NULL, but when -vo2 is used, NULL_STRING is equivalent to "" (a zero-length string).

 

Compatibility Note:

-vo2 does not initialize STRING fields in structures. Since structures do not have default constructors, structure fields cannot have initializer expressions. Although this is not a compatiblity issue since you cannot create structures (value types) in Visual Objects, it is something to keep in mind if you use structures in an application that uses -vo2.

 

Note that the use of the term "structure" here refers to STRUCTURE in X#, not STRUCTURE in Visual Objects, which has been renamed VOSTRUCT in X#. -vo2 has no effect on VOSTRUCT since you cannot declare fields that are reference types in a VOSTRUCT.

To set this compiler option in the Visual Studio development environment:

 

1.Open the project's Properties page.

2.Click the Dialect tab.

3.Change the value.

4.Click here to see the property page

Example

// using default or /vo2-
FUNCTION Start() AS VOID
  LOCAL s AS STRING
  ? s == NULL               // true
  ? s == ""                 // false
  ? s == String.Empty       // false
  ? Len(a)                 // runtime error - NullReferenceException
  ? a:Length               // runtime error - NullReferenceException
  ? String.IsNullOrEmpty(a) // true
  RETURN

// using /vo2 or /vo2+
FUNCTION Start() AS VOID
  LOCAL s AS STRING         // this compiles as 'LOCAL s := "" AS STRING'
  ? s == NULL               // false
  ? s == ""                 // true
  ? s == String.Empty       // true
  ? Len(a)                 // 0
  ? a:Length               // 0
  ? String.IsNullOrEmpty(a) // true
RETURN