Link to home
Start Free TrialLog in
Avatar of Olukayode Oluwole
Olukayode OluwoleFlag for Canada

asked on

How can i return 2 values from a class library into my User Inteface

I have a c#  application and need to return 2 items from a class  library

when i had 1 item i was able to return it but some how i am having error returning 2 items

The 2  items i want returned are   RegisteredStatus  and RegisteredLevel

Both variables  were defined as  strings  ie

string RegisteredStatus = "";
string RegisteredLevel ="";

After  some processing  in the script ,  I have values in both variables which i want returned.

1. Part of my code in the Library

  MyActiveLock.KeyStoreType = ActiveLock3_6NET.IActiveLock.LicStoreType.alsFile;


            MyActiveLock.Acquire(ref strMsg, ref strRemainingTrialDays, ref strRemainingTrialRuns, ref strTrialLength,
                   ref strUsedDays, ref strExpirationDate, ref strRegisteredUser, ref strRegisteredLevel, ref strLicenseClass,
                   ref strMaxCount, ref strLicenseFileType, ref strLicenseType, ref strUsedLockType);

            if (strMsg != null && strMsg.Length > 0) //There's a trial
            {
                RegisteredStatus = "Trial Mode";
                RegisteredLevel = strRegisteredLevel;
            }
            else
            {

                //cmdKillTrial.Visible = false;
                //cmdResetTrial.Visible = false;
                RegisteredStatus = "Registered";
                RegisteredLevel = strRegisteredLevel;        
            }
            return (RegisteredStatus, RegisteredLevel);

Open in new window


Return RegisteredStatus   worked  fine,  while Return (RegisteredStatus, RegisteredLevel) gave a syntax error

I  introduced  the Tuple below into library hoping it would help me return the 2 values

But i think i might not be calling/using the tuple properly  (see Below the tuple in the library)

2. A Tuple also defined in the Library

 
public static Tuple<string, string> Location(string RegisteredStatus, string RegisteredLevel)
        {
            return new Tuple<string, string>(RegisteredStatus, RegisteredLevel);
        }

Open in new window



3. Eventually in my application i need to get the 2 returned variables in to

some text field on the screen .

For the 1 variable   which worked  my script was

 ActiveLockProcessor processor = new ActiveLockProcessor();
 txtRegStatus.Text = processor.GenerateActiveLockStatus();

What would be the correct syntax now for getting the 2 variables to my Text fields on screen

Please NOTE that i do not have to use Tuples if there is a simpler and more straight way to achieve this.

Thanks

Olukay
Avatar of Ryan Chong
Ryan Chong
Flag of Singapore image

Return RegisteredStatus   worked  fine,  while Return (RegisteredStatus, RegisteredLevel) gave a syntax error

try declare a class and return that class object instead. For class object, you can define it with objects or properties you wanted.
You can return array of string:
   public string[] ......
      {
             string[] result = {RegisteredStatus, RegisteredLevel};
              return result;
         }

Open in new window

result[0] = RegisterStatus
result[1]= RegisteredLevel
Hi,

If you're using C# 7.0 (Visual Studio 2017) there is a new construction to do that:

return (RegisteredStatus: registeredStatus, RegisteredLevel: registeredLevel);

Open in new window

you can pass the strings registeredStatus and registeredLevel as arguments to your function by reference. then the function could assign the values and return a bool for example to signal success.

Sara
Or, indeed if you in general use this pair of info,  you could create and use a tiny Struct like:

public struct RegisteredInfo
{
    public string Status;
    public string Level;
}

Open in new window

struct (C# Reference)
ASKER CERTIFIED SOLUTION
Avatar of Chris Stanyon
Chris Stanyon
Flag of United Kingdom of Great Britain and Northern Ireland image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Hi,

You can also go for dynamic object-

dynamic obj = new ExpandoObject();
obj.Property1 = "hello world";
obj.Property2 = 123;
return obj;

Open in new window


Check ExpandoObject
Avatar of Olukayode Oluwole

ASKER

I have tried the solution that is a 1 liner  (i.e  by Rikin Shah) because of its simplicity

return (RegisteredStatus: registeredStatus, RegisteredLevel: registeredLevel);

The line was how flagged as an error.

Below please find my full method including the declaration  of RegisteredStatus  and RegisteredLevel

{
    public class ActiveLockProcessor
    {
        private object strUsedLockType;
        private string strKeyStorePath;
        private string strAutoRegisterKeyPath;
        string autoRegisterKey = null;
        bool boolAutoRegisterKeyPath = false;
        string[] A;
        // public object MyActiveLock { get; private set; }
        //public _IActiveLock MyActiveLock1 { get => this.MyActiveLock; set => this.MyActiveLock = value; }

        //private _IActiveLock MyActiveLock;
        private ActiveLockEventNotifier ActiveLockEventSink;
        private _IActiveLock MyActiveLock;
        private object modMain;

        [DllImport("kernel32.dll")]
        static extern uint GetSystemDirectory([Out] StringBuilder lpBuffer, uint uSize);
        [DllImport("shell32.dll")]
        static extern bool SHGetSpecialFolderPath(IntPtr hwndOwner, [Out] StringBuilder lpszPath, int nFolder, bool fCreate);

        internal const string SOFTWARENAME = "Crystal";

        string RegisteredStatus = "";
        string RegisteredLevel = "";

        public string GenerateActiveLockStatus()
        {
  
            string activelockFile = Application.StartupPath + @"\Activelock3_6NET.dll";

            string strMsg = null;
            string strRemainingTrialDays = null, strRemainingTrialRuns = null, strTrialLength = null, strUsedDays = null, strExpirationDate = null,
                strRegisteredUser = null, strRegisteredLevel = null, strLicenseClass = null, strMaxCount = null, strLicenseFileType = null,
                strLicenseType = null, strUsedLockType = null;
            //string RegisteredStatus;


            StringBuilder sbAppfilePath = new StringBuilder(260);
            SHGetSpecialFolderPath(IntPtr.Zero, sbAppfilePath, 46, false); // 46 is for ...\All Users\Documents folder.
            string AppfilePath = sbAppfilePath.ToString();

            Globals MyAL = new Globals();
            MyActiveLock = MyAL.NewInstance();

            //MyActiveLock.SoftwareName = modMain.SOFTWARENAME;
            MyActiveLock.SoftwareName = "Crystal";
            MyActiveLock.SoftwareVersion = "8";
            //MyActiveLock.SoftwareName = "TestApp";
            //MyActiveLock.SoftwareVersion = "3";
            MyActiveLock.SoftwarePassword = Convert.ToChar(99).ToString() + Convert.ToChar(111).ToString() + Convert.ToChar(111).ToString() + Convert.ToChar(108).ToString();
            string PUB_KEY = "";
            // Below is PUB_KEY  for TestApp  Version 3
            PUB_KEY = "RSA1024<RSAKeyValue><Modulus>yEN14ITfjgrvHBcctrYVAPL/f1vLiHCob30+w45h0csbWAM/cbu9MYszZT0H+i23sSKKgmpGcrq2V1oYWOBLSNOakTnvYVYyMNF/EIy+J9LEUaBsjEh22jDgTOoPhPU6+yUhk8u9TgWeDSpvN4ob360Ho3OYdvAz5ZhPLI/Qamk=</Modulus><Exponent>AQAB</Exponent></RSAKeyValue>";


            //// Below is PUB_KEY  for Crystal  Version 8
            //PUB_KEY = "RSA1024<RSAKeyValue><Modulus>ve8rCNEU6MuyBedGQNk68ASxKQ505eOvUI1CUNcHOg44FZxzDVAzFo8I/w93S+jKoiTOKycrtgkV0BFrm/4zrTOiN8fU1ZFgH7TsBznWG+aY8yOIzL/UuUQL8Uj05TEnxfCsXr/LUPqITq7SnfOdI2Ze72qBsNDQc1B243t6RN0=</Modulus><Exponent>AQAB</Exponent></RSAKeyValue>";
            //MyActiveLock.SoftwareCode = modMain.Dec(modMain.PUB_KEY);
            MyActiveLock.SoftwareCode = PUB_KEY;

            strKeyStorePath = Application.StartupPath + @"\Crystal.lic";
            MyActiveLock.Init(Application.StartupPath, ref strKeyStorePath);
            System.Diagnostics.Debug.WriteLine("License path is " + strKeyStorePath);
            MyActiveLock.KeyStorePath = strKeyStorePath;
            // Obtain the EventNotifier so that we can receive notifications from AL.
            ActiveLockEventSink = MyActiveLock.EventNotifier;




            if (!Directory.Exists(AppfilePath + @"\" + MyActiveLock.SoftwareName + MyActiveLock.SoftwareVersion))
                Directory.CreateDirectory(AppfilePath + @"\" + MyActiveLock.SoftwareName + MyActiveLock.SoftwareVersion);
            {
                strAutoRegisterKeyPath = AppfilePath + @"\" + MyActiveLock.SoftwareName + MyActiveLock.SoftwareVersion +
                    @"\" + MyActiveLock.SoftwareName + MyActiveLock.SoftwareVersion + ".all";
                MyActiveLock.AutoRegisterKeyPath = strAutoRegisterKeyPath;
            }
            if (File.Exists(strAutoRegisterKeyPath))
            {
                boolAutoRegisterKeyPath = true;
            }


            MyActiveLock.KeyStoreType = ActiveLock3_6NET.IActiveLock.LicStoreType.alsFile;


            MyActiveLock.Acquire(ref strMsg, ref strRemainingTrialDays, ref strRemainingTrialRuns, ref strTrialLength,
                   ref strUsedDays, ref strExpirationDate, ref strRegisteredUser, ref strRegisteredLevel, ref strLicenseClass,
                   ref strMaxCount, ref strLicenseFileType, ref strLicenseType, ref strUsedLockType);

            if (strMsg != null && strMsg.Length > 0) //There's a trial
            {
                RegisteredStatus = "Trial Mode";
                RegisteredLevel = strRegisteredLevel;
            }
            else
            {

                //cmdKillTrial.Visible = false;
                //cmdResetTrial.Visible = false;
                RegisteredStatus = "Registered";
                RegisteredLevel = strRegisteredLevel;        // 24 in value if registered
            }
            return RegisteredStatus;
            return (RegisteredStatus: registeredStatus, RegisteredLevel: registeredLevel);
        }

Open in new window



This first single parameter return did not raise any error.
When i replaced it with the 2 parameter  line the line was flagged (see  screen shot below)

User generated image
Also No mention was made on how to retrieve the values into UI   text boxes

Grateful   for further assistance

Olukay
Hi

Your function definition should be as below-

public (string, string) GenerateActiveLockStatus()

Open in new window

Have a read through my previous comment. Your method declaration needs to match the return types. If you just want to return a tuple, thne you declaration looks like this:

public (string, string) GenerateActiveLockStatus()
{
    ...
    return (registeredStatus, registeredLevel);
}

Open in new window

If you want to return a named tuple, then it will look like this:

public (string status, string level) GenerateActiveLockStatus()
{
    ...
    return (status : registeredStatus, level : registeredLevel);
}

Open in new window

In the first method the result will contain 2 properties called Item1 and Item2. In the second example the results will contain properties called status and level
Thanks for the help