Results 1 to 6 of 6

Thread: [RESOLVED] Help - Cannot implicitly convert type 'char' to 'string'

  1. #1

    Thread Starter
    Member
    Join Date
    Mar 2006
    Posts
    41

    Resolved [RESOLVED] Help - Cannot implicitly convert type 'char' to 'string'

    Hi again folks,

    If I have a String object as in -

    Code:
    String myString = "ABCDEF";
    I can write the following -

    Code:
    Console.WriteLine(myString1[2]);
    and it will compile, outputting C to the console.

    That's all well and good, but what I'd like to be able to do is something like -

    Code:
    myButton.Text = myString[2];
    But I get the following error when I try to compile the program -

    Cannot implicitly convert type 'char' to 'string'

    I've tried casting the char to a string as in -

    Code:
    String myNewString = (String)(myString[2]);
    but that does not work either

    The word "implicitly" in the error I receive leads me to believe that what I'm trying to do is possible, but I've just got the syntax wrong, am I correct in that assumption?

    Can anyone show me how to get this working? Thank you.
    Using: VB.net + C#.net 2005 Express Editions + .Net Framework 2.0

    ~ Man Is The Warmest Place To Hide ~

  2. #2

    Thread Starter
    Member
    Join Date
    Mar 2006
    Posts
    41

    Smile Re: Help - Cannot implicitly convert type 'char' to 'string'

    I found a solution to my problem, here it is.


    Code:
    String myString = "ABCDEF";
    myButton.Text = myString.Substring(2, 1); // myButton's text will be 'C' 
    
    //If we develop this further we can have something like
    
    String myString = "ABCDEF";
    
    Button[] myButtons = new Button[myString.Length];
    
    for(int i = 0; i <= myString.Length; i++)
    {
    
         myButtons[i] = new Button();
    
         myButtons[i].Text = myString.Substring(i, 1);
    
         //Within this loop we can programmatically add these buttons to our forms, etc. 
    
    }
    Hope this was of help to someone
    Using: VB.net + C#.net 2005 Express Editions + .Net Framework 2.0

    ~ Man Is The Warmest Place To Hide ~

  3. #3
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,221

    Re: [RESOLVED] Help - Cannot implicitly convert type 'char' to 'string'

    You're trying to cast a char object as a string. A char object is not a string so that cannot be done. You must explicitly convert your char object to a string:
    Code:
    String myNewString = myString[2].ToString();
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

  4. #4

    Thread Starter
    Member
    Join Date
    Mar 2006
    Posts
    41

    Smile Re: [RESOLVED] Help - Cannot implicitly convert type 'char' to 'string'

    Hi jmcilhinney, thanks for the info.

    Can you explain the difference between casting and converting?
    Using: VB.net + C#.net 2005 Express Editions + .Net Framework 2.0

    ~ Man Is The Warmest Place To Hide ~

  5. #5
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,221

    Re: [RESOLVED] Help - Cannot implicitly convert type 'char' to 'string'

    Have you ever heard the expression "to cast something in a new light"? It means to look at the same thing in a different way, which is exactly how "cast" is used in programming. Casting doesn't change the type of the underlying object, only the type of the reference used to access it, e.g.
    Code:
    object obj = "Hello World";
    string str = (string)obj;
    The object that obj refers to is a string, so the cast doesn't change the type of the object. It simply creates a reference of a different type to the same object.

    It's very important to always remember that OOP is designed to mimic the real world. This casting is not a programming phenomenon. We do the same thing in life all the time. Let's say that you went to the vet with your pet. When you walk in the vet knows that you have an animal, but they don't know exactly what type of animal. You tell the vet that your pet is a dog. The vet had an Animal reference to your pet dog and you just cast it as a Dog. The dog itself was completely unaffected, but now the vet knows that your pet has all the attribute of a dog, while before they only knew it had the attributes of an animal. That's exactly why you cast in programming too: to get access to the members of the object that belong to the type you're casting as.
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

  6. #6
    New Member
    Join Date
    Apr 2011
    Posts
    1

    Thumbs up Re: [RESOLVED] Help - Cannot implicitly convert type 'char' to 'string'

    hey thanks a lot i got my problem solved

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width