StrataFrame Forum

String Comparison in BO is not working properly

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

By Jeff Pagley - 8/27/2007

I have setup a field string value in the BO Mapper custom field properties as follows:

Return Alternate on Null/Set Null on Alternate (reference type)

NULL Replacement Value: ""

The problem is when I clear the field value on the form's control and save, the BO is writing an empty string back to the database instead of NULL.

However, when I modify your BO code by changing [If value IsNot "" Then] to

[If value.Length > 0 Then] (see modified code below),  the logic works and the BO writes the NULL value back to the database.

What is going on here?

''' <summary>

''' JobAddress

''' </summary>

''' <remarks></remarks>

<Browsable(False), _

BusinessFieldDisplayInEditor(), _

Description("JobAddress"), _

DesignerSerializationVisibility(DesignerSerializationVisibility.Hidden)> _

Public Property [JobAddress]() As System.String

Get

Dim loValue As Object

loValue = Me.CurrentRow.Item("JobAddress")

If loValue Is DBNull.Value Then

Return ""

Else

Return CType(loValue, System.String)

End If

End Get

Set(ByVal value As System.String)

' If value IsNot "" Then

If value.Length > 0 Then

Me.CurrentRow.Item("JobAddress") = value

Else

Me.CurrentRow.Item("JobAddress") = DBNull.Value

End If

End Set

End Property

By StrataFrame Team - 8/28/2007

Being a string, you might want to change it from the reference type option to value type option (since strings are kinda treated like value types being immutable and all...).  That way, the test will be "If value <> "" Then" which should work.  Also, you might consider using String.Empty as the alternate value instead of "" since String.Empty is already interned.
By Jeff Pagley - 8/28/2007

I am confused. Why did you tell this user to use the Reference type for strings here http://forum.strataframe.net/FindPost10792.aspx  and you are telling me to use the value type for strings.
By StrataFrame Team - 8/28/2007

Well, the difference is what you're using for the alternate value.  He was using "Nothing" (I think) which requires that VB use the Is and IsNot operators, while String.Empty or "" requires that you use the = and <> operators (which get set by using the value type).  So, if you want to use Nothing as your alternate value, then use reference type, but if you want to use String.Empty or "", then use the value type because it will change the operators that are used in the test.