Home > database >  The name 'ascon1' does not exist in the current context // windows form app
The name 'ascon1' does not exist in the current context // windows form app

Time:12-14

so i have created a program using TitaniumAS that connects to an OPC server and Write/Reads data from it. it works fine on console but i wanted to make a windows form app out of it to make it more practical. the thing is i can't access my methods/variables in the events (Button clicks...etc) and it says that the The name does not exist in the current context.

Any suggestions ?

using TitaniumAS.Opc.Client.Common;
using TitaniumAS.Opc.Client.Da;
using TitaniumAS.Opc.Client.Da.Browsing;
using System.Threading;
using System.Threading.Channels;



namespace OPCtests
{

    public partial class Form1 : Form
    {
        public Form1()
        {


            TitaniumAS.Opc.Client.Bootstrap.Initialize();

            Uri url = UrlBuilder.Build("Kepware.KEPServerEX.V6");

            using (var server = new OpcDaServer(url))
            {
                server.Connect();

                OpcDaGroup group = server.AddGroup("MyGroup");
                group.IsActive = true;

                Ascon ascon1 = new Ascon();

                ascon1.ALM = ReadX("Channel1.Ascon1.AsconS");


                void WriteX(String Link, String Ascon)
                {
                    var definition1 = new OpcDaItemDefinition
                    {
                        ItemId = Link,
                        IsActive = true
                    };

                    OpcDaItemDefinition[] definitions = { definition1 };
                    OpcDaItemResult[] results = group.AddItems(definitions);



                    OpcDaItem tag = group.Items.FirstOrDefault(i => i.ItemId == Link);
                    OpcDaItem[] items = { tag };

                    object[] Values = { Ascon };
                    HRESULT[] Results = group.Write(items, Values);

                }
                string ReadX(String Link)
                {
                    var definition1 = new OpcDaItemDefinition
                    {
                        ItemId = Link,
                        IsActive = true
                    };

                    OpcDaItemDefinition[] definitions = { definition1 };
                    OpcDaItemResult[] results = group.AddItems(definitions);
                    OpcDaItemValue[] values = group.Read(group.Items, OpcDaDataSource.Device);

                    return Convert.ToString(values[0].Value);


                }


            }

            InitializeComponent();

        }

       

        void Read_Click(object sender, EventArgs e)
            {
               textBox1.Text = ascon1.ALM;
            }
        
    }
}

CodePudding user response:

Read_Click() method is trying to access ascon1, but this variable is not in the current scope. ascon1 is defined inside the Form1 constructor, but it is not accessible from other methods in the Form1 class. Declare your variable outside of the constructor

CodePudding user response:

You need to put "Ascon ascon1 = new Ascon();" in "public partial class Form1 : Form{ }". Regarding your "CS8422 Static local function cannot contain a reference to ’this‘ or ‘base’" error, it is because you added the "static" modifier after the local function. If your issue is not resolved, please share some information about "Ascon".

  • Related