从get和set类传回整数值 [英] Pass integer value back from get and set class

查看:85
本文介绍了从get和set类传回整数值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述



我试图根据许可证信息在类中设置一个整数值,然后将该值传递回我的Program.cs类(例如,值10).

但是,当我尝试将值返回到类的另一部分时(如下所示),它说"无法将类型隐式转换为Manco.Licensing.License ".也知道如何调用该类以将该整数值返回给Program.cs,即应该为 LCheck.Value.days 吗?在该类中,daysleft是一个整数值.

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.

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;
            }
        }
    }

推荐答案

首先,让我看看我是否了解您要执行的操作.您创建了一个许可证类,其中包含有关许可证的许多信息.然后,您创建了一个单独的类来检查许可证以查看其是否仍然有效.正确吗?

如果要让独立类检查许可证,则应使其成为具有单个静态函数的静态类.在设置属性然后检查其他属性的情况下进行完整的类没有任何意义.相反,您可以在静态类中简单地拥有一个LicenseCheck函数.

它可能类似于:
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:
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:

if (LicenseCheck.CheckLicense(m_license))
{
  //do something
}



但是,请查看您在发布的虚假答案中发布的代码.您创建了一个新的LicCheck对象,但是没有任何可初始化Manco.Licensing.License对象的东西.因此,使用您发布的主要代码,您应该会得到一个错误.我必须说,您创建这些类的方式是一团糟.为什么您有一个名为no_days的双精度变量,然后又有另一个名为No_days的变量.谈论混淆您的最终用户.同样,虽然您可以在LicCheck中直接引用m_license,但更正确的方法是使用base.m_license.对于使用m_license的编码器来说更清楚.



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.


get返回一个整数,而不是Manco.Licensing.License对象.

为了使此工作正常进行,您必须返回m_License对象本身.
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.


再次感谢您的帮助.我不得不离开这个,但是现在回到它身上,尝试使其正常运行.我已根据反馈和进一步的尝试和错误在下面修改了我的代码.

据我所知,这应该可以,但是无法实现.主要问题似乎是该类中的两种类型的值(在 LicCheck.cs 中),即一种包含大量许可信息( No_days )的值,并且试图通过其中一部分,即专门留给课程另一部分的天数( no_days ),然后将其作为值(整数,双精度等)返回给 Program. cs .然后,该值将用于检查剩余天数.

任何建议/建议都将不胜感激.

在LicCheck.cs类中:
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:
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); }
    }
}



在Program.cs类中:



In Program.cs class:

static void Main()
{
    LicCheck l1 = new LicCheck();
    int x = Convert.ToInt32(l1.No_days);
}



[edit]固定的代码格式[/edit]



[edit]fixed code formatting[/edit]


这篇关于从get和set类传回整数值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆