开发者

Cannot implicitly convert type 'char' to 'char[]'

开发者 https://www.devze.com 2023-03-12 22:09 出处:网络
public partial class _Default : System.Web.UI.Page { char[] chname1=new char[20]; char[] chname2=new char[20];
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);
}
}

This is a simple asp.net code,i cannot convert char to char array... How to find the character at ch[i] th positi开发者_如何学Goon?


You've got a string, and you're trying to convert it to char, and then assign the result to a char[] variable.

Are you sure you don't just want:

chname1 = txtname1.Text.ToCharArray();

Note that calling ToString() on a char[] probably doesn't do what you want it to either... converting a char[] to a string is normally done via new string(char[]).


You can use the ToCharArray() method of the string class:

chname1 = txtname1.Text.ToCharArray();

But what are you trying to do exactly? if you only need the length, there is no need to convert to char[], just use

txtname1.Text.Length;


Use txtname1.Text.SubString(startPosition-1, 1)


To convert a string to char array:

chname1 = txtname1.Text.ToCharArray()

(ToCharArray documentation)


txtname1.Text[i] should actually work I guess...


What is it you are trying to achieve? Are your txtname1 and textname2 textboxes? In that case why bother with an array of chars. Just use strings and substring functionality.

0

精彩评论

暂无评论...
验证码 换一张
取 消