Solved

Operator '!' cannot be applied to operand of type

Posted on 2008-10-17
11
507 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
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 
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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Suggested Solutions

Performance in games development is paramount: every microsecond counts to be able to do everything in less than 33ms (aiming at 16ms). C# foreach statement is one of the worst performance killers, and here I explain why.
This article aims to explain the working of CircularLogArchiver. This tool was designed to solve the buildup of log file in cases where systems do not support circular logging or where circular logging is not enabled
In this video I am going to show you how to back up and restore Office 365 mailboxes using CodeTwo Backup for Office 365. Learn more about the tool used in this video here: http://www.codetwo.com/backup-for-office-365/ (http://www.codetwo.com/ba…
Hi friends,  in this video  I'll show you how new windows 10 user can learn the using of windows 10. Thank you.

911 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

20 Experts available now in Live!

Get 1:1 Help Now