Solved

Operator '!' cannot be applied to operand of type

Posted on 2008-10-17
11
521 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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
Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
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

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

Extention Methods in C# 3.0 by Ivo Stoykov C# 3.0 offers extension methods. They allow extending existing classes without changing the class's source code or relying on inheritance. These are static methods invoked as instance method. This…
This article introduced a TextBox that supports transparent background.   Introduction TextBox is the most widely used control component in GUI design. Most GUI controls do not support transparent background and more or less do not have the…
In this video, viewers are given an introduction to using the Windows 10 Snipping Tool, how to quickly locate it when it's needed and also how make it always available with a single click of a mouse button, by pinning it to the Desktop Task Bar. Int…
Monitoring a network: how to monitor network services and why? Michael Kulchisky, MCSE, MCSA, MCP, VTSP, VSP, CCSP outlines the philosophy behind service monitoring and why a handshake validation is critical in network monitoring. Software utilized …

717 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