Home > Mobile >  Methods return type in C#
Methods return type in C#

Time:12-15

i am using a method to retrieve data from an OPC DA server using TitaniumAS packages, the problem i am having is that i have a lot of tags to read/write so i have to use methods. The WriteX method works fines as it doesnt have to return anything but the read does not, well it does its job, it reads but i cannot use that data outside of the method because it was a void method, when i tried to use it as a String method (that's the type of data i need) it says :

Error CS0161 'ReadX(string, string)': not all code paths return a value
PS : note that i am just a beginner in C#

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using TitaniumAS.Opc.Client.Common;
using TitaniumAS.Opc.Client.Da;
using TitaniumAS.Opc.Client.Da.Browsing;
using System.Threading;
using System.Threading.Channels;
using Async;

namespace OPCDA
{
    class Program
    {
        static void Main(string[] args)
        {

            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();
               
               ReadX("Channel1.Ascon1.AsconS", ascon1.ALM);
               Console.WriteLine("value = {0}", ascon1.ALM);

                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, String read)
                {
                    var definition1 = new OpcDaItemDefinition
                    {
                        ItemId = Link,
                        IsActive = true
                    };

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

                    read = Convert.ToString(values[0].Value);
                   
                    
                }
            }

        }
    }
}

CodePudding user response:

the first step was to state the return like this : return Convert.ToString(values[0].Value) instead of read = Convert.ToString(values[0].Value)

then go up and use that value with my variable : ascon1.ALM=ReadX("Channel1.Ascon1.AsconS");

  • Related