StrataFrame Forum

Using Nullable in BO

http://forum.strataframe.net/Topic724.aspx

By Kevin Lingofelter - 3/24/2006

Hello All!

I am setting up some fields in the mapper to Use Nullable Generic. This setting works ok if the field is a numeric type, but if the field is varchar, the code the mapper generates is causing the build to fail. Here is the code for the field:

''' <summary>
''' Test
''' </summary>
''' <remarks></remarks>
<Browsable(False), _
BusinessFieldDisplayInEditor(), _
Description(
"Test"), _
DesignerSerializationVisibility(DesignerSerializationVisibility.Hidden)> _
Public Property [Test]() As Nullable(Of System.String)
  Get
    Dim loValue As Object
    loValue = Me.CurrentRow.Item("Test")
   
If loValue Is DBNull.Value Then
       
Return CType(Nothing, Nullable(Of System.String))
   
Else
       
Return CType(loValue, Nullable(Of System.String))
   
End If
 
End Get
 
Set(ByVal value As Nullable(Of System.String))
   
If value.HasValue Then
     
Me.CurrentRow.Item("Test") = value.Value
   
Else
     
Me.CurrentRow.Item("Test") = DBNull.Value
   
End If
 
End Set
End Property

The hilighted code is giving the error:

Error 1 Type argument 'String' does not satisfy the 'Structure' constraint for type parameter 'T' ...

Anyone know why this is? Thanks!

By Robert Linton - 3/24/2006

Hi Kevin,

Any value type, including struct can be nullable: Except reference types. Strings are reference types so you can configure your BO string types as nullable.

I'm new to SF...so this may not be entirely correct, but I've been using the "replace..." option and returning a " " instead.

Hope that helps,

Rob

By Robert Linton - 3/24/2006

Error in above post...

String are reference types so you can not assign them as nullable

By Trent L. Taylor - 3/24/2006

Kevin,

Only structure data types support a generic nullable, and a string is not a structure data type.  To determine what does and does not support a nullable generic, you can reference the object browse and look at the inheritance.  Anything that inherits from a System.ValueType can be used with a nullable generic.  Here is the rule of thumb, if Nothing or NULL can be assigned to it, it does not support a nullable generic.

When nullable generic is not supported, use Return Alternate on NULL option for the field and specify the exact text below that should be returned.  For example, if you want to return an empty string, enter the following value: ""

By Kevin Lingofelter - 3/24/2006

Sounds good. What's the advantage of using the nullable generic type then?
By Trent L. Taylor - 3/25/2006

Really the same concept of any other type of generic...saving time when applicable without having to explicitly define a specific class or value while remaining strong-typed.  That is why they are dubbed "generics"...the same class can be used interchangably for different value types.