tags:

views:

109

answers:

6

Hello,

I am using .NET 3.5. I am creating a simple class and wants to make sure that the class after processing should not be null or should not be a new too..

So if we can just Test it like that

Dim objClass as new Class()

' do some processing with the class '
' and then.. check that if this object returned is not empty '

If (objClass = Class.Empty) Then
//Do stuff
Else
//Do Other Stuff
End If

is there a way we can create this empty Field like we have in String.Empty?

+1  A: 

Yes, it is called null for reference types, and new T() for value types.

leppie
but string.empty is not null for reference types
Arseny
Yes Arseny it is not null.. But I want this empty field to validate both Null and New Object at the same time... Can it be done? Is their any code that I should write?
Mohit
@leppie: Maybe you mean `default(T)` ?
abatishchev
+4  A: 

there is design pattern Null Object pattern for it.

Arseny
+1  A: 

But in your example the class isn't technically empty. You've created an instance of Class using the default constructor. Who knows, the default constructor may have initialised over 10MB of string content. What your code is technically checking is, is the class equal to it's default state just after being constructed.

See this for example of VB constructors and what is happening.

If you correctly implemented CompareTo(...) you could call

If (objClass.CompareTo(new Class()) == 0) Then 
 //Do stuff 
Else
 //Do Other Stuff 
End If 

But that would seem overkill / expensive, but the only way it would work.

Another option would be: (sorry c# based example)

Interface IEmpyClass
{
   bool IsEmptyClass{get;}
}

public class Class : IEmptyClass
{
   public bool IsEmptyClass{get; private set;}

   public Class()
   {
     IsEmptyClass = true;
   }

   public void DoSomething()
   {
       // Do something
       IsEmptyClass = false;
   }
}

You would be responsible for implementing the example and changing the property when the class state changed from "empty", but that would be quicker in code, etc. and could cope when clases have constructors with members. It could be checked just with

If (objClass.IsEmptyClass) Then 
 //Do stuff 
Else
 //Do Other Stuff 
End If 
Paul Hadfield
Yes Paul.. You are right I am checking the class object if it is null or if it is in default state.. Basically in the processing I am sending this object to somewhere and getting it back as objClass=ProcessClass(objClass)Now here I need to know whether this class object now contains Data in it or not..
Mohit
See edit in answer above, sorry examples are in C#. You could say that this is a version of the "NULL object" design pattern referred to by Arseny
Paul Hadfield
Thanks Paul... See my answer below... And if possible tell me if there is any better thing I can do in it?
Mohit
A: 

You can have an Extension method called "Empty" on "Class" which takes one parameter of type var (if .net 4.0) or object (older version). The implementation of this extension method will check whether its a null or not initialised to the default state and return true or false.

TheITGuy
Why would this have to be an extension method?
Hans Passant
So that every class will have "Empty" method with a generic implementaion of Empty logic using an interface. Have a look at following code.public static class ObjectExtensions { public static bool Empty(this object obj) { IEmpty otherObj = obj as IEmpty; if (otherObj == null) return true; else if (otherObj.IsEmpty) return true; else return false }}Now if any class that implement IEmpty interface gets true if its null or IsEmpty is set to true otherwise false. If any class that don't implement IEmpty will always get true as IsEmpty never been implemented.
TheITGuy
A: 

OK Guys Thanks for your help.. I have reached here so far.. Working very good now.

Public Class cc
    Private _obj As Boolean = False
    Public _index As Integer = 0

    Public ReadOnly Property IsEmpty() As Boolean
        Get
            Return _obj
        End Get
    End Property

    Public Property Index() As Integer
        Get
            Return _index
        End Get
        Set(ByVal value As Integer)
            _obj = False
            _index = value //I need to set it to false for every property of that class.
        End Set
    End Property


    Sub New()
        _obj = True
    End Sub



    Public Shared Function Empty(ByVal obj As cc) As Boolean
        If Not (obj Is Nothing) Then
            If Not (obj.IsEmpty()) Then
                Return False
            End If
        End If
        Return True
    End Function
End Class

In Separate Class--------------------

        Dim Sc As New cc()
        //'Sc=Nothing
        Sc.Index=1

        If (cc.Empty(Sc)) Then
            Label1.Text = "Empty"
        Else
            Label1.Text ="Not Empty"
        End If
Mohit
Please comment on it guys.. if there is something wrong here.. I guess this can be done without Interface too. But anyway thats not a thing to worry..
Mohit
It definitely looks a workable solution if it does exactly what you want.
Paul Hadfield
Thanks You all... :)Regards.Mohit Bhardwaj.
Mohit
Two things: 1) The shared Empty function looks more like IsNullOrEmpty and could be written as `Return obj IsNot Nothing AndAlso Not obj.IsEmpty` (no parentheses on IsEmpty, it's a property) 2) If you want to be able to say `obj = cc.Empty`, you will need to override the equality operator (see http://blogs.msdn.com/b/jaredpar/archive/2008/05/12/equality-isn-t-easy.aspx and the articles it links to for information on how to do this correctly)
Gideon Engelberth
A: 

To define an Empty field you have to create a static readonly property in your class. The trouble if you have also to define the = operator on your class.

For example, if your class contains only one field of type string, you can define an = operator that check if the string fields of the two instances are equals.

Andrea Parodi