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

asked on

How do i move a method into a library and call it from any where in my application

In my c# application i have a method  as  below;

 
private void GetLicenseStatus()
        {
            string libregstatus = txtRegStatus.Text;
            string libreglevel = txtRegisteredLevel.Text;
            string liblicensetype = txtLicenseType.Text;
        }

Open in new window


When i called the method   with  GetLicenseStatus();  I got the required results in the 3 string variables.

Now I want to comment out the method and put in a class  library  so that the 3 string variable values will be
available to other parts of my application.  Below see  the class library definition.

2. Movement of Method to Class Library

namespace DRYDemoLibrary
{
   public  class RevisedActiveLockProcessor
   {
        public string GetLicenseStatus(regstat,reglev,reglicense)
        {
            string libregstatus = txtRegStatus;
            string libreglevel = txtRegisteredLevel;
            string liblicensetype = txtLicenseType;
        }
    }
}

Open in new window


Below  is the error screen i got when trying to get the method into a library

User generated image


What  am i doing wrong and how do i move  this local method into  a class library
with the appropriate call statement from other parts of my application

Thanks

Olukay
Avatar of Russ Suter
Russ Suter

Your function declaration does not include types for the parameters. It should look like this instead...
public string GetLicenseStatus(string regstat, string reglev, string reglicense)

Open in new window

Avatar of Olukayode Oluwole

ASKER

I have modified  the script a little bit now to use tuples:

So this is what  i have

1. In my application form  the method and the call  are:

private void GetLicenseStatus()
        {
            string libregstatus = txtRegStatus.Text;
            string libreglevel = txtRegisteredLevel.Text;
            string liblicensetype = txtLicenseType.Text;
        }

Open in new window


and the call to it  is GetLicenseStatus();

2. In the class library  i have :

namespace DRYDemoLibrary
{
    public class RevisedActiveLockProcessor
    {
        public Tuple<string, string,string> GetLicenseStatus(string regstat, string reglev, string reglicense)
        {
            string libregstatus = regstat;
            string libreglevel = reglev;
            string liblicensetype = reglicense;
            return new Tuple<string, string, string>(libregstatus,libreglevel,liblicensetype);
        }
    }
}

Open in new window


3. There does not appear to be any syntax issues.  So can you plaese tell me what the new call
to the library  will be

and  how to access  the called items in my application

Thanks

Olukay
In this case you will need to create an instance of your class then call the method.
using DRYDemoLibrary;
...
{
RevisedActiveLockProcessor classInstance = new RevisedActiveLockProcessor();
Tuple<string, string, string> output = classInstance.RevisedActiveLockProcessor(regstat, reglev, reglicense);

Open in new window

It's not really clear what you are trying to access, but it seems that you are accessing fields in the form of your application. The problem with trying to move that into a method in a library is that the library doesn't automatically have any reference to your form. One way to do that would be to supply a reference to the form to the class, so that it can access the fields.

Another problem is to access the values in a reasonable way. In your code you are trying to use a tuple to the return the strings. That would work, but then you just have three strings named Item1, Item2 and Item3, which is not particularly informative. Perhaps it would be better to have three properties in the class instead of a method returning three values.

Your library method could look like this:

namespace DRYDemoLibrary
{
   public class RevisedActiveLockProcessor
   {
        // Local variable to keep the reference to the main form, use the type of your form here:
        private MainForm _form;

        // Constructor to set up the object with a reference to the main form, use the type of your form for the parameter:
        public RevisedActiveLockProcessor(MainForm form) {
            _form = form;
        }

        // Properties to access form values:

        public string RegStatus {
            get {
                return _form.txtRegStatus.Text;
            }
        }

        public string RegLevel {
            get {
                return _form.txtRegisteredLevel.Text;
            }
        }

        public string LicenseType {
            get {
                return _form.txtLicenseType.Text;
            }
        }

    }
}

Open in new window


To use the class you would first create an instance of it in your main program. By supplying this in the call you give the object a reference to the main form:

RevisedActiveLockProcessor processor = new RevisedActiveLockProcessor(this);

Open in new window


Now you can pass this obejct to any code you like, and that code can access the fields in the form:

string status = processor.RegStatus;
string level = processor.RegLevel;

Open in new window


(Of course you don't need to place each value in a variable, you can just use a property like processor.RegStatus anywhere in your code where you need the value.)

I'm not sure if this is the best way to accomplish what you are trying to do, but at least it should provide you some information on how fields are accessible and how to pass references around in your application.
Please see  the code below on the formload event  where i need the variables

private void UserProfileForm_Load(object sender, EventArgs e)
        {
            RevisedActiveLockProcessor classInstance = new RevisedActiveLockProcessor();
            Tuple<string, string, string> output = classInstance.RevisedActiveLockProcessor(regstat, reglev, reglicense);
            var result = classInstance.GetLicenseStatus();
            txtRegStatus.Text = output.Item1;
            txtRegLevel.Text = output.Item2;
            this.Hide();
        }

Open in new window


and the error   screen is

User generated image
There are  a number of issues

1.  At what stage is the data  moved from the first form to the library  and whats the syntax for the
call  to enable the class library get the data

The GetLicenseStatus();   call i guess  should pass the parameters to the library.  If this is correct
what would be  the syntax for this.

2. The values being passed   are not entered  manually on the screen .  They are extracted from activelock.dll

and as confirmed before they actually get extracted  into the variables in the originating form

Will i  in that case still need input parameters into the tuple

Grateful for your assistance.

Olukay
I am thinking a way to solve this would be by using teamviewer.

I have a copy and if anyone  has a copy and would like to help

i will be grateful to have  a suitable time

Thanks


Olukay
I think that using a library is an unneccessary step for getting the information from one form to another. You could just declare properties in the user profile form, and put the values into those properties when you create the form.

Put some properties in the user profile form:

public string RegStatus { get; set; }
public string RegLevel { get; set; }
public string LicenseType { get; set; }

Open in new window


In the main form when you create the user profile form, put the values into the properties. Example:

var userForm = new UserProfileForm();
userForm.RegStatus = txtRegStatus.Text;
userForm.RegLevel = txtRegisteredLevel.Text;
userForm.LicenseType = txtLicenseType.Text;
userForm.ShowDialog(); // or however you show it

Open in new window


Now, when the user profile form opens you can access the values using the properties.
This solution is assuming that I enter the parameters  and i am just transfering it to another form.

The information is coming from activelock.dll

When i display my license form ( from within the application) these values get pull up to the screen.  

See  below a typical form with values. (The values are not manually entered but pulled out from activelock.dll)


User generated image
Ordinarily when a user tries to login these  values are pull from activelock.dll  to validate

whether a license is valid or not.  These  are the  values i am trying to get programatically during login

So do you think this solution will still hold  ??

What do you advise

Olukay
ASKER CERTIFIED SOLUTION
Avatar of Göran Andersson
Göran Andersson
Flag of Sweden 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
Goran ,

Thanks very much for your input.

I have been knocked off my original post by too many posts and advises

that have led to too many changes. .

I will kindly appreciate if you will send me the codes  that you finally got to work

and where i should put them in my application.  Thanks

Olukay
Hello  Goran,

I am not getting the same results  you are  getting
Please see the code below:

1. License Form Code

  try
                {
                    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);

                    // strMsg is to get the trial status
                    // All other parameters are Optional and you can actually get all of them
                    // using MyActivelock.Property usage, but keep in mind that 
                    // doing so will check the license every time making this a time consuming 
                    // way of reading those properties
                    // The fastest approach is to use the arguments from Acquire() method.
                    if (strMsg != null && strMsg.Length > 0) //There's a trial
                    {
                        A = strMsg.Split(new char[] { Convert.ToChar(13) });
                        txtRegStatus.Text = A[0];
                        txtUsedDays.Text = A[1].Replace("\n", "");
                        SetFunctionalities(true);
                        frmSplash mfrmsplash = new frmSplash();
                        mfrmsplash.lblInfo.Text = "\r\n" + strMsg;
                        mfrmsplash.Visible = true;
                        mfrmsplash.Refresh();
                        Thread.Sleep(3000); //wait about 3 seconds
                        mfrmsplash.Close();
                        cmdKillTrial.Visible = true;
                        cmdResetTrial.Visible = true;
                        txtLicenseType.Text = "Free Trial";
                       // this.Refresh();
                        //return; 
                    }
                    else
                    {
                        cmdKillTrial.Visible = false;
                        cmdResetTrial.Visible = false;
                    }
                }
                catch (Exception)
                {

                    throw;
                }
                
                // If you are here already, that means you have a valid license.
                // Set the textboxes in your app accordingly.
                txtRegStatus.Text = "Registered";
                txtUsedDays.Text = strUsedDays;
                txtExpiration.Text = strExpirationDate;

                //----------------------------  Implementing Suggestion by Goran-----
                var userForm = new UserProfileForm();
                userForm.RegStatus = txtRegStatus.Text;
                userForm.RegLevel = strRegisteredLevel;
                userForm.LicenseType = strLicenseType;

                //----------------------------------

Open in new window


You will see your last suggestion reflected there  and when debugged i got the values i wanted  as below

RegStatus = "Registered"
RegLevel = "24"

see  screen below

User generated image
Now  when i now go to my  UserProfileForm (Which is my  Login form)  RegStatus returned null
even though I defined the variables below

public string RegStatus { get; set; }
public string RegLevel { get; set; }
public string LicenseType { get; set; }


See  Screen below

User generated image
Where  i think the problem is coming is that in my License Form  you observer that
there is a MyActiveLock.Acquire   statement.  This is the statement that get the values out of
activelock.dll

Now on the Loginform  Ithink MyactiveLock.Acquire needs to be called before it will return values
to those variables.

If this position is  correct how do I call  MyactiveLock.Acquire  in the License Form

2. By any chance do you have Teamviewer so you can help me to resolve this problem realtime.
It is free from their web site ( since i have a license to connect )

Thanks for your patience and effort

Olukay
Somehow i am not able to replicate what you have done.

Since you claim it worked for you, i will temporarily try to close this thread

I think the issue for me is getting the parameters  out of the activelock.dll.

If like i suggested , you are able to link up on Teamviewer i will very much appreciate it

Thanks

Olukay
As you call MyactiveLock.Acquire in the license form, that will put the values in the properties of the login form, so you don't need to call MyactiveLock.Acquire from the login form also.

As you successfully placed the values in the properties of the login form, but the values are null when you try to access them in the form, that suggests that you have two separate instances of that form.

In the code that I suggested there is code to create an instance of the login form, and that instance will have the properties set with the correct values. If you then create another instance of the login form in the code that opens it, that instance will not have the properties set. You have to use the same instance of the form when you set the properties as when you open the login form. If the code that opens the login form is in another method, you could declare the userForm variable in the license form, so that you can access the variable from both methods.