Link to home
Start Free TrialLog in
Avatar of assaultkitty
assaultkitty

asked on

Line 33

This is the error that I am recieving. Can you help me.  I am a student needing tutoring and guidance.  The following errror is the problem in my program on line 33:

Error      1      Property or indexer 'string.this[int]' cannot be assigned to -- it is read only      C:\Users\acer\Documents\TitleCase\Default.aspx      33      25      C:\...\TitleCase\

This is what I am trying to accomplish.

I must create a script that takes text that a user enters into a form field and converts it to title case capitalization.  Use a split()  method to split words in the string into an indexed array.  Then create a for loop that uses another split() method that splits each word in the elements of the indexed array into another another indexed array of characters.  Within the for loop, us he ToUpper() method to convert the first element in the second array (which represents the first characters in the word to uppercase) to uppercase, and then use the Join() method to rebuild the array of words in the text string.  Execute a final Join() method to convert the array of words back into a single text strin.  Save the project as TitleCase
TitleCase.zip
ASKER CERTIFIED SOLUTION
Avatar of Vikram Singh Saini
Vikram Singh Saini
Flag of India image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of assaultkitty
assaultkitty

ASKER

Can you tell me what the possible error maybe?  Because this is the second time that an expert has told me this and I am unable to visually see my output.
This is the error.  Please help me.  Because I need to solve this problem or I am not going to get anything done.
Doc3.docx
I have searched the internet and I cannot find the solution to this problem.
Avatar of Navneet Hegde
Hi!

Change your Default.aspx to

<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Default.aspx.cs" Inherits="_Default" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head id="Head1" runat="server">
    <title>Converted Text to Title Case Capitalization:</title>
    <link rel="Stylesheet" href="asp_styles.css" type="text/css" />
</head>
<body>
    <h3>
        Text to Title Case Capitalization</h3>
    <script runat="server">  //code declaration block begins here:

        void convertTitleCase() //a function which returns a text string converted to title case capitalization
        {
            string enteredText = Request.Form["stringOfWords"];
            enteredText = enteredText.ToTitleCase();
            Response.Write(string.Join(" ", enteredText));
        }

    </script>
    <% //code rendering block begins here:

        convertTitleCase(); //calling statement of the function
                                      
    %>
</body>
</html>

Open in new window


And Your Code Default.aspx.cs to

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

public partial class _Default : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {

    }

 
}

public static class myString
{
    public static string ToTitleCase(this string Input)
    {
        return System.Threading.Thread.CurrentThread.CurrentCulture.TextInfo.ToTitleCase(Input.ToLower());
    }

}

Open in new window


Thanks!
A Correction Change

Defult.aspx -
Line 18 : Response.Write(enteredText);

Thanks!
I have searched the internet and I cannot find the solution to this problem.

1. I have why for your error but I have still to find why for the very same code works on our pc. However here is your WHY:

The only part of your code which is responsible for error is :

enteredTextArray[i] = tempS;  // showed on line 40 in VS 2010

Open in new window


It is because string are immutable and the indexer is read only on a String object. So you can read value such as :

string myString = "vs00saini";
char firstCharacter = myString[0]; // returns 'v'

Open in new window


But if you try to assign some value at that specified index (myString[2] = 'h'), it would throw error as you faced. For more details check link: Programming C# 4.0 excerpt

2. Ok! I got it but still I want to use my code with modifications. How can I do?

For that you have to use StringBuilder which allows mutation of string or make them mutable. See below modified code (using string builder class):

Code in Default.aspx: (only script part)
<script runat="server">  //code declaration block begins here:

        void convertTitleCase() //a function which returns a text string converted to title case capitalization
        {
            // Declared StringBuilder class
            StringBuilder builder = new StringBuilder();

            string enteredText = Request.Form["stringOfWords"];
            string[] enteredTextArray = enteredText.Split(' '); //a Split() method that converts the string into an array
            for (int i = 0; i < enteredTextArray.Length; ++i)
            {
                //split word to character array
                char[] temp = new char[enteredTextArray[i].Length];

                //convert only first character to upper case
                for (int j = 0; j < enteredTextArray[i].Length; j++)
                {
                    if (j == 0)
                        temp[j] = char.Parse(enteredTextArray[i][j].ToString().ToUpper());
                    else
                        temp[j] = enteredTextArray[i][j];
                }

                //join characters to word                   
                for (int j = 0; j < enteredTextArray[i].Length; j++)
                {
                    // Appending the value
                    builder.Append(temp[j]);
                }

                // Adding space between words
                builder.Append(" ");

            }

            // Showing joined words
            Response.Write("<b>Your text:</b> " + builder);
        }

    </script>

Open in new window


Hope that helps you to understand the reason of error.
Can you send me a screen shot of the output of the code at the local host?  I am still having problems with this project.  Thank you.
1. Problems would be part of development until you stop testing.  So I have attached working code and screen shots as asked. Check them.

2. Which IDE or development tool are you using? Are you testing your website after hosting on IIS locally?

3. Is it Asp.net 2.0 or 4.0 website. Either of the case, have you tried deleting temporary files from %SYSTEM%\Microsoft.NET\Framework\v4.0.30319\Temporary ASP.NET Files for 4.0 or for 2.0 replace v4.0 with v2.0.50727. Sometimes they would not delete as might be in use, so try to delete after restarting your system. Most of times it help to solve the issue.

4. Last, might sound ridiculous, change your project or website folder to some other drive and also change name; then try to run website. I hope this would solve the matter.

Let us know how (step-by-step) you are testing or developing your website. Might be something we can catch to help you.
TitleCase.zip
Screenshot.zip
This for being a great expert.