6 Replies - 450 Views - Last Post: 30 April 2012 - 08:57 AM Rate Topic: -----

#1 AN1554  Icon User is offline

  • D.I.C Head

Reputation: 7
  • View blog
  • Posts: 129
  • Joined: 03-April 11

Overheads - IF this then do that, or directly do that?

Posted 28 April 2012 - 11:40 AM

If txtbox.Text <> "" Then txtbox.Text = ""


Or

txtbox.Text = ""
?

Is it better to just erase the text of a textbox or first check if it is not empty then erase it if it is not? Does the IF add to overheads?

This post has been edited by AN1554: 28 April 2012 - 12:27 PM

Is This A Good Question/Topic? 0
  • +

Replies To: Overheads - IF this then do that, or directly do that?

#2 Neku  Icon User is offline

  • D.I.C Regular

Reputation: 18
  • View blog
  • Posts: 255
  • Joined: 21-May 09

Re: Overheads - IF this then do that, or directly do that?

Posted 28 April 2012 - 12:00 PM

depened.. what if it hold something you dont want to delete?
Was This Post Helpful? 0
  • +
  • -

#3 AN1554  Icon User is offline

  • D.I.C Head

Reputation: 7
  • View blog
  • Posts: 129
  • Joined: 03-April 11

Re: Overheads - IF this then do that, or directly do that?

Posted 28 April 2012 - 12:03 PM

The question is whether IF adds to overheads.

This post has been edited by AN1554: 28 April 2012 - 12:27 PM

Was This Post Helpful? 0
  • +
  • -

#4 Neku  Icon User is offline

  • D.I.C Regular

Reputation: 18
  • View blog
  • Posts: 255
  • Joined: 21-May 09

Re: Overheads - IF this then do that, or directly do that?

Posted 28 April 2012 - 12:49 PM

i guess it will take 1 or 2 extra cycles from the computer but again its really depened why you do this.
if its simply to reset a form its logical to just do
Textbox.Text = ""


but if it hold some data you want to keep its cheaper not to touch it than transfering the data to a variable and then back to the textbox.
Was This Post Helpful? 0
  • +
  • -

#5 BobRodes  Icon User is offline

  • Your Friendly Local Curmudgeon
  • member icon

Reputation: 551
  • View blog
  • Posts: 2,911
  • Joined: 19-May 09

Re: Overheads - IF this then do that, or directly do that?

Posted 30 April 2012 - 07:30 AM

AN is asking whether it's more overhead to set a text box to empty if it is already empty, or more to check first to see if it's empty. The answer is yes, IF adds to overhead, although the overhead is negligible. Evaluating a condition requires processor instructions to be run.

Also, you have to access the objects property whether you get it or set it, and this requires overhead too--more than a condition evaluation. So the greatest overhead is in referencing the property. Getting vs setting the property is about equal. Therefore, you incur double overhead each time you use the if statement, because you are referencing the property twice instead of once.

So, just set it to "". :)

This post has been edited by BobRodes: 30 April 2012 - 07:34 AM

Was This Post Helpful? 0
  • +
  • -

#6 Neku  Icon User is offline

  • D.I.C Regular

Reputation: 18
  • View blog
  • Posts: 255
  • Joined: 21-May 09

Re: Overheads - IF this then do that, or directly do that?

Posted 30 April 2012 - 07:36 AM

i know that this is what he asked but what if the textbox is not empty? >.<"

This post has been edited by Neku: 30 April 2012 - 07:36 AM

Was This Post Helpful? 0
  • +
  • -

#7 BobRodes  Icon User is offline

  • Your Friendly Local Curmudgeon
  • member icon

Reputation: 551
  • View blog
  • Posts: 2,911
  • Joined: 19-May 09

Re: Overheads - IF this then do that, or directly do that?

Posted 30 April 2012 - 08:57 AM

The answer to your question is that the code should empty it! He's not interested in the condition that you mention, "if it holds some data that you want to keep".

Look at his code. He's saying if the text box is not empty, then empty it. Or should he just empty it without checking, thereby having the possibility of a code redundancy by emptying an already empty text box.

A practical example would be:
Sub ClearTextBoxes()
For each tBox in Controls
    If TypeOf tBox Is TextBox Then
        tBox.Text = ""
    End If
Next

' Or:

For each tBox in Controls
    If TypeOf tBox is TextBox Then
        If tBox.Text <> "" Then
            tBox.Text = ""
        End If
    End If
Next


He's just asking which bit of code is more efficient in terms of performance.

And AN, by the way, this iterating through all the controls and checking to see if the type is a text box is the standard way of doing it, even though it has the inefficiency of checking each control including those that are not text boxes. :)
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1