Author Topic: VB.NET to C#  (Read 8663 times)

0 Members and 1 Guest are viewing this topic.

jsr

  • Guest
VB.NET to C#
« 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

Jeff H

  • Needs a day job
  • Posts: 6150
Re: VB.NET to C#
« Reply #1 on: December 25, 2010, 05:22:28 AM »
In VB the And is a logical and bitwise operator

Kerry

  • Mesozoic relic
  • Seagull
  • Posts: 11654
  • class keyThumper<T>:ILazy<T>
Re: VB.NET to C#
« Reply #2 on: December 25, 2010, 07:39:12 PM »

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

kdub, kdub_nz in other timelines.
Perfection is not optional.
Everything will work just as you expect it to, unless your expectations are incorrect.
Discipline: None at all.

jsr

  • Guest
Re: VB.NET to C#
« Reply #3 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'.

gile

  • Gator
  • Posts: 2507
  • Marseille, France
Re: VB.NET to C#
« Reply #4 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) ...
Speaking English as a French Frog

sinc

  • Guest
Re: VB.NET to C#
« Reply #5 on: December 26, 2010, 10:37:25 AM »
You would not want to use the "== 2" part...   That wouldn't work right.

gile

  • Gator
  • Posts: 2507
  • Marseille, France
Re: VB.NET to C#
« Reply #6 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) ...
Speaking English as a French Frog

sinc

  • Guest
Re: VB.NET to C#
« Reply #7 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)

It's Alive!

  • Retired
  • Needs a day job
  • Posts: 8691
  • AKA Daniel
Re: VB.NET to C#
« Reply #8 on: December 27, 2010, 12:27:28 AM »
would this work as well?

Code: [Select]
if ((e.Context.History & PointHistoryBits.NotDigitizer) != 0)

Kerry

  • Mesozoic relic
  • Seagull
  • Posts: 11654
  • class keyThumper<T>:ILazy<T>
Re: VB.NET to C#
« Reply #9 on: December 27, 2010, 01:19:01 AM »
jsr
do you have the link to the original source ??
kdub, kdub_nz in other timelines.
Perfection is not optional.
Everything will work just as you expect it to, unless your expectations are incorrect.
Discipline: None at all.

pkohut

  • Bull Frog
  • Posts: 483
Re: VB.NET to C#
« Reply #10 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.
New tread (not retired) - public repo at https://github.com/pkohut

jsr

  • Guest
Re: VB.NET to C#
« Reply #11 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.

Kerry

  • Mesozoic relic
  • Seagull
  • Posts: 11654
  • class keyThumper<T>:ILazy<T>
Re: VB.NET to C#
« Reply #12 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


« Last Edit: December 27, 2010, 02:15:41 AM by Kerry »
kdub, kdub_nz in other timelines.
Perfection is not optional.
Everything will work just as you expect it to, unless your expectations are incorrect.
Discipline: None at all.

gile

  • Gator
  • Posts: 2507
  • Marseille, France
Re: VB.NET to C#
« Reply #13 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:
« Last Edit: December 28, 2010, 05:41:27 PM by gile »
Speaking English as a French Frog

It's Alive!

  • Retired
  • Needs a day job
  • Posts: 8691
  • AKA Daniel
Re: VB.NET to C#
« Reply #14 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))