wyrd Posted May 10, 2003 Posted May 10, 2003 using System; namespace ConsoleApplication3 { class Class1 { [sTAThread] static void Main(string[] args) { new Class1(); } public Class1() { string stuff = "yayaya"; Console.WriteLine("before: " + stuff); changeString(stuff); Console.WriteLine("after: " + stuff); Console.Read(); } private void changeString(string str) { str = "changed!"; Console.WriteLine("function changed: " + str); } } } This prints out the following; before: yayaya function changed: changed! after: yayaya Am I missing something?! I thought string was an object (reference type) while basic types like int, double, short, etc. and also enums and structures were value types. :confused: Quote Gamer extraordinaire. Programmer wannabe.
*Gurus* divil Posted May 10, 2003 *Gurus* Posted May 10, 2003 Strings are indeed objects but they are immutable. Anything you do where it looks like you're doing something to a string, you're actually creating a new copy of it. If you modified your above code to pass the string by reference instead of by value, you would get the result expected. Quote MVP, Visual Developer - .NET Now you see why evil will always triumph - because good is dumb. My free .NET Windows Forms Controls and Articles
wyrd Posted May 11, 2003 Author Posted May 11, 2003 Still that doesn't make sense to me. It should be automatically passed by reference, as it's an object. It's nothing more then a pointer to the first char in the char array which makes up the "string", and assigning it to a new "string" would do nothing more then point it to a new location. *mumbles* Quote Gamer extraordinaire. Programmer wannabe.
wyrd Posted May 11, 2003 Author Posted May 11, 2003 I just read up on reference types and passing parameters in the MSDN library. It makes sense now according to the way .NET works. I guess I should get C++ off the brain. Oh, and I learned about a new keyword I didn't know about before; out :D Quote Gamer extraordinaire. Programmer wannabe.
Leaders John Posted May 11, 2003 Leaders Posted May 11, 2003 You do know also that all the built in data types are objects as well right, or was that what you found on MSDN? Quote "These Patriot playoff wins are like Ray Charles songs, Nantucket sunsets, and hot fudge sundaes. Each one is better than the last." - Dan Shaughnessy
wyrd Posted May 11, 2003 Author Posted May 11, 2003 :rolleyes: Yes, I know everything in .NET is an object. But I think you knew what I ment when I was talking about the string object (which is a reference type, not a value type) My point was, passing reference types acts a tad differently then what I was thinking for some reason. Quote Gamer extraordinaire. Programmer wannabe.
Leaders John Posted May 12, 2003 Leaders Posted May 12, 2003 ...But I think you knew what I ment when I was talking about the string object (which is a reference type, not a value type)... No, I didn't. Sorry if it seemed that way. I just thought I would point it out in case you (or someone searching the forum in the future) didn't know. Quote "These Patriot playoff wins are like Ray Charles songs, Nantucket sunsets, and hot fudge sundaes. Each one is better than the last." - Dan Shaughnessy
wyrd Posted May 12, 2003 Author Posted May 12, 2003 No, I didn't. Oh. Well I have a habit of not using terms correctly, so I guess I should start paying more attention to what I'm typing. Quote Gamer extraordinaire. Programmer wannabe.
max1mum0v3rdr1v Posted May 27, 2003 Posted May 27, 2003 In case anyone is wondering, this is the correct way to get the above code working. Just need to assign to variable: using System; namespace ConsoleApplication3 { class Class1 { [sTAThread] static void Main(string[] args) { new Class1(); } public Class1() { string stuff = "yayaya"; string newstuff = changeString(stuff); Console.WriteLine("before: " + stuff); Console.WriteLine("after: " + newstuff); Console.Read(); } private string changeString(string str) { str = "changed!"; Console.WriteLine("function changed: " + str); return str; } } } I only posted this for any n00bs out there that were curious as to how to make the above code function the way Wyrd seems to have expected it to. --Sean Quote
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.