不能隐含地将测试转换为测试[] [英] Cannot implicily convert test to test []

查看:101
本文介绍了不能隐含地将测试转换为测试[]的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述





我的课程名为Testdata,如

 公开 测试数据{

public string x;
public string y;
public test [] z;
}



和另一个班级

  public   class  test {
public string m;
public string d;
}



我需要发送 Testdata 类作为输入参数,该参数应具有类的值测试也。



我创建的对象如

 Testdata obj =  new  Testdata(); 



再次

 obj.m =  new  test(); 



我收到的错误如

引用:

不能隐式地将测试转换为测试[]





这有什么帮助吗? ????

解决方案

除了问题中的拼写错误:

 obj.m = < span class =code-keyword> new  test(); 
// 应该
obj.z = new test();
// 重现错误。





Testdata.z 中的测试数组必须初始化为正确大小的数组才能将任何内容放入数组中。

  public   class  Testdata {
public string x;
public string y;
public test [] z = new test [命名常量 数组的正确大小];
}



所以下一个问题是:在你创建 Testdata 实例的时候,做你知道 Testdata.z 数组必须包含多少条目或者它的大小是动态的?

似乎很可能大小不是预先确定,所以将z的声明从测试数组更改为List< test>将是一个很好的解决方案:

  public   class  Testdata {
public string x;
public string y;
public 列表< test> z = new List< test>();
}



然后可以将测试实例添加到z集合中:

 test theTest =  new  test(); 
// 正确初始化测试
obj.z.Add( new theTest);





参见List< T>课程 [ ^ 获取有关使用该集合类型的更多信息。



编辑 - Matt T Heffron:来自评论的后续内容。



1.在创建Testdata时,使用适当的大小初始化z数组,在使用新的Testdata创建后立即从Testdata类的外部开始()

 Testdata obj =  new  Testdata(); 
obj.z = new test [命名常量 正确的数组大小];



2.将#1中的数组初始化为空数组,然后在向数组添加项目时手动管理数组的大小(可能使用System.Array.Resize())

 Testdata obj =  new  Testdata(); 
obj.z = new test [ 0 ];



然后你需要在数组中添加一个新元素:

 Array.Resize( ref  obj.z,obj.z.Length + 1); 
obj.z [obj.z.Length-1] = new test();



这是非常低效的,所以除非你别无选择,否则不要这样做!



3.如果可以的话,创建和收集List中给定Testdata的测试类的所有实例,当它们全部存在时,将Testdata.z设置为转换为数组的集合。例如,使用Linq,这将是obj.z = testsList.ToArray();

 Testdata obj =  new  Testdata(); 
列表< test> testsList = new List< test>();
// 这里构造/初始化此Testdata实例的所有测试实例
test nt = new test();
// 初始化此测试
testsList.Add(nt);
// 创建测试的所有实例并将其收集到testsList中:
obj.z = testsList.ToArray();


如果我理解你,你想存储数组的测试 Testdata 类中的类。如果我没错,请看这篇文章:演练:创建你的自己的收集类 [ ^ ]

Hi ,

I have class named Testdata like

public class Testdata {

    public string x;
    public string y;
    public test[] z;
}


and another class

public class test{
    public string m;
    public string d;
}


I need to send the Testdata class as the input parameter which should have values of class test also .

I created object like

Testdata obj = new Testdata();


and again

obj.m = new test();


I got the error like

Quote:

cannot implicitly convert test to test[]



any help in thiss?????

解决方案

Other than the typo in the question:

obj.m = new test();
// should be
obj.z = new test();
// to reproduce the error.



The test array in Testdata.z must be initialized to a correctly sized array before you can put any content in the array.

public class Testdata {
    public string x;
    public string y;
    public test[] z = new test[named constant for correct size of the array];
}


So the next question is: At the point where you create the Testdata instance, do you know how many entries the Testdata.z array must contain or is its size dynamic?
It seems most likely that the size is not pre-determined, so changing the declaration of z from an array of test to a List<test> would be a good solution:

public class Testdata {
    public string x;
    public string y;
    public List<test> z = new List<test>();
}


Then the test instances can be added to the z collection with:

test theTest = new test();
// initialize theTest appropriately
obj.z.Add(new theTest);



See List< T > class[^] for more information about using that collection type.

Edit - Matt T Heffron: followup from comments.

1. initialize the z array with the appropriate size when Testdata is created, from outside of the Testdata class right after it is created with new Testdata()

Testdata obj = new Testdata();
obj.z = new test[named constant for correct size of the array];


2. initialize the array as in #1 to an empty array and then manage the size of the array manually whenever you add items to the array (probably using System.Array.Resize() )

Testdata obj = new Testdata();
obj.z = new test[0];


Then everywhere you need to add a new element to the array:

Array.Resize(ref obj.z, obj.z.Length+1);
obj.z[obj.z.Length-1] = new test();


This is very inefficient, so don't do this unless you have no other choice!

3. If you can, create and collect all of the instances of the test class for a given Testdata in a List and when they are all there, then set the Testdata.z to the collection converted to an array. E.g., using Linq this would be obj.z = testsList.ToArray();

Testdata obj = new Testdata();
List<test> testsList = new List<test>();
// here construct/initialize all of the instances of test for this instance of Testdata
test nt = new test();
// initialize this test
testsList.Add(nt);
// after all of the instances of test are created and collected into testsList:
obj.z = testsList.ToArray();


If i understand you well, you want to store the array of test class inside Testdata class. If i'm not wrong, please see this article: Walkthrough: Creating Your Own Collection Class[^]


这篇关于不能隐含地将测试转换为测试[]的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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