tags:

views:

319

answers:

3

I'm using FileHelper to generate object's property. Here is an example of one property:

<FieldOptional(), _
 FieldTrim(TrimMode.Both)> _
 <FieldNullValue(GetType(String), " ")> _
Public StoreNo As String

As you can see the StoreNo will either have a value or " ", one of the business policy is to check if the StoreNo is empty or nothing if the object's StoreNo is empty or null then the record will not create.

I though about creating an HasValue Function in the class to check the StoreNo and other properties in the object but I feel like it is a hack.

Public Function HasValue() As Boolean

    Dim _HasValue As Boolean = True

    If StringHelper.IsNullOrBlank(Me.StoreNo) Then
        _HasValue = False
    End If

    Return _HasValue
End Function

I don't think this approach is an ideal solution. what if the StoreNo is remove or change to something else. What's the best approach to check object's property?

+3  A: 

How about String.IsNullOrEmpty( ) ?

Perhaps I am missing something, this seems too simple :)


Also, there is no reason to write code like this:

if String.IsNullOrEmpty( str ) then
    hasValue = false
end

You are already evaluating a boolean expression, so just assign the thing directly:

hasValue = Not String.IsNullOrEmpty( str )
Ed Swangren
+7  A: 

Not sure it totally answers your question, but your HasValue function is horribly convoluted. It could be simplified to the folowing:

Public Function HasValue() As Boolean
    Return Not String.IsNullOrEmpty(Me.StoreNo);
End Function

But then why even bother with the custom function when String.IsNullOrEmpty exists in the BCL?

Noldorin
`String.IsNullOrEmpty` returns false for " ". You should check trimmed string too.
Mehrdad Afshari
I want to check several properties and not just one. It is an example code.
Jack
@Mehrdad: That's true, but only in those cases where a single space *isn't* a valid value. While that covers the vast majority of cases, it's not *always* the case, and bears mentioning.
Mike Hofer
@Mike: ...which is apparently, the OP's case (or maybe not. dunno.)
Mehrdad Afshari
+2  A: 

You can use the String.IsNullOrEmpty function to test your string.

EDIT

I missed that you are asking a broader question before. If what you are wanting is a general purpose type of check that can check multiple properties and be applied to different objects of the same type, you might want to consider extension methods. For example

public static class VariousTests
{
   public static bool IsValidStoreNumber (this string value)
   {
    if (string.IsNullOrEmpty(value)) return false;

     // Put additional checks here.
   }
}

Note that both the class and method are declared as static.  
Note also the use of "this" in the method's parameter list, followed by the 
object type the method applies to.

This would let you call IsValidStoreNumber on any string variable as if it were a method of that string. For example:

string test = "Some Value";
if (test.IsValidStoreNumber() )
{
   // handle case where it is a good store number.  
}
else
{
   // handle case where it is not a good store number.  
}

Hope this is a more helpful answer

EDIT TO ADD VB VERSION OF CODE

I realized I show C# code when your original question used VB code. Here is the VB version of my extension method example.

Imports System.Runtime.CompilerServices

Module VariousTests
  <Extension()> _
  Public Function IsValidStoreNumber (ByVal value As String) as Boolean
    If string.IsNullOrEmpty(value) Then
      Return False;
    End If

    ' Put additional checks here.
  End Sub
End Module

To Use:

Imports VariousTests

Then inside a method :

   Dim test as string  = "Some Value";
   If test.IsValidStoreNumber() Then
      // handle case where it is a good store number.  
   Else
      // handle case where it is not a good store number.  
   End IF
RB Davidson
+1 for extension methods.
Mike Hofer