Solved

Operator '!' cannot be applied to operand of type

Posted on 2008-10-17
11
511 Views
Last Modified: 2012-05-05
Experts I cannot figure out the error below please help

enum AttachedEventsEnum : byte
{
        None = 0,
        Clicked = 1,
        ...
}  

public static void SetDetachedEvent(object ctrl, AttachedEventsEnum eventType)
{
    ControlProperties extProp = GetExtProperties(ctrl);
    if (extProp != null)
    {
        extProp._attachedEvents = (extProp._attachedEvents && (! eventType));  // cs0023 error here
    }
}
0
Comment
Question by:wenyonk
  • 6
  • 5
11 Comments
 
LVL 2

Expert Comment

by:MasterPage
ID: 22746872
Hi wenyonk,

The ! operator can only be used on booleans (bool) in C#.
Rather than checking for !eventType, you'll want to check for eventType == 0.
extProp._attachedEvents = (extProp._attachedEvents && (eventType == 0)); 

Open in new window

0
 
LVL 2

Expert Comment

by:MasterPage
ID: 22746882
Or check for eventType == AttachedEventsEnum.None, rather.  In my first example you'd have to cast eventType to a byte or 0 to an AttachedEventsEnum.


._attachedEvents = (extProp._attachedEvents && (eventType == AttachedEventsEnum.None));

Open in new window

0
 

Author Comment

by:wenyonk
ID: 22747747
Thank you for your quick response

would it be  
._attachedEvents = (extProp._attachedEvents && (eventType != AttachedEventsEnum.None));

instead of                    
._attachedEvents = (extProp._attachedEvents && (eventType == AttachedEventsEnum.None));

0
Announcing the Most Valuable Experts of 2016

MVEs are more concerned with the satisfaction of those they help than with the considerable points they can earn. They are the types of people you feel privileged to call colleagues. Join us in honoring this amazing group of Experts.

 
LVL 2

Expert Comment

by:MasterPage
ID: 22748076
Depends on what you're trying to achieve.  eventType == AttachedEventsEnum.None is the same as what you were trying to achieve in your original code.

(extProp._attachedEvents && (! eventType)
would be true when extProp._attachedEvents is true and eventType is zero if ! worked in C# like it does in C.

SImilarly,
(extProp._attachedEvents && (eventType == AttachedEventsEnum.None)
will be true when extProp._attachedEvents is true and eventType is zero.

Otherwise,
(extProp._attachedEvents && (eventType != AttachedEventsEnum.None)
will be true when extProp._attachedEvents is true and eventType is nonzero.
0
 

Author Comment

by:wenyonk
ID: 22750083
PLEASE HELP...After making the above change I now get the following error:
Operator '&&' cannot be applied to operands of type AttachedEventsEnum' and 'bool'

Please note the following properties and there types
  _attachedEvents  is of type: AttachedEventsEnum
   eventType  is of type AttachedEventsEnum

below is the original vb code
Public Shared Sub SetDetachedEvent(ByVal ctl As Object, ByVal eventType As AttachedEventsEnum)
        Dim ctlProp As ControlProperties = GetProperties(ctl)
        If ctlProp IsNot Nothing Then
            ctlProp._attachedEvents = (ctlProp._attachedEvents And (Not eventType))
        End If
End Sub

0
 
LVL 2

Expert Comment

by:MasterPage
ID: 22750445
I assumed extProp._attachedEvents was a boolean because of the way you are using it.  What are you trying to accomplish with the
ctlProp._attachedEvents = (ctlProp._attachedEvents And (Not eventType))
line of code?  Are you trying to do bitwise masking of the AttachedEventsEnum?
0
 

Author Comment

by:wenyonk
ID: 22750716
Maybe if you see what is happening with the SetAttachEvent Method (in VB)

Public Shared Sub SetAttachedEvent(ByVal ctl As Object, ByVal newEvent As AttachedEvents)
        Dim ctlProp As ControlProperties = GetProperties(ctl)
        If ctlProp Is Nothing Then
            ctlProp = New ControlProperties
            SetProperties(ctl, ctlProp)
        End If
        ctlProp._attachedEvents = ctlProp._attachedEvents Or newEvent  // *** Here
    End Sub
0
 
LVL 2

Expert Comment

by:MasterPage
ID: 22752670
It's still not clear to me what you're expecting
ctlProp._attachedEvents = ctlProp._attachedEvents Or newEvent
to do.
Can you show me all of the values in AttachedEventsEnum?  Are they 0, 1, 2, 4 and you're using them as bit flags in ctlProp._attachedEvents?
0
 

Author Comment

by:wenyonk
ID: 22754421
None = 0
    EnterLeave = 1
    Validated = 2
    ControlClicked = 4
    AfterEnterEditMode = 8
    ItemNotInList = 16
    Print = 32
    AfterSelectChange = 64
    Dispose = 128
0
 
LVL 2

Accepted Solution

by:
MasterPage earned 500 total points
ID: 22761189
To attach an event:
ctlProp._attachedEvents = ctlProp._attachedEvents Or newEvent (VB)
ctlProp._attachedEvents = ctlProp._attachedEvents | newEvent (C#)

To detach an event:
ctlProp._attachedEvents = ctlProp._attachedEvents And Not eventType (VB)
ctlProp._attachedEvents = ctlProp._attachedEvents & ~eventType (C#)

(the ~ is a tilde... it's hard to see in this font)
Your original problem in C# was you were trying to use logical operators rather than bitwise operators.
&& is the logical "and."  & is the bitwise "and."
|| is the logical "or."  | is the bitwise "or."
! is the logical "not."  ~ is the bitwise "not."

Visual Basic doesn't make a distinction between logical and bitwise operators.  It is determined by the context of the code.

0
 

Author Closing Comment

by:wenyonk
ID: 31507368
Thank you Sorry for the Delay
0

Featured Post

Announcing the Most Valuable Experts of 2016

MVEs are more concerned with the satisfaction of those they help than with the considerable points they can earn. They are the types of people you feel privileged to call colleagues. Join us in honoring this amazing group of Experts.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

In order to hide the "ugly" records selectors (triangles) in the rowheaders, here are some suggestions. Microsoft doesn't have a direct method/property to do it. You can only hide the rowheader column. First solution, the easy way The first sol…
It was really hard time for me to get the understanding of Delegates in C#. I went through many websites and articles but I found them very clumsy. After going through those sites, I noted down the points in a easy way so here I am sharing that unde…
Nobody understands Phishing better than an anti-spam company. That’s why we are providing Phishing Awareness Training to our customers. According to a report by Verizon, only 3% of targeted users report malicious emails to management. With compan…
Finds all prime numbers in a range requested and places them in a public primes() array. I've demostrated a template size of 30 (2 * 3 * 5) but larger templates can be built such 210  (2 * 3 * 5 * 7) or 2310  (2 * 3 * 5 * 7 * 11). The larger templa…

825 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question