views:

116

answers:

3

Hey guys, not far into programming and just joined this forum of mighty company so this is a silly question, but what is the best way to clear textboxes in VB.Net and what is the difference between the two methods? I have also seen people be critical of folk using clear objects on their forms and i can see why but in this case, i am only learning.

txtbox1.Clear()

or

txtbox1.Text = ""

Any help is much appreciated.

A: 

Hey,

Clear() set the Text property to nothing. So txtbox1.Text = Nothing does the same thing as clear. An empty string (also available through String.Empty) is not a null reference, but has no value of course.

Brian
The documentation isn’t definite on this so I wouldn’t rely on this behaviour remaining unchanged.
Konrad Rudolph
Thanks guys! =)
William Mc
A: 

I’m not sure why Microsoft felt the need to include this extra method (since it seems to do nothing special) – but since it’s there, I recommend using it.

Konrad Rudolph
+2  A: 

The Clear method is defined as

    public void Clear() { 
        Text = null;
    } 

The Text property's setter starts with

        set { 
            if (value == null) { 
                value = "";
            } 

I assume this answers your question.

SLaks
It looks like somebody at Microsoft had a bad day that day. This looks weird. +1 for digging it out. –1 for Microsoft for crappy documentation (“Clears all the content from the text box” – **really**?) and weird code.
Konrad Rudolph
Thanks very much. Very impressed with the speed of responses. Thanks again!
William Mc
@Konrad: Well, it does. What do you have against the documentation?
SLaks
Hey guys! I can't accept and check an answer at the moment as, like i said, i am new to programming and still a little unsure about what is going on deep behind the scenes of the code you have all shown me other than clearing the text within the textbox on the form interface. I will defo be digging into my tutor for how these methods tie in with checking null references then i'll come back and rate an answer. Thanks again :)
William Mc