TheSwamp
Code Red => .NET => Topic started by: jsr on December 25, 2010, 04:23:05 AM
-
Hi all
while going through Fenton Web's .NET API video tutorial, He searched for left mouse clicks using following code in an event handler routine .
if(args.Context.History AND PointHistoryBits.NotDigitizer)
I am coding in C# and dont know how to translate above to C# statement. Please guide.
Thanks
-
In VB the And is a logical and bitwise operator
-
(args.Context.History && PointHistoryBits.NotDigitizer);
-
Thanks for the replies,
but I have tried that syntax and getting following error message. Please guide.
// Check to see if mouse has been Clicked.
if(args.Context.History && PointHistoryBits.NotDigitizer)
{
// Create a Circle
Circle c = new Circle(args.Context.RawPoint, Vector3d.ZAxis, 10);
args.Context.DrawContext.Geometry.Draw(c);
System.Threading.Thread.Sleep(50);
// Set the new Radius.
c.Radius = 20;
args.Context.DrawContext.Geometry.Draw(c);
System.Threading.Thread.Sleep(50);
// Set the new Radius.
c.Radius = 40;
args.Context.DrawContext.Geometry.Draw(c);
}
Operator '&&' cannot be applied to operands of type 'Autodesk.AutoCAD.EditorInput.PointHistoryBits' and 'Autodesk.AutoCAD.EditorInput.PointHistoryBits'.
-
Hi,
So, it's a bitwise AND operator: '&' in C#.
if (args.Context.History & PointHistoryBits.NotDigitizer == 2) ...
-
You would not want to use the "== 2" part... That wouldn't work right.
-
You would not want to use the "== 2" part... That wouldn't work right.
Oopss !
Some casts and parents missing...
Maybe:
if (((int)args.Context.History & (int)PointHistoryBits.NotDigitizer) == 2) ...
or (more concise)
if (((int)args.Context.History & 2) == 2) ...
-
Actually, the "== 2" would work right, it's just that the whole point of having an enumeration like PointHistoryBits is so that you don't hard-code enumeration values in your code. (Well, that, and the fact that the enumeration helps "comment" the code.)
So you should use something like this:
if ((e.Context.History & PointHistoryBits.NotDigitizer) == PointHistoryBits.NotDigitizer)
Or, a bit simpler:
if ((e.Context.History & PointHistoryBits.NotDigitizer) > 0)
-
would this work as well?
if ((e.Context.History & PointHistoryBits.NotDigitizer) != 0)
-
jsr
do you have the link to the original source ??
-
would this work as well?
if ((e.Context.History & PointHistoryBits.NotDigitizer) != 0)
Semantic sugar. Sinc's version is clearer to the casual observer by not introducing a negative context into the statement.
-
Thanks everybody for the interest in my post.
Kerry, I saw this code in AutoCAD .NET api tutorial from Fenton Web. It was in VB.NET and what he wrote was
if(args.Context.History AND PointHistoryBits.NotDigitizer)
Now as per the suggestions given yesterday, if I use
if(args.Context.History & PointHistoryBits.NotDigitizer == 2)
compiler says that we cannot compare int with PointHistoryBits.NotDigitizer, so even this version is not working.
Thanks again.
-
did it look something like this ?
// ok, so now lets do something really pointless! space invaders!
if ((e.Context.History & PointHistoryBits.NotDigitizer) > PointHistoryBits.DidNotPick)
{
// this means the mouse has been clicked... so...
// create a Circle
Circle circle = new Circle(e.Context.RawPoint, Vector3d.ZAxis, 10);
// draw it
e.Context.DrawContext.Geometry.Draw(circle);
// wait for a while
System.Threading.Thread.Sleep(50);
// draw
circle.Radius = 20;
e.Context.DrawContext.Geometry.Draw(circle);
// wait for a while
System.Threading.Thread.Sleep(50);
// draw it
circle.Radius = 40;
e.Context.DrawContext.Geometry.Draw(circle);
// wait for a while
System.Threading.Thread.Sleep(50);
// draw it
circle.Radius = 80;
e.Context.DrawContext.Geometry.Draw(circle);
// wait for a while
System.Threading.Thread.Sleep(50);
}
ie:
was it related to this http://www.theswamp.org/index.php?topic=19780.0
Was the original something like this ?
' ok, so now lets do something really pointless! space invaders!
If (e.Context.History And PointHistoryBits.NotDigitizer) Then
' this means the mouse has been clicked... so...
' create a Circle
Dim circle As Circle = New Circle(e.Context.RawPoint, Vector3d.ZAxis, 10)
' draw it
e.Context.DrawContext.Geometry.Draw(circle)
' wait for a while
System.Threading.Thread.Sleep(50)
' draw it
circle.Radius = 20
e.Context.DrawContext.Geometry.Draw(circle)
' wait for a while
System.Threading.Thread.Sleep(50)
' draw it
circle.Radius = 40
e.Context.DrawContext.Geometry.Draw(circle)
' wait for a while
System.Threading.Thread.Sleep(50)
' draw it
circle.Radius = 80
e.Context.DrawContext.Geometry.Draw(circle)
' wait for a while
System.Threading.Thread.Sleep(50)
End If
-
OK, the (corrected) solutions I posted are verbose and not very elegant.
I learned about Enum using...
Another thing about the code contained in the 'then' statement: IMO the created circle (which isn't added to the database) have to be explicitly disposed.
Use this:
if ((e.Context.History & PointHistoryBits.NotDigitizer) == PointHistoryBits.NotDigitizer)
{
// this means the mouse has been clicked... so...
// create a Circle
using (Circle circle = new Circle(e.Context.RawPoint, Vector3d.ZAxis, 10))
{
// draw it
e.Context.DrawContext.Geometry.Draw(circle);
// wait for a while
System.Threading.Thread.Sleep(50);
// draw
circle.Radius = 20;
e.Context.DrawContext.Geometry.Draw(circle);
// wait for a while
System.Threading.Thread.Sleep(50);
// draw it
circle.Radius = 40;
e.Context.DrawContext.Geometry.Draw(circle);
// wait for a while
System.Threading.Thread.Sleep(50);
// draw it
circle.Radius = 80;
e.Context.DrawContext.Geometry.Draw(circle);
// wait for a while
System.Threading.Thread.Sleep(50);
} // Dispose the Circle
}
To avoid this:
-
would this work as well?
if ((e.Context.History & PointHistoryBits.NotDigitizer) != 0)
Semantic sugar. Sinc's version is clearer to the casual observer by not introducing a negative context into the statement.
in that case, how about
if(Convert.ToBoolean(e.Context.History & PointHistoryBits.NotDigitizer))
-
in that case, how about
if(Convert.ToBoolean(e.Context.History & PointHistoryBits.NotDigitizer))
Hm, heard something about boats and floating...
-
in that case, how about
if(Convert.ToBoolean(e.Context.History & PointHistoryBits.NotDigitizer))
Hm, heard something about boats and floating...
eh :roll:? just throwing out alternatives
-
in that case, how about
if(Convert.ToBoolean(e.Context.History & PointHistoryBits.NotDigitizer))
This seems to be closer to the VB syntax. It seems to me, VB can have non explcit casts.
-
It's just C# is a bit more "type safe" than other languages, I wonder what refector shows in this case...
-
eh :roll:? just throwing out alternatives
Understood. The humor must of stopped at keyboard ;-)
-
Yeah, I better go get myself a few happy pills BRB :-)
-
Ok chugged a redbull... where was I :laugh:
-
That should teach me about not testing before posting. :oops:
-
Now as per the suggestions given yesterday, if I use
if(args.Context.History & PointHistoryBits.NotDigitizer == 2)
compiler says that we cannot compare int with PointHistoryBits.NotDigitizer, so even this version is not working.
That's because of operator precedence. You would need to do something like the following:
if ((args.Context.History & PointHistoryBits.NotDigitizer) == 2)
Without explicit parentheses, it gets parsed as:
if (args.Context.History & (PointHistoryBits.NotDigitizer == 2))
-
After some tests:
((args.Context.History & PointHistoryBits.NotDigitizer) == 2) => does not work
((e.Context.History & PointHistoryBits.NotDigitizer) > 0) => does not work
((e.Context.History & PointHistoryBits.NotDigitizer) != 0)) => works
(((int)e.Context.History & (int)PointHistoryBits.NotDigitizer) == 2) => works
(((int)e.Context.History & 2) == 2) => works
((e.Context.History & PointHistoryBits.NotDigitizer) == PointHistoryBits.NotDigitizer) => works
((e.Context.History & PointHistoryBits.NotDigitizer) > PointHistoryBits.DidNotPick) => works
(Convert.ToBoolean(e.Context.History & PointHistoryBits.NotDigitizer)) => works
-
Just to confuse the iisue more,
The VB code as written is massaged behind the scenes and the decision logic is changed ... one of the reasons ( no, we won't go there today :) )
This is the VB code
' ok, so now lets do something really pointless! space invaders!
If (e.Context.History And PointHistoryBits.NotDigitizer) Then
' this means the mouse has been clicked... so...
' create a Circle
Dim circle As Circle = New Circle(e.Context.RawPoint, Vector3d.ZAxis, 10)
' draw it
e.Context.DrawContext.Geometry.Draw(circle)
' wait for a while
System.Threading.Thread.Sleep(50)
' draw it
circle.Radius = 20
e.Context.DrawContext.Geometry.Draw(circle)
' wait for a while
System.Threading.Thread.Sleep(50)
' draw it
circle.Radius = 40
e.Context.DrawContext.Geometry.Draw(circle)
' wait for a while
System.Threading.Thread.Sleep(50)
' draw it
circle.Radius = 80
e.Context.DrawContext.Geometry.Draw(circle)
' wait for a while
System.Threading.Thread.Sleep(50)
End If
These are snaphots into the compiled code in Reflector // both the VB and C# representation of the Intermediate Langusge in the compiled DLL
note : the piccys will only be visible to members logged-in.
-
This code is not correct because it only works if Option Strict = off. (this is a compiler option)
If (e.Context.History And PointHistoryBits.NotDigitizer) Then
This is the right way.
If (e.Context.History And PointHistoryBits.NotDigitizer) = PointHistoryBits.NotDigitizer Then
This also works
If (CInt(e.Context.History) And Cint(PointHistoryBits.NotDigitizer)) = 64 Then
If (CInt(e.Context.History) And 64) = 64 Then
If CBool(e.Context.History And PointHistoryBits.NotDigitizer) Then
...
-
This code is not correct because it only works if Option Strict = off. (this is a compiler option)
< .. >
That statement is debatable.
Option Strict = off is an available Option .. so the code is correct in the form it's used.
The real discussion should be "why is Option Strict available as an Option, and should it be used at all"
.. but I won't participate 'cause I'm prejudiced :-P
.. just my opinion :)
-
Option Strict = off is an available Option .. so the code is correct in the form it's used.
The real discussion should be "why is Option Strict available as an Option, and should it be used at all"
It is an inheritance from vb6. This option does not exist in vb6 but internally is always off.
Option Strict On provides strong typing.
http://msdn.microsoft.com/en-us/library/zcd4xwzs%28v=VS.100%29.aspx