Class ComValue

ComValue is a wwDotnetBridge .NET helper class that wraps problem .NET types that cannot be accessed in FoxPro due to COM Type Limitations. COM doesn't support many .NET types and this class allows you to use them with this wrapper object that can be passed and returned to and from the explicit invocation methods like InvokeMethod(), SetProperty(), GetProperty(), which know how to fix up the ComValue structure into real .NET types and vice versa.

This applies both to inbound processing as a method parameter or property set value, as well as on return values. For example, the Long type cannot be passed over COM. If a method requires a Long parameter, you can create a ComValue instance and use .SetLong(lnValue) to pass the value to InvokeMethod(). When the call returns and it also returns a Long, InvokeMethod() automatically returns a ComValue object from which you can retrieve the value .GetLong() (or .GetInt64()).

Important: ComValue fixup works only with the wwDotnetBridge intrinsic methods. It's important to understand that it's wwDotnetBridge that understands ComValue instances, not .NET so you can only pass or receive a ComValue through the indirect access methods, never by accessing the .NET COM instance directly.

Examples of unsupported types include:

  • Long, Single, Decimal number types
  • Guid, Byte, DbNull, char
  • Any Value type (based on struct in C#)
  • Enum Values
  • Access to any Generic Value or Type

That's a pretty wide swath of types that are inaccessible via direct instance COM access, but with the help of the ComValue class it's still possible to access these types in FoxPro.

How it works

ComValue works by creating a .NET wrapper object with a Value property that holds the actual .NET value and methods that allow setting and retrieving that value - or a translation thereof - in FoxPro. The Value is stored in .NET and is never passed directly to FoxPro. Instead you pass or receive a ComValue instance that contains the Value and has conversion routines that allow convert the value between .NET and FoxPro types.

Automatic ComValues

ComValue results are automatically returned for known problem types with:

  • GetProperty()
  • InvokeMethod() result values

For example, if you call a method that returns a Guid value, GetProperty() or InvokeMethod() return a ComValue object and you can call GetGuid() to retrieve the Guid as a string.

loGuid = loBridge.InvokeMethod(loObj,"GetGuid") 
* Get Guid from ComValue
lcGuid = loGuid.GetGuid()

You can pass ComValue objects when using these methods:

  • SetProperty()
  • InvokeMethod() parameters
  • CreateInstance() constructor parameters
  • ComArray.AddItem()

For these methods you create a ComValue instance and set the value and then pass that to one of the above methods.

lcGuid = GetAGuidStringFromSomewhere()
loGuid = loBridge.CreateValue()
loGuid.SetGuid(lcGuid)

llResult = loBridge.InvokeMethod(loObj,"SetGuid",loGuid)

Simple type conversion:

*** Create .NET Object instance
loNet = loBridge.CreateInstance("MyApp.MyNetObject")

*** Convert the 'unsupported' parameter type
LOCAL loVal as Westwind.WebConnection.ComValue
loVal = loBridge.CreateComValue()
loVal.SetInt16(11)

*** Call method that takes Int16 parameter
loBridge.InvokeMethod(loNet,"PassInt16",loVal)

ComValue caching for Method and Property Invocation

This class also supports setting a ComValue from properties and method results. This is useful if you have a method or property that uses a type inaccessible via COM (like strongly typed or subclassed dataset objects for example). In this case you can call the SetValueXXX methods to fill the ComValue structure and then use this ComValue in InvokeMethod, SetProperty calls which automatically pick up this ComValue object's underlying .NET type.

*** Create an array of parameters (ComArray instance)
loParms = loBridge.CreateArray("System.Object")
loParms.AddItem("Username")
loParms.AddItem("Password")
loParms.AddItem("Error Message")

*** Create a ComValue structure to hold the result: a DataSet
LOCAL loValue as Westwind.WebConnection.ComValue
loValue = loBridge.CreateComValue()

*** Invoke the method and store the result on the ComValue structure
*** Result from this method is DataSet which can't be marshalled properly over COM
? loValue.SetValueFromInvokeMethod(loService,"Login",loParms)

*** This is your raw DataSet
*? loValue.Value   && direct access won't work  because it won't marshal

*** Now call a method that requires the DataSet parameter
loBridge.InvokeMethod(loService,"AcceptDataSet",loValue)

The jist of this is that the DataSet result is never passed through FoxPro code, but is stored in ComValue and then that ComValue is used as a parameter in the InvokeMethod call. All indirect execution methods (InvokeMethod,SetProperty etc.) understand ComValue and use the Value property for the parameter provided.

System.Object
  Westwind.WebConnection.ComValue
public class ComValue : object

Class Members

MemberDescription

Constructor

GetGuid

o.ComValue.GetGuid()

GetTypeName

o.ComValue.GetTypeName()

GetValue

Retrieves the fixed up value from the .Value property. The call is returned by calling .GetProperty() to retrieve the value which automatically converts problem types to the appropriate wrapper types (ie. Array/Collections to ComArrays, DbNulls to Nulls etc.).

o.ComValue.GetValue()

NewGuid

Creates a new GUID and stores it in the Value and also returns the new Guid as a string.

o.NewGuid()

SetByte

Returns a byte value which is similar to Int16.

public void SetByte(object value);

SetChar

Converts a FoxPro string or number to a .NET char value on the Value structure.

o.ComValue.SetChar(object val)

SetDbNull

Sets a DbNull value from FoxPro. DbNull is a value type and can't be accessed directly in FoxPro.

o.ComValue.SetDbNull()

SetDecimal

Sets a Decimal value. Turns any FoxPro numeric value to a .NET decimal value.

This can actually be done in FoxPro explicitly with CAST(val as Currency) but this method can be used with pre-VFP 9 and is used internally to convert numerics to decimal.

public void SetDecimal(object val);

SetEnum

Allows you to create an Enum value that can be passed as a parameter.

o.ComValue.SetEnum(string enumString)

SetGuid

o.ComValue.SetGuid(object value)

SetInt16

Sets a Short value which is not supported in Visual FoxPro

public void SetInt16(object val);

SetInt64

Sets a Int64 value from a FoxPro numeric value. Since FoxPro doesn't have a cast to 64 bit values natively you can use decimal or float values in Fox or 32 bit integers.

public void SetInt64(object val);

SetLong

Turns a FoxPro number to a Long 64 bit integer value in .NET. Note that FoxPro doesn't support casting to a 64 bit value, so you can pass a 32 bit integer or numbers that are decimal or floats to this function to achieve 64 bit Long values.

public void SetLong(object val);

SetSingle

Sets the value of this object to a Single based on a FoxPro number passed in.

o.ComValue.SetSingle(lnValue)

SetSingle

Sets the value of this object to a .NET Single/float based on a FoxPro number passed in.

o.SetFloat(lnValue)

SetUInt32

Sets a 32 bit UInt value on the Value property.

o.ComValue.SetUInt34(object val)

SetUInt64

Sets a 64 bit unsigned integer value from a FoxPro number.

o.ComValue.SetUInt64(object val)

SetValueFromCreateGenericInstance

This method creates an instance of a generic type on the ComValue structure by allowing you to specify a generic type name, the generic type parameters (as a ComArray) and optional constructor parameters (as a ComArray.)

o.ComValue.SetValueFromCreateGenericInstance(lcGenericType,loTypes,loParms)

SetValueFromCreateInstance

Allows setting of the Value property from the result of an object instantiation.

Useful when instantiating types that can't be accessed in VFP such as Generic types or Value types. Using this mechanism you can create an instance of the type.

o.ComValue.SetValueFromCreateInstance(string typeName,ComArray parms)

SetValueFromEnum

o.ComValue.SetValueFromEnum(string enumType, string enumKeyName)

SetValueFromInvokeMethod

Sets the Value property from a method call that passes it's positional arguments as an array.

Note that the array of parameters must be passed in as a COM Array.

public void SetValueFromInvokeMethod(object objectRef,     string method,     ComArray parms);

SetValueFromInvokeStaticMethod

Sets the Value property from a method call that passes it's positional arguments as an array.

Note that the array of parameters must be passed in as a COM Array.

public void SetValueFromInvokeMethod(string typename,     string method,     ComArray parms);

SetValueFromProperty

Sets the Value property from a property retrieved from .NET Useful to transfer value in .NET that are marshalled incorrectly in FoxPro such as Enum values (that are marshalled as numbers).

public void SetValueFromProperty(object objectRef,     string property);

SetValueFromStaticProperty

Sets the value property from a static property retrieved from .NET. Useful to transfer value in .NET that are marshalled incorrectly in FoxPro such as Enum values (that are marshalled as numbers)

public void SetValueFromStaticProperty(string typeName,     string property);

SetValueFromSystemConvert

o.ComValue.SetValueFromSystemConvert(string method, object value)

ToString

Returns the value of contained ComValue instance value as a string.

Same as loComValue.Value.ToString() but doesn't require the implicit InvokeMethod() call.

o.ComValue.ToString()

Value

Internally this value is set by the various SetXXX methods. It's of type objcect but set to the appropriate .NET subtype.

Requirements

Namespace: Westwind.WebConnection
Assembly: wwdotnetbridge.dll

© West Wind Technologies, 2004-2020 • Updated: 06/14/20
Comment or report problem with topic