Olukayode Oluwole
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
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
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.GenerateActiveLo ckStatus() ;
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
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);
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);
}
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.GenerateActiveLo
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
You can return array of string:
result[1]= RegisteredLevel
public string[] ......
{
string[] result = {RegisteredStatus, RegisteredLevel};
return result;
}
result[0] = RegisterStatusresult[1]= RegisteredLevel
Hi,
If you're using C# 7.0 (Visual Studio 2017) there is a new construction to do that:
If you're using C# 7.0 (Visual Studio 2017) there is a new construction to do that:
return (RegisteredStatus: registeredStatus, RegisteredLevel: registeredLevel);
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
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;
}
struct (C# Reference)
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Hi,
You can also go for dynamic object-
Check ExpandoObject
You can also go for dynamic object-
dynamic obj = new ExpandoObject();
obj.Property1 = "hello world";
obj.Property2 = 123;
return obj;
Check ExpandoObject
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
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)
Also No mention was made on how to retrieve the values into UI text boxes
Grateful for further assistance
Olukay
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);
}
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)
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-
Your function definition should be as below-
public (string, string) GenerateActiveLockStatus()
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);
}
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);
}
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
ASKER
Thanks for the help
try declare a class and return that class object instead. For class object, you can define it with objects or properties you wanted.