C Board  

Go Back   C Board > General Programming Boards > C# Programming

Reply
 
LinkBack Thread Tools Display Modes
Old 02-02-2008, 05:12 AM   #1
Registered User
 
Join Date: Jan 2007
Posts: 188
Edit string problem

Hey!

Im trying to replace one char in my string with another one. Obviously i don't know what's wrong because then i would fix it. :P W/e, here's my code:

Code:
textBox4.Text[Convert.ToInt32(var)] = Convert.ToChar(var);
The compiler sais:
Code:
Error	1	Property or indexer 'string.this[int]' cannot be assigned to -- it is read only	C:\Users\HP-användare\Documents\Visual Studio 2008\Projects\Hangman\Hangman\Form1.cs	132	17	Hangman
Thank you!
Livijn is offline   Reply With Quote
Old 02-03-2008, 03:07 PM   #2
Registered User
 
Join Date: Jan 2007
Posts: 188
I've googled it many times, but i can't find the answer. I can give you all the information about the code, just tell me.
Livijn is offline   Reply With Quote
Old 02-03-2008, 03:22 PM   #3
Confused
 
Magos's Avatar
 
Join Date: Sep 2001
Location: Sweden
Posts: 3,125
Strings are immutable so you can't manually assign individual characters as you can in say C++. Look up the string methods Replace/Remove/Insert as they may be what you seek.
__________________
MagosX.com

Give a man a fish and you feed him for a day.
Teach a man to fish and you feed him for a lifetime.
Magos is offline   Reply With Quote
Old 02-04-2008, 01:26 AM   #4
Registered User
 
Join Date: Jan 2007
Posts: 188
Thanks for your help!
Livijn is offline   Reply With Quote
Old 02-09-2008, 03:23 PM   #5
Registered User
 
AloneInTheDark's Avatar
 
Join Date: Feb 2008
Posts: 74
Code:
        /*########################################*/
        /*########################################*/
        /*########################################*/
        private String String_Replace(String strText, String strFind, String strReplace)
        {
            try
            {
                int iPos = strText.IndexOf(strFind, System.StringComparison.CurrentCultureIgnoreCase);
                String strReturn = "";
                while (iPos != -1)
                {
                    strReturn += strText.Substring(0, iPos) + strReplace;
                    strText = strText.Substring(iPos + strFind.Length);
                    iPos = strText.IndexOf(strFind);
                }
                if (strText.Length > 0) strReturn += strText;
                return strReturn;
            }
            catch
            {
                return strText;
            }
        }
        /*########################################*/
        /*########################################*/
        /*########################################*/
Is that what you need?

hm, You could I guess split your string into an array of bytes, replace your char there and then put it back together building a string.
AloneInTheDark is offline   Reply With Quote
Old 02-16-2008, 06:25 AM   #6
Registered User
 
Join Date: Jan 2007
Posts: 188
Got a question...
How can i store a word so i can get it from all classes? This doesn't work, but you might understand what i want.

Code:
public struct holder
{
       int io;
}
public class save()
{
      holder save = new holder;
      save.io = 1;
}
public class print()
{
      holder print = new holder;
      if (print.io != 1)
      {
            //true
       }
}
Well, the int io should stay at 1 not change. How do i do that?
Livijn is offline   Reply With Quote
Old 02-16-2008, 09:40 PM   #7
Registered User
 
AloneInTheDark's Avatar
 
Join Date: Feb 2008
Posts: 74
If you need to reach a variable from outside a class, you do it as "public ..." , if not "private ..."

Code:
public class MyClass(){
public string ThisVariableCanBeReached;
private string ThisVariableCanNOTbeReached;
}
So you'll be able to use
MyClass TEST = new MyClass();
TEST.ThisVariableCanBeReached = "bla bla";

If you need something which "doesn't change", try "public const"
public const string ThisCanNotBeChanged = "Can't touch this!";
AloneInTheDark is offline   Reply With Quote
Old 02-17-2008, 05:53 AM   #8
Registered User
 
Join Date: Jan 2007
Posts: 188
When i try public const string... i get an error:
Code:
Error	1	Member 'Hangman.Form1.holder.word' cannot be accessed with an instance reference; qualify it with a type name instead	C:\Users\HP-användare\Documents\Visual Studio 2008\Projects\Hangman\Hangman\Form1.cs	71	37	Hangman
Why?
Livijn is offline   Reply With Quote
Old 02-17-2008, 03:25 PM   #9
Registered User
 
AloneInTheDark's Avatar
 
Join Date: Feb 2008
Posts: 74
Show me the code, not that weird long error without a line break in it!
AloneInTheDark is offline   Reply With Quote
Old 02-18-2008, 01:10 PM   #10
Registered User
 
Join Date: Jan 2007
Posts: 188
Well since my computer crashed, it will take a while before... I must recode all!
Livijn is offline   Reply With Quote
Old 02-21-2008, 02:54 AM   #11
Registered User
 
AloneInTheDark's Avatar
 
Join Date: Feb 2008
Posts: 74
Quote:
Originally Posted by Livijn View Post
Well since my computer crashed, it will take a while before... I must recode all!
huh? computer crashed? What does that have to do with your code? Did the drive die?

Dude, ever heard of BACKUPS ? DVDs? External Drives? Don't tell me you formatted your drive and "lost" everything! hahahha... god damn it, my stomac hurts from the laugh.

AloneInTheDark is offline   Reply With Quote
Reply

Thread Tools
Display Modes

Forum Jump

Similar Threads
Thread Thread Starter Forum Replies Last Post
Inheritance Hierarchy for a Package class twickre C++ Programming 7 12-08-2007 04:13 PM
string problem INeedSleep C++ Programming 24 11-08-2007 11:52 PM
Program using classes - keeps crashing webren C++ Programming 4 09-16-2005 03:58 PM
string pattern search problem goron350 C Programming 6 11-25-2004 08:50 AM
Edit Control problem Malek Windows Programming 3 06-16-2002 01:12 AM


All times are GMT -6. The time now is 05:40 AM.


Powered by vBulletin® Version 3.8.1
Copyright ©2000 - 2010, Jelsoft Enterprises Ltd.
Search Engine Optimization by vBSEO 3.3.2

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22