|
-
Oct 23rd, 2008, 08:04 PM
#1
Thread Starter
Fanatic Member
[RESOLVED] [2008] The "or" keyword
Hey guys,
I used the vb.net to c# converter to convert john's formanimator code to csharp.
However, he has this method in his code:
vb.net Code:
'Windows API function used to animate the form.
Private Declare Auto Function AnimateWindow Lib "user32" (ByVal hWnd As IntPtr, _
ByVal dwTime As Integer, _
ByVal dwFlags As Integer) As Boolean
'Animates the form automatically when it is loaded.
Private Sub m_Form_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles m_Form.Load
'MDI child forms do not support transparency so do not try to use the Blend method.
If Me.m_Form.MdiParent Is Nothing OrElse Me.m_Method <> AnimationMethod.Blend Then
'Activate the form.
Me.AnimateWindow(Me.m_Form.Handle, _
Me.m_Duration, _
Me.AW_ACTIVATE Or Me.m_Method Or Me.m_Direction)
End If
End Sub
and the associated variables + their types:
vb.net Code:
Public Enum AnimationDirection
Right = &H1
Left = &H2
Down = &H4
Up = &H8
End Enum
Public Enum AnimationMethod
Roll = &H0
Centre = &H10
Slide = &H40000
Blend = &H80000
End Enum
Private Const AW_HIDE As Integer = &H10000 'Hide the form.
Private Const AW_ACTIVATE As Integer = &H20000 'Activate the form.
Private m_Method As AnimationMethod 'The animation method used to show and hide the form.
Private m_Direction As AnimationDirection 'The direction in which to Roll or Slide the form.
So
now I dont understand how his hexadecimal values are working. The API is expecting an integer for "flags" for its last parameter.
however John is using
Me.AW_ACTIVATE Or Me.m_Method Or Me.m_Direction
which dosnt make sense to me.. what does OR do in this case? adds them up? because i think that the "flags" parameter (which is an int) must contain information about activate/hide, methodoftravel, and direction of travel.
So what does this mean and how can I incorparate it into csharp.
thanks
-
Oct 23rd, 2008, 08:52 PM
#2
Frenzied Member
Re: [2008] The "or" keyword
Well,
Code:
(1 = 1) or (1 = 2) 'returns -1
(1 = 2) or (1 = 2) 'returns 0
-1 is an integer and so is 0
(-1 means true, 0 means false)
-
Oct 23rd, 2008, 09:07 PM
#3
Re: [2008] The "or" keyword
I've replied to your PM but I'll elaborate here. In this case it's performing a bitwise OR operation. The dwFlags parameter is so-named because each bit is a flag. That means that the 32-bit value is basically the combination of 32 boolean values, where each bit is either 0 or 1, meaning False or True.
All the values used as constants in that code will have only one bit set to 1 in their binary form, with all other bits set to 0. The result of a bitwise OR will produce a number that has all the corresponding bits set. When that value gets passed to the API function each individual bit gets tested and those that are set will produce the corresponding behaviour. For instance, let's assume that m_Method was Slide (&H40000) and m_Direction was Left (&H2). In that case we are performing a bitwise OR on the following binary values:
Code:
00000000000000100000000000000000 (AW_ACTIVATE = &H20000)
00000000000001000000000000000000 (Slide = &H40000)
00000000000000000000000000000010 (Left = &H2)
In that case the result of a bitwise OR is 00000000000001100000000000000010, which is &H60002 in hex or 393218 in decimal. Hopefully those numbers show why hex is the preferred format. Note that a bitwise OR is basically a logical OR on each pair of corresponding bits where 0 and 1 are treated as False and True respectively.
-
Oct 23rd, 2008, 09:30 PM
#4
Thread Starter
Fanatic Member
Re: [2008] The "or" keyword
before I get onto language specific question, let me make sure I understand after googling it a bit.
( i am going to use 4 bits to make it easier on the eyes)
the result of a bitwise OR in that case would be 0110. If it was
the result would be 0100
so in csharp would I simply have to add brackets around 2 variables leaving the third variable out?
ps: i didn't recieve a new PM from you John. its okay though. I suspect your reply included something about the "+=" to add the event handlers.
edit1: Dosnt work. I have this statement:
csharp Code:
int flags = this.m_Method | this.m_Direction;
Error is:
Operator '|' cannot be applied to operands of type 'FormAnimator.AnimationMethod' and 'FormAnimator.AnimationDirection'
edit: I dont know if this is right, but i've casted the hex forms to integers. (int)variable.
I cant really test it for a few hours but is that okay? it should be because coverting to int should give it its integer value, do the operation on that.
Last edited by masfenix; Oct 23rd, 2008 at 09:48 PM.
-
Oct 23rd, 2008, 09:47 PM
#5
Re: [2008] The "or" keyword
C# uses stricter typing than VB. VB will implicitly convert the enumerated constants to integers, while C# requires an explicit cast:
CSharp Code:
int flags = (int)this.m_Method | (int)this.m_Direction;
Or is or, whether logical or bitwise. Exclusive or is exclusive or, whether logical or bitwise. 'Or' and '|' are the VB and C# operators for bitwise OR operations. 'Xor' and '^' are the VB and C# operators for bitwise exclusive OR operations. An OR will evaluate to true if either or both operands are true while an exclusive or will evaluate to true if either one or the other but NOT both of the operands are true.
0100 | 0010 = 0110
0100 ^ 0010 = 0110
0110 | 0010 = 0110
0110 ^ 0010 = 0100
-
Oct 23rd, 2008, 09:57 PM
#6
Re: [2008] The "or" keyword
 Originally Posted by masfenix
i didn't recieve a new PM from you John. its okay though. I suspect your reply included something about the "+=" to add the event handlers.
Hmmm... I went to all the trouble of typing it up and then apparently I didn't send it as it doesn't show up in my Sent Items. Not sure how I managed that. It certainly was about using += to attach event handlers, but you seem to have that in hand.
-
Oct 23rd, 2008, 10:22 PM
#7
Thread Starter
Fanatic Member
Re: [2008] The "or" keyword
Yea I knew about those however i jsut didnt know where to place them and then it hit me.. hey why not place them right after you get the form instance.
Anyways, i've converted them to (int) and it seems to be working now. A bug here or there because of the conversion but everything is in proper order. I've tested it out and its a-okay.
thankyou all.
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|