Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Boolean results for a comparison

Status
Not open for further replies.

gbaughma

IS-IT--Management
Staff member
Nov 21, 2003
4,769
11
38
58
US
I used to do a "Trick" a long time ago.

Instead of using "IF" statements, for example:

Code:
If A = B Then
    Something = True
Else
    Something = False
End If

... I used to be able to do something like:
Code:
Something = (A = B)
... which would evaluate A=B, and set the "True" or "False" accordingly. Of course, granted, this was WAAAAY back in the day.

Well, now I have a situation where I have to set the visibility of form elements; I've marked whether they should be visible or not using the Tag field (I have to toggle them on and off).

I *WAS* trying to do something like this:
Code:
btnFirstButton.Visible = (btnFirstButton.Tag = "Y")

... however, that's not working. Is there not a simple way to do this any more? I would hate to do a whole bunch of "If btnFirstButton.Tag = "Y" Then btnFirstButton.Visible = True ELSE btnFirstButton.Visible = False"

... 'cause that would suck.

Any thoughts?

TIA!


Just my 2¢

"What the captain doesn't realize is that we've secretly replaced his Dilithium Crystals with new Folger's Crystals."

--Greg
 
Maybe since .Tag holds an Object, you need to cast it to a String?
Code:
btnFirstButton.Visible = (CStr(btnFirstButton.Tag) = "Y")
 
Dave is most likely right, but I have a side comment/solution. If you are doing the same thing in several buttons, e.g. btnFirstButton.Visible = (CStr(btnFirstButton.Tag) = "Y") or the If then construct one should consider creating a button sub class and put the code in that class, once and only once, then use that special button class on the form.

Think OOP. Anytime code is used more then once the idea of subclassing (and/or composition) should be considered.

Lion Crest Software Services
Anthony L. Testi
President
 
  • Thread starter
  • Moderator
  • #4
But the .Visible is looking for a "True" or "False"

I don't see how casting it to a string is going to set the property to "True" or "False".

Like I said, in the "Old days", I could do something like:

Print A=A
... and it would return a boolean "True"

... therefore, if I did:

button.Visible = (button.Tag="Y")
... it would essentially set button.Visible = True or False, depending on if button.tag was, in fact, ="Y"

I guess it was a programming trick that used to work, but doesn't any longer. It seems a waste, though, because it would let me save a lot of "IF" statements.

It would be nice if there was something that would allow me to do something like this:

Button.Visible = GiveABooleanResultFor(Button.Tag = "Y")

... that would return a True or a False, and let me set the property from there. If there's a command that does that, I haven't been able to find it.



Just my 2¢

"What the captain doesn't realize is that we've secretly replaced his Dilithium Crystals with new Folger's Crystals."

--Greg
 
You need to use the Convert because the Tag property is an object, not a string. You are trying to compare an object to a string and this will never be true. It is like comparing an actual car to the name Car. A car does not equal the word Car.

Instead, you use the convert to change that object into a string. So you are taking what the code thinks is an object and creating a string representative. Then you are comparing a string car to the text of car.

Finally, if you have a lot of controls on your form that you are doing this for, MrDataGuy is right in creating your own class for that. If that is not something you are comfortable with, you also might want to consider a chunk of code that loops through the controls and does all this without having to resort to coding each code separately.

Code:
For Each ctl As Control In Me.Controls
    If (TypeOf ctl Is Button) Then
        Dim btn As Button = DirectCast(ctl, Button)
        btn.Visible = (CStr(btn.Tag) = "Y")
    End If
Next ctl

=======================================
People think it must be fun to be a super genius, but they don't realize how hard it is to put up with all the idiots in the world. (Calvin from Calvin And Hobbs)

Robert L. Johnson III
CCNA, CCDA, MCSA, CNA, Net+, A+, CHDP
C#.NET Programmer
 
One other point. IMO ( and only IMO ) the TAG property should only be used as last resort, you should instead.

1) Once again create a new class,
2) Then add an appropriate boolean property to that new class
3) In the assign method of that new cCass:property change the Class:Visible proprety.

One of the results is that the need for a IF is completly removed.

Side comment:

Years ago my Bosses Boss took me under his wing as a mentor and taught me some deep understanding of OOP. He was a long time Small Talk programmer (One of the first, and best OOP languages in his opinion. ) and he was a self proclaimed OOP NAZI! Anyhow he showed me (or tried to show me) that any flow control staments, "IF", "CASE" etc. in a program most likely showed that a programmer had poor OOP understanding. That is 99% of the time the "IF" and other control statements are not needed when good OOP is used. Above I just showed an example. Again and again I gave him examples of where I thought I needed an IF, or a Case or a... and he showed me no, and in fact the 'answer' was much cleaner code.

Now do I have "IF" and the like in my code, yes very much so. But maybe ~50% less then the next programmer. In many situations I do stop and think, do I need this "IF", do I need this "Case" should I think more OOP? Sometimes yes, sometimes no, but I think it is a good question to ask one self while programming.

Lion Crest Software Services
Anthony L. Testi
President
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top