[英]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; 第一个叫LimitRang并保留两个限制范围变量; one for the lower rang and for the top rang.
一个用于较低的响铃和一个用于顶部的响铃。 The second called NumberLimitRang and keep the number and the first class LimitRang.
第二个叫NumberLimitRang并保留数字和第一类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'. 当我尝试创建变量mvarRang时,在NumberLimitRang的构造函数中,LimitRang var如何收到错误:'无法将类型'VariableTypes.Supplement.LimitRang'隐式转换为'T'。
My code for LimitRang: 我的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: 我的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: 即使我将两个T替换为int,我仍然会收到错误:
- 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
. 构造函数采用类型为
T
的变量mvarRang
。 This declaration hides the instance member mvarRang
of the type LimitRang<T>
. 此声明隐藏了
LimitRang<T>
类型的实例成员mvarRang
。
Either change the parameter name, or refer to the instance member explicitly using this
: 更改参数名称,或使用
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
. 您将
mvarRang
作为参数传递,并且您具有名称为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>();
但是在方法
NumberLimitRang
编译器使用mvarRang
作为参数,至极是类型T
和tryes替换它与值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. 但是现在,你的论据
mvarRang
尚未使用。 Revise your logic in code. 修改代码中的逻辑。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.