Actually, I'm unclear what you what to do. The following works as you'd
expect:
public class MyClass
{
public static void Main()
{
MyClass m = new MyClass();
Console.WriteLine(m.Foo(10, ""));
}
public string Foo(int n, string str)
{
if (n == 0)
return str;
else
return String.Format("{0}-{1}", n, Foo(n-1, str));
}
}
==========================================
This also works as you'd expect:
public class MyClass
{
public static void Main()
{
MyClass m = new MyClass();
string str = "";
m.Foo2(10, ref str);
Console.WriteLine(str);
}
public void Foo2(int n, ref string str)
{
if (n == 0)
return ;
else
{
str += '-' + n.ToString();
Foo2(n-1, ref str);
return ;
}
}
}
================================================
This, on the other hand, seems to show the error you are getting:
public class MyClass
{
public static void Main()
{
MyClass m = new MyClass();
m.Foo2(10, ref "");
Console.WriteLine(str); /// what do you output here?
RL();
}
public void Foo2(int n, ref string str)
{
if (n == 0)
return ;
else
{
str += '-' + n.ToString();
Foo2(n-1, ref str);
return ;
}
}
}
========================================
That won't compile, but even if it did, it wouldn't work, because Foo2 is
generating output, but aren't saving it.
"ref" says "I'm giving you a place to put the answer", but you expressly
DON'T want to give it such a place. The problem isn't with the compile, but
with your code, which is not consistent with itself.
""
cannot be passed by ref as it is not an lvalue (as I suggested, it is on
the stack not the heap.)
No. "" is not an lvalue because it is NEITHER on the stack frame NOR on
the heap.
--
--
Truth,
James Curran
[erstwhile VC++ MVP]
Home:
www.noveltheory.com Work:
www.njtheater.com
Blog:
www.honestillusion.com Day Job:
www.partsearch.com