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!

Set Color to Nothing

Status
Not open for further replies.

sony2000

Programmer
Dec 7, 2003
34
0
0
HK
Hi,

I have a variable "Dim color1 as Color", but when I
use "if ( color1 is nothing ) then ...."

the VB.NET does not let me to do so....I don't know why.
Can someone teach me if there is another way of doing
the same thing?

Thanks,
Raymond
 
It won't let you because Color is a structure, which is a value type, and Is is an operator reserved for comparing reference types to see if they point at the same object.

If that doesn't make sense, you need to look up value and reference types, and structures as well.

However, luckily for you, a color variable gets initialised to the value Color.Empty.

So you might think that you can do

Code:
if color1 = Color.Empty

But although I agree that would be the most obvious choice, you have to use the Equals method of the color structure. You can either do it one of the three following ways - it does not make any difference (though the fact that you can do it three ways does add to the confusion - just pick the way that is most natural for you)

Code:
        If Color.Equals(Color.Empty, Color1)...
        If Color1.Equals(Color.Empty) Then...
        If Color.Empty.Equals(Color1) Then...

In other languages like Java, the class designer can change what = and other operators mean. I don't think this is yet possible in .net, which is presumably why you have to use the Equals method.

Hope this helps

Mark [openup]
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top