Author Topic: C#10 not all code working fine  (Read 486 times)

0 Members and 1 Guest are viewing this topic.

daboho

  • Newt
  • Posts: 37
C#10 not all code working fine
« on: August 02, 2022, 08:58:11 AM »
i am has test C#10 in visual studio 2022 in autocad 2019 64 bit but not all code work as well example indexing of array in last of my code
see snapshot
how to make all code work fine in C#10
Code: [Select]
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Autodesk.AutoCAD.ApplicationServices.Core;
using Autodesk.AutoCAD.DatabaseServices;
using Autodesk.AutoCAD.Geometry;
using Autodesk.AutoCAD.Runtime;
using Autodesk.AutoCAD.EditorInput;
using CadApp = Autodesk.AutoCAD.ApplicationServices.Application;
using Autodesk.AutoCAD.DatabaseServices.Filters;
using System.Collections;
using static cadtest22_1.PersonClass;

namespace cadtest22_1
{
 
    //1.test create struct Tipe T
    public struct Foo<T>
    {
        public T Var1;
        public T Var2;
    }
    public struct Point
    {
        public double X { get; set; }
        public double Y { get; set; }
        public double Distance => Math.Sqrt(X * X + Y * Y);

        public override string ToString() =>
            $"({X}, {Y}) is {Distance} from the origin";
    }
 
    //2. test record
    public class PersonClass
    {
        public string Name { get; }
        public string Surname { get; set; }
        public record PersonRecord
        {
            public string Name { get; set; }
            public string Surname { get; set; }

            public PersonRecord(string name, string surname)
            {
                Name = name;
                Surname = surname;
            }
            public void Deconstruct(out string name, out string surname)
            {
                name = Name;
                surname = Surname;
            }
        }
    }

    public class Class1
    {
       
        //3.test call
        int M()
        {
            int y = 5;
            int x = 7;
            return Add(x, y);

            static int Add(int left, int right) => left + right;
        }
       

        [CommandMethod("test")]
        public void test1()
        {
            var doc = CadApp.DocumentManager.MdiActiveDocument;
            var ed = doc.Editor;
            var db = doc.Database;


            //call struct foo
            var d = new Foo<int> { Var1 = 1, Var2 = 2 };          //ok
            var dd = new Foo<double> { Var1 = 1.1, Var2 = 20.1 }; //ok
            CadApp.ShowAlertDialog($"struct foo T {d.Var1} - {d.Var2}");       //ok

            // test switch multiple variable
            string RockPaperScissors(string first, string second)
         => (first, second) switch
         {
             ("rock", "paper") => "rock is covered by paper. Paper wins.",
             ("rock", "scissors") => "rock breaks scissors. Rock wins.",
             ("paper", "rock") => "paper covers rock. Paper wins.",
             ("paper", "scissors") => "paper is cut by scissors. Scissors wins.",
             ("scissors", "rock") => "scissors is broken by rock. Rock wins.",
             ("scissors", "paper") => "scissors cuts paper. Scissors wins.",
             (_, _) => "tie"
         };
            CadApp.ShowAlertDialog($"{RockPaperScissors("rock","paper")}");


            //add function without aggregate,action
            int M()
            {
                int y = 5;
                int x = 7;
                return Add(x, y);

                static int Add(int left, int right) => left + right;
            } //ok

            //test multiple switch
            static int GetTax4(int muncipalityId) => muncipalityId switch
            {
                0 or 1 => 20,
                > 1 and < 5 => 21,
                > 5 and not 7 => 22,
                7 => 23,
                _ => 20
            };
            CadApp.ShowAlertDialog("value switch " + GetTax4(6).ToString());//ok

            // test switch to autocad
            static string ent(Entity myent) => myent.GetType().Name.ToString() switch
            {
               "Line" or "Polyline" or "Circle" => "curve",
               _ => "Not curve"
            };
            var ge = ed.GetEntity("\nclick entiti");
            if (ge.Status != PromptStatus.OK) return;
            using(var tr = db.TransactionManager.StartTransaction())
            {
                var ent1 = tr.GetObject(ge.ObjectId, OpenMode.ForRead) as Entity;
                string result = ent(ent1);
                CadApp.ShowAlertDialog($"value switch {result}"); //ok
           
          // test nullable
            List<int> numbers = null;
            int? i = null;

            numbers ??= new List<int>();
            numbers.Add(i ??= 17);
            numbers.Add(i ??= 20);
                // test nullable in autocad
                var cur = ent1 as Curve;
                Point3d? pt = cur?.StartPoint ?? (cur as Circle)?.Center;
                CadApp.ShowAlertDialog($"value {cur.GetType().Name} {pt}");
            }

            // tes bool function is and or
            bool IsLetter(char c) => c is >= 'a' and <= 'z' or >= 'A' and <= 'Z';
            CadApp.ShowAlertDialog("is true :" + IsLetter('a').ToString());//ok


            var person = new PersonRecord("Silver", "Chariot");
            var (name, surname) = person;

            CadApp.ShowAlertDialog($"name {name} and surname {surname}");
            //testing lambda discard parameter
            Application.BeginDoubleClick += (_,_)=> CadApp.ShowAlertDialog("testing dobule click"); //ok


         [b]   var value = new[] { 10, 11, 12, 13 };
            //this not working system index not import what must to do

            int a = value[^1]; // 13
            int b = value[^2]; // 12
            int c = value[2..3];[/b]
        }


    }
}


57gmc

  • Newt
  • Posts: 160
Re: C#10 not all code working fine
« Reply #1 on: August 02, 2022, 10:17:30 AM »
To program AutoCAD, you won't be able to make use of all the latest features. Start your class project with the template called "Class (.NET)" not the one just called "Class", which targets .NET Core. Review this document on how to create a new project and make a template from it. For 2019, you need to target a specific version of the .NET Framework. There is also a chart in the pdf on which version of the .NET Framework that your 2019 project needs to target.

daboho

  • Newt
  • Posts: 37
Re: C#10 not all code working fine
« Reply #2 on: August 02, 2022, 02:54:54 PM »
What is relation with template
Are you has try that and work in net 5.0
I has add in xml
<langversion>preview<langversion>
And netframework is 4.8
« Last Edit: August 02, 2022, 03:01:36 PM by daboho »

kdub

  • Mesozoic keyThumper
  • SuperMod
  • Swamp Rat
  • Posts: 1470
  • class keyThumper<T>:ILazy<T>
Re: C#10 not all code working fine
« Reply #3 on: August 02, 2022, 04:06:18 PM »
@daboho

.NET Framework 4.8 uses C# 7.3, so your code will be restricted to it's features.
As previously mentioned, AutoCAD requires the .NET Framework, not .Net Core and not .Net 5.0

Records
and static local functions
and recursive patterns
and relational patterns
and coalescing assignment
and index operator
and range operator
and the other good stuff after C# 7.3 aren't available, which is a pain in the butt.

Regards,

added:
This
https://help.autodesk.com/view/OARX/2023/ENU/?guid=GUID-450FD531-B6F6-4BAE-9A8C-8230AAC48CB4
documents the expectations,
though a later version of VS can be used ; ie I'm using VS2022 with AutoCAD 2023 (.net Framework 4.8 )
« Last Edit: August 02, 2022, 04:15:11 PM by kdub »
called Kerry in my other life

Sometimes the question is more important than the answer.

I don't really work crazy hours . . I just live at UTC + 12.00
#ridesober

57gmc

  • Newt
  • Posts: 160
Re: C#10 not all code working fine
« Reply #4 on: August 02, 2022, 05:34:59 PM »
As kdub reiterated, you have to stick with the requirements for your version of AutoCAD, which for 2019 is .NET Framework 4.7. This is shown also in the link kdub provided. As far as the pdf I mentioned, it tells you how to correctly create a new project. It only describes making a template at the end. The problem you have now is that you created a project with the wrong template and you won't be able to target the 4.7 framework. To fix your problem, you need to start a new project using the C# template that has the words "Class (.NET)".

gile

  • Water Moccasin
  • Posts: 2400
  • Marseille, France
Re: C#10 not all code working fine
« Reply #5 on: August 03, 2022, 02:39:03 AM »
Records
and static local functions
and recursive patterns
and relational patterns
and coalescing assignment
and index operator
and range operator
and the other good stuff after C# 7.3 aren't available, which is a pain in the butt.

All these features were available in F# since .NET Framework 2.0.
Speaking English as a French Frog

daboho

  • Newt
  • Posts: 37
Re: C#10 not all code working fine
« Reply #6 on: August 03, 2022, 03:33:03 AM »
@gile thanks for information
@kdub your visual studio  2022 and for
Autocad 2023 whats is latest languange version ?

kdub

  • Mesozoic keyThumper
  • SuperMod
  • Swamp Rat
  • Posts: 1470
  • class keyThumper<T>:ILazy<T>
Re: C#10 not all code working fine
« Reply #7 on: August 03, 2022, 03:37:55 AM »
@gile thanks for information
@kdub your visual studio  2022 and for
Autocad 2023 whats is latest languange version ?

@daboho

.NET Framework 4.8 uses C# 7.3, so your code will be restricted to it's features.
As previously mentioned, AutoCAD requires the .NET Framework, not .Net Core and not .Net 5.0

<< ... >>

added:
This
https://help.autodesk.com/view/OARX/2023/ENU/?guid=GUID-450FD531-B6F6-4BAE-9A8C-8230AAC48CB4
documents the expectations,
though a later version of VS can be used ; ie I'm using VS2022 with AutoCAD 2023 (.net Framework 4.8 )

Regards,
called Kerry in my other life

Sometimes the question is more important than the answer.

I don't really work crazy hours . . I just live at UTC + 12.00
#ridesober