不能隐式将类型字符串转换为字符串[]? [英] Cannot implicitly convert type string to string[] ?
本文介绍了不能隐式将类型字符串转换为字符串[]?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
ShadingAnalysisDataSetTableAdapters.tbl_CadEngineersTeamTableAdapter cd;
cd = new ShadingAnalysisDataSetTableAdapters.tbl_CadEngineersTeamTableAdapter();
DataTable dt2 = new DataTable();
dt2 = cd.GetAssignTeam(x);
object a = cd.GetAssignTeam(x);
string[] b = a.ToString(); // error popup here
推荐答案
代码
Hi,
In the code
string[] b = a.ToString();
您正在为字符串对象分配数组。你必须为它分配一个字符串数组类型对象。
字符串是String类型的对象,其值为text。在内部,文本存储为Char对象的顺序只读集合。
虽然字符串数组是字符串的集合。因此,您不能指定那种方式。
尝试将对象转换为字符串数组,然后将其分配给数组。
例如在你的代码中尝试:
you are assigning a string object to array. You will have to assign a string array type object to it.
A string is an object of type String whose value is text. Internally, the text is stored as a sequential read-only collection of Char objects.
While a string array is a collection of strings. Hence, you cannot assign that way.
Try converting a object to string array and then assigning it to the array.
For e.g in your code try:
string[] b = ((System.Collections.IEnumerable)a).Cast<object>().Select(x => x.ToString()).ToArray();
希望这会有所帮助!! :)
问候,
Praneet
Hope this helps !! :)
Regards,
Praneet
你好,
.ToString()
返回单个字符串而不是字符串数组。所以在执行前检查你的代码。
Hello ,
.ToString()
retuns single string rather than string array . so check your code before execution .
每个数组都实现了IEnumerable。你可以在这里使用这个功能。
试试这个代码
Hi,
Every array implements IEnumerable. You can use that functionality here.
Try this code
string[] b = ((IEnumerable)obj).Cast<object>() .Select(x => x.ToString()).ToArray();
这篇关于不能隐式将类型字符串转换为字符串[]?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文