简体   繁体   中英

Create variable of a generic class from a generic class

I want to create a class that can keep a number between to values. Here for I have created my first two generic classes.

The first called LimitRang and keep two limit rang variables; one for the lower rang and for the top rang. The second called NumberLimitRang and keep the number and the first class LimitRang.

When I try to create the variable mvarRang, how is a LimitRang var, in the constructor of NumberLimitRang I receive the error : 'Cannot implicitly convert type 'VariableTypes.Supplement.LimitRang' to 'T'.

My code for LimitRang:

namespace VariableTypes.Supplement
{
/// <summary>
/// Manage the boundaries for a number.
/// </summary>
/// <typeparam name="T">The numberic type for the limit parameters</typeparam>
public class LimitRang<T> where T : IComparable
{
    private T mvarLowestLimitNumber;
    private T mvarHighestLimitNumber;

    /// <summary>
    /// The default constructor (between 0 and 100)
    /// </summary>
    public LimitRang()
    {
        if (Functions.IsNumericType(typeof(T)))
        {
            try
            {
                mvarLowestLimitNumber = (T)Convert.ChangeType(0, typeof(T));
                mvarHighestLimitNumber = (T)Convert.ChangeType(100, typeof(T));
            }
            catch
            {
                mvarLowestLimitNumber = default(T);
                mvarHighestLimitNumber = default(T);
            }
        }
    }
}
}

My code for NumberLimitRang:

 namespace VariableTypes
 {
 /// <summary>
 /// Can contain a number that need to be between or equal to two limit numbers
 /// </summary>
 public class NumberLimitRang<T> where T : IComparable
 {
     private Supplement.LimitRang<T> mvarRang;
     private T mvarNumber;

    /// <summary>
    /// The default constructor (between 0 and 100/Number = 0)
    /// </summary>
    public NumberLimitRang(T mvarRang)
    {
        mvarRang = new Supplement.LimitRang<T>();
        mvarNumber = (T)Convert.ChangeType(0, typeof(T));
    }
}
}

Even when I replace the two T's into int, I still receive the error:

-    private Supplement.LimitRang<int> mvarRang;

-        mvarRang = new Supplement.LimitRang<int>();

Can you tell me what I do wrong?

Thx a lot

public NumberLimitRang(T mvarRang)
{
    mvarRang = new Supplement.LimitRang<T>();
    mvarNumber = (T)Convert.ChangeType(0, typeof(T));
}

The constructor takes a variable mvarRang of type T . This declaration hides the instance member mvarRang of the type LimitRang<T> .

Either change the parameter name, or refer to the instance member explicitly using this :

public NumberLimitRang(T mvarRang)
{
    this.mvarRang = new Supplement.LimitRang<T>();
    mvarNumber = (T)Convert.ChangeType(0, typeof(T));
}

Problem is here:

public NumberLimitRang(T mvarRang)
{
    mvarRang = new LimitRang<T>();
    mvarNumber = (T)Convert.ChangeType(0, typeof(T));
}

You pass mvarRang as an argument and you have field with name mvarRang . But in method NumberLimitRang compiler uses mvarRang as argument, wich is type of T and tryes to replace it's value with new LimitRang<T>(); . So, first of all, rename field or argument. For example:

public class NumberLimitRang<T> where T : IComparable
{
    private LimitRang<T> _mvarRang;
    private T _mvarNumber;

    /// <summary>
    /// The default constructor (between 0 and 100/Number = 0)
    /// </summary>
    public NumberLimitRang(T mvarRang)
    {
        _mvarRang = new LimitRang<T>();
        _mvarNumber = (T)Convert.ChangeType(0, typeof(T));
    }
}

It's good practice to name fields with _ in the beginning.

But now, your argument mvarRang is unused. Revise your logic in code.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM