Jump to content
Xtreme .Net Talk

Recommended Posts

Posted

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:

Gamer extraordinaire. Programmer wannabe.
  • *Gurus*
Posted

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.

MVP, Visual Developer - .NET

 

Now you see why evil will always triumph - because good is dumb.

 

My free .NET Windows Forms Controls and Articles

Posted

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*

Gamer extraordinaire. Programmer wannabe.
Posted

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

Gamer extraordinaire. Programmer wannabe.
  • Leaders
Posted
You do know also that all the built in data types are objects as well right, or was that what you found on MSDN?
"These Patriot playoff wins are like Ray Charles songs, Nantucket sunsets, and hot fudge sundaes. Each one is better than the last." - Dan Shaughnessy
Posted

: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.

Gamer extraordinaire. Programmer wannabe.
  • Leaders
Posted
...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.

"These Patriot playoff wins are like Ray Charles songs, Nantucket sunsets, and hot fudge sundaes. Each one is better than the last." - Dan Shaughnessy
Posted

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.

Gamer extraordinaire. Programmer wannabe.
  • 3 weeks later...
Posted

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

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...