tags:

views:

70

answers:

5

I want to add one more function/property to a text property of a textbox like this. txtControl.Text.IsEmpty(); or txtControl.Text.IsEmpty; which returns me bool value.

I dont want to compare for empty string every time.

i.e. if(txtControl.text==string.Empty) {} else {}

Can we do some other way also

if just want to do it like if(txtControl.text.isEmpty){}

+2  A: 

The text property is a string. The string class contains a static method IsNullOrEmpty() So you could use if (String.IsNullOrEmpty(txtControl.Text)) {} else {}

Option2 is to create a extra property on the textbox:

public class MyTextbox : Textbox
{
  public bool IsEmpty
  { 
    get
    { 
      return String.IsNullOrEmpty(this.Text);
    }
  }
}

You can use it like this: if (txtMyTextbox.IsEmpty) {} else {}

edosoft
+2  A: 

Why not create an extension method:

public static bool HasEmptyText(this TextBox textBox)
{
    return string.IsNullOrEmpty(textBox.text);
}

Then you can just use txtControl.HasEmptyText().

David M
@david: How can i implement it for all txtboxes , i m trying to create custom controls.
Shantanu Gupta
look up what extention methodes are Shantanu. As long as the methodes are in a referenced assembly they will work on all classes you have declared them for.
Sdry
A: 

The Text property is a string, which already has a IsNullOrEmpty function on it.

Oded
A: 

String.IsNullOrEmpty( textControl.Text )

kurige
+6  A: 

In c# 3.0 you can do something like this..

namespace ExtensionMethods
{
    public static class MyExtensions
    {
        public static bool IsTextEmpty(this Textbox txtBox)
        {
            return string.IsNullOrEmpty(txtBox.Text);
        }
    }   
}

Then you can use it like this.

bool isEmpty = yourTxtBox.IsTextEmpty();

This will work for all textbox instances across your application, provided you have referred the namespace of extension method. If you have your custom textbox then replace the type TextBox with the type of your custom Textbox. It may look like this if you have your own TextBox..

namespace ExtensionMethods
{
    public static class MyExtensions
    {
        public static bool IsTextEmpty(this MyTextbox txtBox)
        {
            return string.IsNullOrEmpty(txtBox.Text);
        }
    }   
}
this. __curious_geek
this Textbox txtBox : Why this is used here ?
Shantanu Gupta
This is a new feature in C# 3.0 - extension methods. you can find more information about extension methods here - http://msdn.microsoft.com/en-us/library/bb383977.aspx.
this. __curious_geek
@this: Thank's that's a great help nd something new for me
Shantanu Gupta
C# 3.0 has lot of new exciting features. you may check it out here - http://msdn.microsoft.com/en-us/library/ms364047%28VS.80%29.aspx
this. __curious_geek
@this: Can I overload this type of functions ?
Shantanu Gupta
Yes. The only condition is that the first argument has to be of the type for which you are creating the extension method prefixed with keyword 'this' :)
this. __curious_geek