7 Replies - 17435 Views - Last Post: 05 August 2010 - 07:40 AM Rate Topic: -----

#1 Guest_Somya gupta*


Reputation:

Error :Cannot implicitly convert type 'string' to 'char

Posted 04 August 2010 - 02:24 PM

Hi,

I am new to c#. While trying to split a string I encountered the following error "Error 2 Cannot implicitly convert type 'string' to 'char'"

The code is as follows:
     string sGroupID;
     string[] aGroupList;
     sGroupID = Request.QueryString["GROUPIDS"];
     aGrouplist = sGroupID.Split(new char [] {","});



Anyhelp will be appreciated

Is This A Good Question/Topic? 0

Replies To: Error :Cannot implicitly convert type 'string' to 'char

#2 Robin19   User is offline

  • D.I.C Addict
  • member icon

Reputation: 272
  • View blog
  • Posts: 552
  • Joined: 07-July 10

Re: Error :Cannot implicitly convert type 'string' to 'char

Posted 04 August 2010 - 02:37 PM

new char[] {","}

should be
new char[] {','}

Chars are shown with single quotes. Double quotes are for strings.
Was This Post Helpful? 1
  • +
  • -

#3 tlhIn`toq   User is offline

  • Xamarin Cert. Dev.
  • member icon

Reputation: 6535
  • View blog
  • Posts: 14,450
  • Joined: 02-June 10

Re: Error :Cannot implicitly convert type 'string' to 'char

Posted 04 August 2010 - 02:38 PM

Quote go around strings.
Apostophes go around chars


aGrouplist = sGroupID.Split(new char [] {','});
Was This Post Helpful? 0
  • +
  • -

#4 Guest_Somya gupta*


Reputation:

Re: Error :Cannot implicitly convert type 'string' to 'char

Posted 05 August 2010 - 06:33 AM

Thanks but when i do that it gives an error "System.NullReferenceException: Object reference not set to an instance of an object." for the code

aGrouplist = sGroupID.Split(new char [] {','});




Was This Post Helpful? 0

#5 Robin19   User is offline

  • D.I.C Addict
  • member icon

Reputation: 272
  • View blog
  • Posts: 552
  • Joined: 07-July 10

Re: Error :Cannot implicitly convert type 'string' to 'char

Posted 05 August 2010 - 06:56 AM

Request.QueryString["GROUPIDS"] is ASP, which is a language I haven't used. But it looks like from the documentation that it should be parentheses.
sGroupID = Request.QueryString("GROUPIDS");


You are getting the NullReferenceException because you are trying to call Split(), but sGroupID is still set to Null when you call it. Therefore it must be the line above that sets sGroupID that is causing the problem.

This post has been edited by Robin19: 05 August 2010 - 06:57 AM

Was This Post Helpful? 0
  • +
  • -

#6 Guest_Somya gupta*


Reputation:

Re: Error :Cannot implicitly convert type 'string' to 'char

Posted 05 August 2010 - 07:23 AM

Thanks
Was This Post Helpful? 0

#7 Curtis Rutland   User is offline

  • (╯□)╯︵ (~ .o.)~
  • member icon


Reputation: 5106
  • View blog
  • Posts: 9,283
  • Joined: 08-June 10

Re: Error :Cannot implicitly convert type 'string' to 'char

Posted 05 August 2010 - 07:35 AM

Sorry, Robin, but that's incorrect. In ASP, that would be true, because the language behind ASP is VBScript.

But in ASP.NET, it uses C# or VB.NET. C# uses square brackets for indexes.

Here's an example of what the OP is trying to do:

if (!string.IsNullOrEmpty(Request.QueryString["test"]))
{
    string[] tokens = Request.QueryString["test"].Split(',');
    foreach (string token in tokens)
        label1.Text += token + " ";
}


And the result:
Posted Image

OP, you should learn about the params array. It makes your life easier. You don't have to supply an array, you can just supply values and the array is created for you.

This post has been edited by insertAlias: 05 August 2010 - 07:36 AM

Was This Post Helpful? 0
  • +
  • -

#8 Robin19   User is offline

  • D.I.C Addict
  • member icon

Reputation: 272
  • View blog
  • Posts: 552
  • Joined: 07-July 10

Re: Error :Cannot implicitly convert type 'string' to 'char

Posted 05 August 2010 - 07:40 AM

That's why you're the expert. Thanks Alias.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1