无法将类型\'char\'隐式转换为\'char []\'

问题描述

||
public partial class _Default : System.Web.UI.Page 
{
char[] chname1=new char[20];
char[] chname2=new char[20];
protected void Page_Load(object sender,EventArgs e)
{

}
protected void btsubmit_Click(object sender,EventArgs e)
{
    chname1 = Convert.ToChar(txtname1.Text);
    chname2 = Convert.ToChar(txtname2.Text);
    Response.Write(chname1.ToString().Length+\"---\"+chname2.ToString().Length);
}
}
这是一个简单的asp.net代码,我无法将char转换为char数组... 如何找到第ch [i]个位置的字符?     

解决方法

        您有一个字符串,您正试图将其转换为
char
,然后将结果分配给
char[]
变量。 您确定不只是想要:
chname1 = txtname1.Text.ToCharArray();
请注意,在
char[]
上调用
ToString()
可能也没有执行您想要的操作...将
char[]
转换为
string
通常是通过
new string(char[])
完成的。     ,        您可以使用
string
类的
ToCharArray()
方法:
chname1 = txtname1.Text.ToCharArray();
但是,您到底想做什么?如果只需要长度,则无需转换为char [],只需使用
txtname1.Text.Length;
    ,        使用txtname1.Text.SubString(startPosition-1,1)     ,        要将
string
转换为
char
数组:
chname1 = txtname1.Text.ToCharArray()
(ToCharArray文档)     ,        我猜16 actually应该可以正常工作...     ,        您要达到的目标是什么?是您的txtname1和textname2文本框吗?在那种情况下,为什么要打扰字符数组。只需使用字符串和子字符串功能。     

相关问答

错误1:Request method ‘DELETE‘ not supported 错误还原:...
错误1:启动docker镜像时报错:Error response from daemon:...
错误1:private field ‘xxx‘ is never assigned 按Alt...
报错如下,通过源不能下载,最后警告pip需升级版本 Requirem...