Results 1 to 5 of 5

Thread: [RESOLVED] Uppercase first letter

  1. #1

    Thread Starter
    Frenzied Member
    Join Date
    Jun 2005
    Posts
    1,170

    Resolved [RESOLVED] Uppercase first letter

    Can someone tells me what's wrong with my code:

    Code:
            protected string CapFirstLetter(string sentence)
            {
                sentence = char.ToUpper(sentence[0]) + sentence.Substring(1);
                return sentence;
            }
    Then I call CapFirstLetter(Mytextbox.text)

    It never prints the first letter upper case.

  2. #2
    Hyperactive Member
    Join Date
    Mar 2002
    Location
    Boston, MA
    Posts
    391

    Re: Uppercase first letter

    The code for your method is fine it's the way you're invoking it.

    Using CapFirstLetter(Mytextbox.Text) doesn't do anything because you've passed the value of the text to the method not a reference to the value.

    Try this:

    MyTextBox.Text = CapFirstLetter(MyTextbox.Text)

  3. #3

    Thread Starter
    Frenzied Member
    Join Date
    Jun 2005
    Posts
    1,170

    Re: Uppercase first letter

    Is there a way to modify my method so that it will use reference?

  4. #4
    Hyperactive Member
    Join Date
    Mar 2002
    Location
    Boston, MA
    Posts
    391

    Re: Uppercase first letter

    Normally yes, you can be tagging your parameter with the ref keyword. But I believe that C# doesn't allow you to pass properties by reference (VB does I think). But you don't need to just change your statement from a method call to an assignment like I suggested and you should be fine.

  5. #5

    Thread Starter
    Frenzied Member
    Join Date
    Jun 2005
    Posts
    1,170

    Re: Uppercase first letter

    Thank you. I guess I can live with that

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