TheSwamp

Code Red => .NET => Topic started by: jsr on December 25, 2010, 04:23:05 AM

Title: VB.NET to C#
Post 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
Title: Re: VB.NET to C#
Post by: Jeff H on December 25, 2010, 05:22:28 AM
In VB the And is a logical and bitwise operator
Title: Re: VB.NET to C#
Post by: Kerry on December 25, 2010, 07:39:12 PM

(args.Context.History && PointHistoryBits.NotDigitizer);

Title: Re: VB.NET to C#
Post by: jsr on December 26, 2010, 06:03:03 AM
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'.
Title: Re: VB.NET to C#
Post by: gile on December 26, 2010, 07:57:59 AM
Hi,

So, it's a bitwise AND operator: '&' in C#.

Code: [Select]
if (args.Context.History & PointHistoryBits.NotDigitizer == 2) ...
Title: Re: VB.NET to C#
Post by: sinc on December 26, 2010, 10:37:25 AM
You would not want to use the "== 2" part...   That wouldn't work right.
Title: Re: VB.NET to C#
Post by: gile on December 26, 2010, 11:03:57 AM
Quote
You would not want to use the "== 2" part...   That wouldn't work right.

Oopss !

Some casts and parents missing...

Maybe:

Code: [Select]
if (((int)args.Context.History & (int)PointHistoryBits.NotDigitizer) == 2) ...
or (more concise)

Code: [Select]
if (((int)args.Context.History & 2) == 2) ...
Title: Re: VB.NET to C#
Post by: sinc on December 27, 2010, 12:12:40 AM
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:

Code: [Select]
if ((e.Context.History & PointHistoryBits.NotDigitizer) == PointHistoryBits.NotDigitizer)
Or, a bit simpler:

Code: [Select]
if ((e.Context.History & PointHistoryBits.NotDigitizer) > 0)
Title: Re: VB.NET to C#
Post by: It's Alive! on December 27, 2010, 12:27:28 AM
would this work as well?

Code: [Select]
if ((e.Context.History & PointHistoryBits.NotDigitizer) != 0)
Title: Re: VB.NET to C#
Post by: Kerry on December 27, 2010, 01:19:01 AM
jsr
do you have the link to the original source ??
Title: Re: VB.NET to C#
Post by: pkohut on December 27, 2010, 01:34:08 AM
would this work as well?

Code: [Select]
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.
Title: Re: VB.NET to C#
Post by: jsr on December 27, 2010, 01:35:44 AM
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.
Title: Re: VB.NET to C#
Post by: Kerry on December 27, 2010, 02:00:31 AM
did it look something like this ?
Code: [Select]
// 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 ?
Code: [Select]
    ' 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


Title: Re: VB.NET to C#
Post by: gile on December 27, 2010, 02:44:53 AM
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:

Code: [Select]
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:
Title: Re: VB.NET to C#
Post by: It's Alive! on December 27, 2010, 02:48:35 AM
would this work as well?

Code: [Select]
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

Code: [Select]
if(Convert.ToBoolean(e.Context.History & PointHistoryBits.NotDigitizer))
Title: Re: VB.NET to C#
Post by: pkohut on December 27, 2010, 03:01:22 AM
in that case, how about
Code: [Select]
if(Convert.ToBoolean(e.Context.History & PointHistoryBits.NotDigitizer))

Hm, heard something about boats and floating...
Title: Re: VB.NET to C#
Post by: It's Alive! on December 27, 2010, 03:05:43 AM
in that case, how about
Code: [Select]
if(Convert.ToBoolean(e.Context.History & PointHistoryBits.NotDigitizer))

Hm, heard something about boats and floating...


eh :roll:?    just throwing out alternatives
Title: Re: VB.NET to C#
Post by: gile on December 27, 2010, 03:08:05 AM
in that case, how about

Code: [Select]
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.
Title: Re: VB.NET to C#
Post by: It's Alive! on December 27, 2010, 03:14:46 AM
It's just C# is a bit more "type safe" than other languages, I wonder what refector shows in this case...
Title: Re: VB.NET to C#
Post by: pkohut on December 27, 2010, 03:14:55 AM
eh :roll:?    just throwing out alternatives

Understood. The humor must of stopped at keyboard  ;-)
Title: Re: VB.NET to C#
Post by: It's Alive! on December 27, 2010, 03:21:03 AM
Yeah, I better go get myself a few happy pills BRB  :-)
Title: Re: VB.NET to C#
Post by: It's Alive! on December 27, 2010, 03:26:27 AM
Ok chugged a redbull... where was I   :laugh:
Title: Re: VB.NET to C#
Post by: Kerry on December 27, 2010, 04:28:26 AM
That should teach me about not testing before posting.  :oops:
Title: Re: VB.NET to C#
Post by: sinc on December 27, 2010, 01:05:02 PM

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:

Code: [Select]
if ((args.Context.History & PointHistoryBits.NotDigitizer) == 2)
Without explicit parentheses, it gets parsed as:

Code: [Select]
if (args.Context.History & (PointHistoryBits.NotDigitizer == 2))
Title: Re: VB.NET to C#
Post by: gile on December 27, 2010, 05:04:47 PM
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
Title: Re: VB.NET to C#
Post by: Kerry on December 27, 2010, 11:24:07 PM
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
Code: [Select]
   ' 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.
Title: Re: VB.NET to C#
Post by: jgr on December 28, 2010, 08:11:48 AM
This code is not correct because it only works if Option Strict = off. (this is a compiler option)
Code: [Select]
If (e.Context.History And PointHistoryBits.NotDigitizer) Then
This is the right way.
Code: [Select]
If (e.Context.History And PointHistoryBits.NotDigitizer) = PointHistoryBits.NotDigitizer Then

This also works
Code: [Select]
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

...


Title: Re: VB.NET to C#
Post by: Kerry on December 28, 2010, 08:20:40 AM
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 :)

 


Title: Re: VB.NET to C#
Post by: jgr on December 28, 2010, 08:55:28 AM
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