KIRAN K J
KIRAN K J

Reputation: 732

Cannot implicitly convert type 'char' to '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);
}
}

This is a simple asp.net code,i cannot convert char to char array... How to find the character at ch[i] th position?

Upvotes: 1

Views: 11617

Answers (6)

Pleun
Pleun

Reputation: 8920

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.

Upvotes: 0

Paolo Tedesco
Paolo Tedesco

Reputation: 57192

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;

Upvotes: 3

Davide Piras
Davide Piras

Reputation: 44605

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

Upvotes: 0

Jon Skeet
Jon Skeet

Reputation: 1500355

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[]).

Upvotes: 6

manji
manji

Reputation: 47978

To convert a string to char array:

chname1 = txtname1.Text.ToCharArray()

(ToCharArray documentation)

Upvotes: 1

DaveRead
DaveRead

Reputation: 3413

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

Upvotes: 1

Related Questions