Solved

Operator '!' cannot be applied to operand of type

Posted on 2008-10-17
11
516 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
SharePoint Admin?

Enable Your Employees To Focus On The Core With Intuitive Onscreen Guidance That is With You At The Moment of Need.

 
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

Suggested Solutions

Title # Comments Views Activity
insert value of checklistbox checked 4 39
RegEx with optional part 4 56
C# XML Get Values 4 35
ASP.NET data base connection 35 40
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
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…
Exchange organizations may use the Journaling Agent of the Transport Service to archive messages going through Exchange. However, if the Transport Service is integrated with some email content management application (such as an antispam), the admini…

726 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