0

I am following a code sample in this link.

I hit a snag right on the second line:

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
//using EnvDTE;

namespace TwinCAT_Automation_Interface
{
    public partial class Main : Form
    {
        public Main()
       {
            InitializeComponent();
        }

        //Creating TwinCAT Projects via templates (Recommended)
        Type        myType  =   System.Type     .GetTypeFromProgID  ("VisualStudio.DTE.12.0");
        dynamic     myDTE   =   System.Activator.CreateInstance     (myType);  //error right here
    }
}

The error says:

Error 1 A field initializer cannot reference the non-static field, method, or property 'TwinCAT_Automation_Interface.Main.myType'

What exactly am I doing wrong? It's the same code snippet; I just modify it a bit. Please help!!!!

OK, I got it fixed by changing it to the following:

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
//using EnvDTE;

namespace TwinCAT_Automation_Interface
{
    public partial class Main : Form
    {
        public Main()
       {
            InitializeComponent();
        }

        public void myMethod()
        {
            //Creating TwinCAT Projects via templates (Recommended)
            Type        myType  =   System.Type     .GetTypeFromProgID  ("VisualStudio.DTE.12.0");
            dynamic     myDTE   =   System.Activator.CreateInstance     (myType);                   // dynamic linking for DTE-object
        }
    }
}

The explanation is in this link. It is a compiler error.

Community
  • 1
  • 1
CaTx
  • 1,421
  • 4
  • 21
  • 42

1 Answers1

0

It is a compiler error. See question for link to the answer.

CaTx
  • 1,421
  • 4
  • 21
  • 42