Click here to Skip to main content
15,895,370 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
Hi,

I am trying to set a integer value in a class from license information, and then get that value passed back to my Program.cs class (e.g. of value 10).

However when I try to return the value to another part of the class (as below) it says 'Cannot implicitly convert type to Manco.Licensing.License'. Also any idea how do I call this class to get this integer value back to Program.cs ie should this be LCheck.Value.days? In this class daysleft is a integer value.
C#
using System;

    public class LCheck
    {
        private Manco.Licensing.License m_License;
        public static int daysleft;
        public static int days;

        public int Value
        {
            get
            {
                return days;
            }

        }

        public Manco.Licensing.License License
        {
            set
            {
                m_License = value;
                daysleft = Convert.ToInt32(m_License.DaysLeft.TotalDays);
            }

            get
            {
                return daysleft;
            }
        }
    }
Posted
Updated 23-Nov-10 6:24am
v2

First of all, let me see if I am understanding what you're trying to do. You have a license class created with a lot of information about the license. Then, you created a separate class to check the license to see if it is still valid. Is that correct?

If you want a stand-alone class to check licenses, you should make it a static class with a single static function. Making a full class where you set a property and then check a different property doesn't make any sense. Instead, you could simply have a LicenseCheck function in your static class.

It could look something like:
C#
static class LicenseCheck
{
  public static bool CheckLicense(Manco.Licensing.License license)
  {
    return license.DaysLeft.TotalDays > 0;
  }
}


Then if you want to check a license all you have to do is:
C#
if (LicenseCheck.CheckLicense(m_license))
{
  //do something
}


But, look at the code you posted in the fake answer you posted. You create a new LicCheck object, but you don't have anything that initializes your Manco.Licensing.License object. So with the Main code you posted, you should get an error. And I must say that the way that you created those classes is a mess. Why do you have a double called no_days and then another variable called No_days. Talk about confusing your end user. Also, while you can refer directly to m_license in LicCheck, the more proper way would be to use base.m_license. That is more clear to the coder where m_license came from.
 
Share this answer
 
Comments
Richard MacCutchan 21-Dec-10 15:47pm    
Excellent analysis of the issue, IMHO.
sclarke18 22-Dec-10 6:34am    
Hi William,

Thanks for the feedback. I didn't mean it as a 'fake answer', as I simply wanted to reply to the posts to try and get this resolved.

I agree that my code was constructed very well, as I am trying everything.

Anyway, I tried as you suggested above, but got the message of 'The name 'm_license' does not exist in the current context'. I include the if '(LicenseCheck.CheckLicense(m_license))' part in the Program.cs class - is this what you meant?

In another part of my code I have the following which gets this information (ie days left), and displays it in a form (works fine). So I am trying to return the days left value to a variable rather than display it in a form.

public partial class LicenseForm : Form
{

private Manco.Licensing.License m_License;

public LicenseForm()
{
InitializeComponent();
}

public Manco.Licensing.License License
{
set
{
m_License = value;
lblDays.Text = string.Format("{0:F0}", m_License.DaysLeft.TotalDays);
}

}
The get is returning an integer, and not the Manco.Licensing.License object.

In order for this get to work, you would have to return the m_License object itself.
 
Share this answer
 
Comments
sclarke18 23-Nov-10 12:26pm    
Thanks for the fast answer. Yes, I was going to return the object. However this contains a lot more information than just the value of daysleft, so I just want to create an integer variable which gets this value in this class locally and just passes back this value eg 10, 20, etc. Any ideas about how I would change to do this?
fjdiewornncalwe 23-Nov-10 13:32pm    
In order to pass just that value back, you could just do something like this, but you can't have a different argument type for the get and the set of a given property.

public Manco.Licensing.License License{ get; set; }

public int LicenseDaysLeft
{
get { return m_License.DaysLeft.TotalDays; }
}
sclarke18 24-Nov-10 7:55am    
Thanks for getting back to me. I tried doing as you suggested but I don't think that it is getting the 'm_License' value in this case. I tried the following as a variation ie setting the daysleft value within 'private Manco.Licensing.License License', and then returning it from 'public double LicenseDaysLeft'. I think it should work but its not returning the 'daysleft' value that I need:


In LicClass.cs class
public class LicCheck
{
private Manco.Licensing.License m_License;
public static double daysleft;

private Manco.Licensing.License License
{
set
{
m_License = value;
daysleft = Convert.ToInt32(m_License.DaysLeft.TotalDays);
}
}
public double LicenseDaysLeft
{
get { return LicCheck.daysleft; }
}
}

In Program.cs class
LicCheck licCh = new LicCheck();

int locdaysleft = Convert.ToInt32(licCh.LicenseDaysLeft);
Thanks again for your help with this. I had to leave this, but getting back to it now to try and get it working. I have modified my code below based on feedback and further trial and error.

As far as I can tell, this should work but can't get it to. The main problem seems to be 2 types of value in the class (in LicCheck.cs) ie one which contains a lot of licensing information (No_days), and trying to pass one part of this ie specifically the number of days left to another part of the class (no_days), and then return this as an value (integer, double, etc), back to Program.cs. This value will then be used to check on the number of days left.

Any suggestions/advice is much appreciated.

In LicCheck.cs class:
C#
public class Lic    //class to get the license information. This part doesn't seem to be called?
{
    protected Manco.Licensing.License m_License;
    private double no_days;
    public Manco.Licensing.License No_days
    {
        get { return m_License; }
        set 
        { 
            m_License = value;
            int test = Convert.ToInt32(m_License.DaysLeft.TotalDays); //test to see if it shows value, but doesn't
        }
    }
}

public class LicCheck : Lic  //class to specifically get the number of days left from the license information
{
    private double no_days;
    public new double No_days
    {
        get { return no_days; }
        set { no_days = Convert.ToInt32(m_License.DaysLeft.TotalDays); }
    }
}


In Program.cs class:
C#
static void Main()
{
    LicCheck l1 = new LicCheck();
    int x = Convert.ToInt32(l1.No_days);
}


[edit]fixed code formatting[/edit]
 
Share this answer
 
v2
Comments
Richard MacCutchan 21-Dec-10 15:04pm    
You seem to be making quite a meal of this with your conversion between double and integer; why not stick to integer values throughout?
You also have a getter returning a type of Manco.Licensing.License but you call it No_days; why not call it Licence, since that is what it returns?
If you want to do something with objects in your code from Program.cs, the best way is to create a static class and instantiate/initiate those objects in the static class. Static classes are created when the program starts (and before your main form is instantiated).

At that point you should be gold.

Beyond that, your property is of type License, yet you're returning an int in the get delegate. You shoudl add another property that returns the number of days from the License object, or maybe even a bool that simply indicates whether or not the software's license has expired or not.

 
Share this answer
 
I'm not even sure where to begin. You do not seem to understand the way that object-oriented programming works.

I took a look at the sample code from the Manco website and it seems like you've pulled out one form and missed the other form. And you don't understand the Manco licensing scheme at all.

When you first start up your form, you have to initialize the License using a LicenseManager. And the Manco license has a property called IsValid. You don't need to do your own checking of whether or not the license is valid.

I doubt any of us can really help you until you understand exactly what you're trying to do and what your problem really is. First, you should look at the documentation for the License object. See what properties it has. (Actually, first, it seems you should take some object-oriented programming classes).
 
Share this answer
 
Comments
sclarke18 23-Dec-10 9:18am    
Hi William, This is one one small part of my code, and I have the other parts working i.e. checking that the 'IsValid' is true and the key being generated from the Licese Manager, and accepted by my program when correct ie allows the program to start.

Its true that I am not an expert programmer, and have been trying to learn more about this and the correct way to do this, hence my questions. As above, this is only one class of the overall program and I simply want to get the days left value from the license object, and use this value to only display the license form (ie asking for key) if less than 2 or 3 days to go. Perhaps I should have been clearer about this, but was looking for suggestions as to how I would do this eg use static class, get set, etc, especially when trying to convert a value from the License object to an integer, etc. Thanks for your time with this anyway.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900