char to string fails

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • dave

    #1

    char to string fails

    char[] ch = new char[1];

    ch[0]="-";

    FileStream fp=new FileStream("z:\ \" + "math" + ch[0].ToString() +
    "optpivot",File Mode.Open,FileA ccess.Read);



    Why does this fail with Cannot implicitly convert type 'string' to 'char'
    (CS0029) ?

    Best

    David


  • Trey Walpole

    #2
    Re: char to string fails

    double quotes indicate string type, single quotes indicate char type
    so try:

    ch[0] = '-';

    dave wrote:
    char[] ch = new char[1];
    >
    ch[0]="-";
    >
    FileStream fp=new FileStream("z:\ \" + "math" + ch[0].ToString() +
    "optpivot",File Mode.Open,FileA ccess.Read);
    >
    >
    >
    Why does this fail with Cannot implicitly convert type 'string' to 'char'
    (CS0029) ?
    >
    Best
    >
    David
    >
    >

    Comment

    • Nicholas Paldino [.NET/C# MVP]

      #3
      Re: char to string fails

      David,

      It's this line:

      ch[0]="-";

      It should be:

      ch[0]='-';

      Note the single quotes which are used to indicate a character literal.

      --
      - Nicholas Paldino [.NET/C# MVP]
      - mvp@spam.guard. caspershouse.co m

      "dave" <spammer@nospam mer.netwrote in message
      news:e65Yi.5073 9$8G5.40435@big news4.bellsouth .net...
      char[] ch = new char[1];
      >
      ch[0]="-";
      >
      FileStream fp=new FileStream("z:\ \" + "math" + ch[0].ToString() +
      "optpivot",File Mode.Open,FileA ccess.Read);
      >
      >
      >
      Why does this fail with Cannot implicitly convert type 'string' to 'char'
      (CS0029) ?
      >
      Best
      >
      David
      >
      >

      Comment

      • =?Utf-8?B?UGV0ZXIgQnJvbWJlcmcgW0MjIE1WUF0=?=

        #4
        RE: char to string fails

        As the exception message indicates, there is no implicit cast from string to
        char.
        Use this instead (with single quote marks):
        ch[0]='-';
        -- Peter

        unBlog: http://petesbloggerama.blogspot.com
        BlogMetaFinder: http://www.blogmetafinder.com



        "dave" wrote:
        char[] ch = new char[1];
        >
        ch[0]="-";
        >
        FileStream fp=new FileStream("z:\ \" + "math" + ch[0].ToString() +
        "optpivot",File Mode.Open,FileA ccess.Read);
        >
        >
        >
        Why does this fail with Cannot implicitly convert type 'string' to 'char'
        (CS0029) ?
        >
        Best
        >
        David
        >
        >
        >

        Comment

        Working...