Getting property and parameter values

Use one of the overloaded GetValue methods to get parameter and property values from the IDO protocol classes. These examples show how to do this using the LoadCollectionResponseData IDO protocol class.

GetValue with LoadCollectionResponseData

In this example, the desired native .NET data type is specified as an additional parameter in the call to GetValue (using the .NET Framework's generics feature).

LoadCollectionResponseData loadResponse; 
long userId = 0; 
string userName = null; 
DateTime recordDate; 
Guid rowPointer; 

loadResponse = this.Context.Commands.LoadCollection("UserNames", "UserId, UserName, RecordDate, RowPointer", "", "", 0); 
userId = loadResponse[0, "UserId"].GetValue<long>(); 
userName = loadResponse[0, "UserName"].GetValue<string>(); 
recordDate = loadResponse[0, "RecordDate"].GetValue<DateTime>(); 
rowPointer = loadResponse[0, "RowPointer"].GetValue<Guid>(); 

Handling nullable values

Be aware that, if any of the values are null, this version of the GetValue method throws an exception. When working with nullable values, you have three options to avoid throwing an exception:

  • Check the IsNull property before calling GetValue.
  • Call the overloaded GetValue method with a default value to be returned in case of null.
  • Use .NET nullable value types.

Those three techniques are illustrated in this example:

LoadCollectionResponseData loadResponse; 
Nullable<long> userId; 

loadResponse = this.Context.Commands.LoadCollection("UserNames", "UserId, UserName, RecordDate, RowPointer", "", "", 0); 

// Check for null before calling GetValue: 
if (!loadResponse[0, "UserId"].IsNull) { 
      userId = loadResponse[0, "UserId"].GetValue<long>(); 
} else { 
      userId = -1; 
} 

// Call GetValue, return -1 if null: 
userId = loadResponse[0, "UserId"].GetValue<long>(-1); 

// Call GetValue, check for null return value: 
userId = loadResponse[0, "UserId"].GetNullableValue<long>(); 

if (!userId.HasValue) 
      userId = -1;