PC Review


Reply
Thread Tools Rate Thread

Comparing Empty Value types

 
 
=?Utf-8?B?RGVubmlz?=
Guest
Posts: n/a
 
      17th Nov 2004
I have a public variable in a class of type color declared as follows:

public mycolor as color = color.Empty

I want to check to see if the user has specified a color like;

if mycolor = Color.Empty then.....
or
if mycolor is Color.Empty then .......

I get errors saying that = can be used for colors and is can't be used with
value types. I give up...how do I do this. Thanks.
--
Dennis in Houston
 
Reply With Quote
 
 
 
 
Imran Koradia
Guest
Posts: n/a
 
      17th Nov 2004
Since ValueType derives from Object, you can use the Equals method. For
Value Types, this returns true if the instances are of the same type and
have the same value.

If mycolor.Equals(Color.Empty) Then
MessageBox.Show("mycolor is Empty")
End If

hope that helps..
Imran.


"Dennis" <(E-Mail Removed)> wrote in message
news:9A638870-CBE0-47D8-A453-(E-Mail Removed)...
>I have a public variable in a class of type color declared as follows:
>
> public mycolor as color = color.Empty
>
> I want to check to see if the user has specified a color like;
>
> if mycolor = Color.Empty then.....
> or
> if mycolor is Color.Empty then .......
>
> I get errors saying that = can be used for colors and is can't be used
> with
> value types. I give up...how do I do this. Thanks.
> --
> Dennis in Houston



 
Reply With Quote
 
Jay B. Harlow [MVP - Outlook]
Guest
Posts: n/a
 
      17th Nov 2004
Dennis,
Did you try the Equals function or the Equals Operator?

If mycolor.Equals(Color.Empty) Then

End If

To use the Equals Operator you need to call the shared function that is
created.

If Color.op_Equality(mycolor, Color.Empty) Then

End If

See the Color.Equals function in the online help for some important remarks
about using Color.Equals and when you may want to use Color.ToArgb to
compare two colors.

If mycolor.ToArgb() = Color.Empty.ToArgb() Then

End If


NOTE: VS.NET 2005 (aka Whidbey, due out later in 2005) will support calling
the Equals Operator with the familiar "=" syntax. For information on VS.NET
2005 see http://lab.msdn.microsoft.com/vs2005/.

' VS.NET 2005 sample
If mycolor = Color.Empty Then

End If

Hope this helps
Jay

"Dennis" <(E-Mail Removed)> wrote in message
news:9A638870-CBE0-47D8-A453-(E-Mail Removed)...
>I have a public variable in a class of type color declared as follows:
>
> public mycolor as color = color.Empty
>
> I want to check to see if the user has specified a color like;
>
> if mycolor = Color.Empty then.....
> or
> if mycolor is Color.Empty then .......
>
> I get errors saying that = can be used for colors and is can't be used
> with
> value types. I give up...how do I do this. Thanks.
> --
> Dennis in Houston



 
Reply With Quote
 
Cor Ligthert
Guest
Posts: n/a
 
      17th Nov 2004
Dennis,

As alternative
If mycolor.ToArgb = mycolor.Empty.ToArgb

End if

Cor

"Dennis" <(E-Mail Removed)>:
>
> public mycolor as color = color.Empty
>
> I want to check to see if the user has specified a color like;
>
> if mycolor = Color.Empty then.....
> or
> if mycolor is Color.Empty then .......
>
> I get errors saying that = can be used for colors and is can't be used
> with
> value types. I give up...how do I do this. Thanks.
> --
> Dennis in Houston



 
Reply With Quote
 
Herfried K. Wagner [MVP]
Guest
Posts: n/a
 
      17th Nov 2004
"Cor Ligthert" <(E-Mail Removed)> schrieb:
> As alternative
> If mycolor.ToArgb = mycolor.Empty.ToArgb


This only works for 'Color'. I would stick with the more general pattern of
using 'Equals'...

--
M S Herfried K. Wagner
M V P <URL:http://dotnet.mvps.org/>
V B <URL:http://dotnet.mvps.org/dotnet/faqs/>

 
Reply With Quote
 
Cor Ligthert
Guest
Posts: n/a
 
      17th Nov 2004
"Herfried K. Wagner [MVP]"

>>As alternative
>>If mycolor.ToArgb = mycolor.Empty.ToArgb


> This only works for 'Color'. I would stick with the more general pattern
> of using 'Equals'...


You are free to do that. I will not blaim you for that.

However I use this one, I find it nicer just a personal choise

May I do that as well?

:-)

Cor


 
Reply With Quote
 
Jay B. Harlow [MVP - Outlook]
Guest
Posts: n/a
 
      17th Nov 2004
Cor (& Herfried),
As I stated in my initial reply:

> See the Color.Equals function in the online help for some important
> remarks about using Color.Equals and when you may want to use Color.ToArgb
> to compare two colors.


If I want to compare what is visible seen I will use Color.ToArgb in other
words to compare the Alpha, Red, Green & Blue values.

If I want to compare what is literally used I will use Color.Equals or
Color.op_Equality, in other words to compare the names of the color or the
values for unnamed colors.

For example:

Dim color1 As Color = Color.Black
Dim color2 As Color = Color.FromArgb(0,0,0)

Both are visibly Black, however they are not logically the same value. In
other words:

If color1.Equals(color2) Then
' color1 is the same named color as color2
End If

If color1.ToArgb() = color2.ToArgb() Then
' color1 & color2 have the same Alpha, Red, Green & Blue values
End If

Just a thought
Jay


"Cor Ligthert" <(E-Mail Removed)> wrote in message
news:(E-Mail Removed)...
> Dennis,
>
> As alternative
> If mycolor.ToArgb = mycolor.Empty.ToArgb
>
> End if
>
> Cor
>
> "Dennis" <(E-Mail Removed)>:
>>
>> public mycolor as color = color.Empty
>>
>> I want to check to see if the user has specified a color like;
>>
>> if mycolor = Color.Empty then.....
>> or
>> if mycolor is Color.Empty then .......
>>
>> I get errors saying that = can be used for colors and is can't be used
>> with
>> value types. I give up...how do I do this. Thanks.
>> --
>> Dennis in Houston

>
>



 
Reply With Quote
 
Cor Ligthert
Guest
Posts: n/a
 
      17th Nov 2004
Jay,

It was just an alternative, this one is not real very important for me,
however I like more evaluating for equality using the = operator than
evaluating using a methode and with the color that is possible.

But the "Is" in this functions tells it very good, therefore it is not
really that important for me.

However as I said, just an alternative.

Cor

"Jay B. Harlow [MVP - Outlook]" <(E-Mail Removed)>

> Cor (& Herfried),
> As I stated in my initial reply:
>
>> See the Color.Equals function in the online help for some important
>> remarks about using Color.Equals and when you may want to use
>> Color.ToArgb to compare two colors.

>
> If I want to compare what is visible seen I will use Color.ToArgb in other
> words to compare the Alpha, Red, Green & Blue values.
>
> If I want to compare what is literally used I will use Color.Equals or
> Color.op_Equality, in other words to compare the names of the color or the
> values for unnamed colors.
>
> For example:
>
> Dim color1 As Color = Color.Black
> Dim color2 As Color = Color.FromArgb(0,0,0)
>
> Both are visibly Black, however they are not logically the same value. In
> other words:
>
> If color1.Equals(color2) Then
> ' color1 is the same named color as color2
> End If
>
> If color1.ToArgb() = color2.ToArgb() Then
> ' color1 & color2 have the same Alpha, Red, Green & Blue values
> End If
>
> Just a thought
> Jay
>
>
> "Cor Ligthert" <(E-Mail Removed)> wrote in message
> news:(E-Mail Removed)...
>> Dennis,
>>
>> As alternative
>> If mycolor.ToArgb = mycolor.Empty.ToArgb
>>
>> End if
>>
>> Cor
>>
>> "Dennis" <(E-Mail Removed)>:
>>>
>>> public mycolor as color = color.Empty
>>>
>>> I want to check to see if the user has specified a color like;
>>>
>>> if mycolor = Color.Empty then.....
>>> or
>>> if mycolor is Color.Empty then .......
>>>
>>> I get errors saying that = can be used for colors and is can't be used
>>> with
>>> value types. I give up...how do I do this. Thanks.
>>> --
>>> Dennis in Houston

>>
>>

>
>



 
Reply With Quote
 
Jay B. Harlow [MVP - Outlook]
Guest
Posts: n/a
 
      17th Nov 2004
Cor,
> It was just an alternative, this one is not real very important for me,


Obviously I know its an alternative, Hopefully you noticed that I stated
last night it was an alternative & again just now.

Thanks for noticing.
Jay



"Cor Ligthert" <(E-Mail Removed)> wrote in message
news:%(E-Mail Removed)...
> Jay,
>
> It was just an alternative, this one is not real very important for me,
> however I like more evaluating for equality using the = operator than
> evaluating using a methode and with the color that is possible.
>
> But the "Is" in this functions tells it very good, therefore it is not
> really that important for me.
>
> However as I said, just an alternative.
>
> Cor
>
> "Jay B. Harlow [MVP - Outlook]" <(E-Mail Removed)>
>
>> Cor (& Herfried),
>> As I stated in my initial reply:
>>
>>> See the Color.Equals function in the online help for some important
>>> remarks about using Color.Equals and when you may want to use
>>> Color.ToArgb to compare two colors.

>>
>> If I want to compare what is visible seen I will use Color.ToArgb in
>> other words to compare the Alpha, Red, Green & Blue values.
>>
>> If I want to compare what is literally used I will use Color.Equals or
>> Color.op_Equality, in other words to compare the names of the color or
>> the values for unnamed colors.
>>
>> For example:
>>
>> Dim color1 As Color = Color.Black
>> Dim color2 As Color = Color.FromArgb(0,0,0)
>>
>> Both are visibly Black, however they are not logically the same value. In
>> other words:
>>
>> If color1.Equals(color2) Then
>> ' color1 is the same named color as color2
>> End If
>>
>> If color1.ToArgb() = color2.ToArgb() Then
>> ' color1 & color2 have the same Alpha, Red, Green & Blue values
>> End If
>>
>> Just a thought
>> Jay
>>
>>
>> "Cor Ligthert" <(E-Mail Removed)> wrote in message
>> news:(E-Mail Removed)...
>>> Dennis,
>>>
>>> As alternative
>>> If mycolor.ToArgb = mycolor.Empty.ToArgb
>>>
>>> End if
>>>
>>> Cor
>>>
>>> "Dennis" <(E-Mail Removed)>:
>>>>
>>>> public mycolor as color = color.Empty
>>>>
>>>> I want to check to see if the user has specified a color like;
>>>>
>>>> if mycolor = Color.Empty then.....
>>>> or
>>>> if mycolor is Color.Empty then .......
>>>>
>>>> I get errors saying that = can be used for colors and is can't be used
>>>> with
>>>> value types. I give up...how do I do this. Thanks.
>>>> --
>>>> Dennis in Houston
>>>
>>>

>>
>>

>
>



 
Reply With Quote
 
Herfried K. Wagner [MVP]
Guest
Posts: n/a
 
      17th Nov 2004
Jay,

"Jay B. Harlow [MVP - Outlook]" <(E-Mail Removed)> schrieb:
> For example:
>
> Dim color1 As Color = Color.Black
> Dim color2 As Color = Color.FromArgb(0,0,0)
>
> Both are visibly Black, however they are not logically the same value. In
> other words:
>
> If color1.Equals(color2) Then
> ' color1 is the same named color as color2
> End If
>
> If color1.ToArgb() = color2.ToArgb() Then
> ' color1 & color2 have the same Alpha, Red, Green & Blue values
> End If


That's a valid point, and the way to go depends on the situation.

--
M S Herfried K. Wagner
M V P <URL:http://dotnet.mvps.org/>
V B <URL:http://dotnet.mvps.org/dotnet/faqs/>

 
Reply With Quote
 
 
 
Reply

Thread Tools
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Comparing types Mike J Microsoft C# .NET 3 9th May 2007 12:15 AM
Comparing Types derek Microsoft C# .NET 5 23rd Dec 2004 05:13 PM
Comparing Types =?Utf-8?B?RGVubmlz?= Microsoft VB .NET 3 21st Nov 2004 06:22 PM
Comparing types. Chris Blanco Microsoft C# .NET 3 5th Aug 2003 03:53 PM
Is there any where I can see a grid comparing SQL Types to system Types? Nick Microsoft ADO .NET 2 4th Jul 2003 02:19 AM


Features
 

Advertising
 

Newsgroups
 


All times are GMT +1. The time now is 10:11 AM.