Solved

Operator '!' cannot be applied to operand of type

Posted on 2008-10-17
11
500 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
 
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
How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

 
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

Do You Know the 4 Main Threat Actor Types?

Do you know the main threat actor types? Most attackers fall into one of four categories, each with their own favored tactics, techniques, and procedures.

Join & Write a Comment

Suggested Solutions

Exception Handling is in the core of any application that is able to dignify its name. In this article, I'll guide you through the process of writing a DRY (Don't Repeat Yourself) Exception Handling mechanism, using Aspect Oriented Programming.
Real-time is more about the business, not the technology. In day-to-day life, to make real-time decisions like buying or investing, business needs the latest information(e.g. Gold Rate/Stock Rate). Unlike traditional days, you need not wait for a fe…
This video shows how to remove a single email address from the Outlook 2010 Auto Suggestion memory. NOTE: For Outlook 2016 and 2013 perform the exact same steps. Open a new email: Click the New email button in Outlook. Start typing the address: …
This video explains how to create simple products associated to Magento configurable product and offers fast way of their generation with Store Manager for Magento tool.

708 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

Need Help in Real-Time?

Connect with top rated Experts

12 Experts available now in Live!

Get 1:1 Help Now