Solved

Line 33

Posted on 2012-03-24
10
375 Views
Last Modified: 2012-03-26
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
0
Comment
Question by:assaultkitty
  • 5
  • 3
  • 2
10 Comments
 
LVL 16

Accepted Solution

by:
Vikram Singh Saini earned 500 total points
ID: 37761950
I tested your code in .zip file. And it is working fine as expected. I have faced no error.
0
 

Author Comment

by:assaultkitty
ID: 37761992
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.
0
 

Author Comment

by:assaultkitty
ID: 37761998
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
0
Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 

Author Comment

by:assaultkitty
ID: 37762002
I have searched the internet and I cannot find the solution to this problem.
0
 
LVL 15

Expert Comment

by:navneethegde
ID: 37762156
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!
0
 
LVL 15

Expert Comment

by:navneethegde
ID: 37762157
A Correction Change

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

Thanks!
0
 
LVL 16

Expert Comment

by:Vikram Singh Saini
ID: 37762491
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.
0
 

Author Comment

by:assaultkitty
ID: 37762912
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.
0
 
LVL 16

Expert Comment

by:Vikram Singh Saini
ID: 37765364
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
0
 

Author Closing Comment

by:assaultkitty
ID: 37768738
This for being a great expert.
0

Featured Post

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
orderby list (from Json) 1 30
ASP.net Will we ever be able to build mobile apps in it 6 32
Need help with a query 14 39
ASP Error Handler 5 21
IntroductionWhile developing web applications, a single page might contain many regions and each region might contain many number of controls with the capability to perform  postback. Many times you might need to perform some action on an ASP.NET po…
Calculating holidays and working days is a function that is often needed yet it is not one found within the Framework. This article presents one approach to building a working-day calculator for use in .NET.
With Secure Portal Encryption, the recipient is sent a link to their email address directing them to the email laundry delivery page. From there, the recipient will be required to enter a user name and password to enter the page. Once the recipient …
How to Install VMware Tools in Red Hat Enterprise Linux 6.4 (RHEL 6.4) Step-by-Step Tutorial

733 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question