newbie: string reference

  • Thread starter Thread starter sklett
  • Start date Start date
S

sklett

I know there aren't pointers in C#, well, there are but... you know...

I have a situation there I need to have a reference to string a and then
later, string b.

I hoped I could do something like;
<code>
string a;
string b;

string ref refStr = a;
// some code, then the switch
refStr = b;
</code>


This doesn't seem possible or at least syntactically I'm failing.
Any takers? Any ideas?

Thanks,
Steve
 
Steve, you don't need the "ref" keyword except to pass parameters to
functions.

Test out the following code:

string a = "123";
string b = "456";
string c = a;

System.Diagnostics.Debug.Writeline (string.Format("a={0}, b={1}, c={2}", a,
b,c);

And, of course, don't use 1-character variable names in your real world
code! ;-)

-jv
 
When you say

string a;

a _is_ a reference. Perhaps if you would give more information about
what you hope to do with string references, we could give you more
help.

By the way, if what you want is to pass a string into a method, and
upon return have it change to a different string, you can do this using
ref:

private void MyMethod(ref string x)
{
x = "Goodbye";
}

string a = "Hello";
MyMethod(ref a);
// At this point a will be "Goodbye".
 
Another point of note here that may be of importance to the original poster
is that String is immutable -- the value of an instance never changes.

string str;

str = "abc";
str += "xyz";

Despite what the code looks like, the last line causes a new instance of
String to be allocated, and the reference to "abc" is orphaned.
 
Yes. This is most obvious when altering the string in a method:

string a = "abc";
AppendXyz(a);
// At this point a is still "abc"
AppendXyzWithRef(ref a);
// At this point a is "abcxyz"

private void AppendXyz(string str)
{
str += "xyz";
}

private void AppendXyzWithRef(ref string str)
{
str += "xyz";
}
 
wow, that is great participation from you all, thank you very much.

This appears to be a classic case of over complicated things. I didn't
realize that a string variable by itself was a reference type.

What I want to do is while parsing a text file, transfer the current line
into the appropriate field in my class.
So I will have a switch that will handle keywords as they are encountered in
the filem IE:
<code>
@@Description
This is the description section of the file...

@@Title
This is where the title will be...
</code>

So I will set my "portal" string to the correct reference depending on the
context of the file, IE:
<code>
string strPortal;

// code to retreive the current keyword
if(strKeyword == Description)
{
strPortal = MyClass.Description;
}
else if(strKeyword == Title)
{
strPortal = MyClass.Title;
}

//
// then as I read the contents of each section, I concat them into the
strPortal which is in turn assigning to my class' appropriate field
</code>

I hope that makes more sense. It might not be the best way, but it has
always worked for me in the past, I'm just used to using a char* ;)

Thanks again all, that was a great effort! Have a good weekend.
 
Back
Top