Solved

Line 33

Posted on 2012-03-24
10
370 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
 

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
IT, Stop Being Called Into Every Meeting

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

 
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

What Security Threats Are You Missing?

Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

Join & Write a Comment

Have you ever needed to get an ASP script to wait for a while? I have, just to let something else happen. Or in my case, to allow other stuff to happen while I was murdering my MySQL database with an update. The Original Issue This was written…
Real-time is more about the business, not the technology. In day-to-day life, to make real-time decisions like buying or investing, business needs the latest information(e.g. Gold Rate/Stock Rate). Unlike traditional days, you need not wait for a fe…
In this seventh video of the Xpdf series, we discuss and demonstrate the PDFfonts utility, which lists all the fonts used in a PDF file. It does this via a command line interface, making it suitable for use in programs, scripts, batch files — any pl…
You have products, that come in variants and want to set different prices for them? Watch this micro tutorial that describes how to configure prices for Magento super attributes. Assigning simple products to configurable: We assigned simple products…

744 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

Need Help in Real-Time?

Connect with top rated Experts

13 Experts available now in Live!

Get 1:1 Help Now