mtwilson Posted August 22, 2004 Posted August 22, 2004 I am probably just be being a bit stupid but... public MyClass( string s) : this( s, true ) public MyClass( string s, bool b ) { ... } That works fine, but what if I want to alter the string in the string in the first constructor before passing it on to the second? public MyClass( string s) { this( s + "muh", true); } public MyClass( string s, bool b ) { ... } That obviously doesn't work and I have tried other things so what is the solution? Unless moving the shared code into a seperate method is the only way. Thanks -Martin Quote
Administrators PlausiblyDamp Posted August 22, 2004 Administrators Posted August 22, 2004 you will have to move the code into a seperate method and call that from both constructors. Quote Posting Guidelines FAQ Post Formatting Intellectuals solve problems; geniuses prevent them. -- Albert Einstein
Joe Mamma Posted August 22, 2004 Posted August 22, 2004 That obviously doesn't work and I have tried other things so what is the solution? Unless moving the shared code into a seperate method is the only way. Thanks -MartinI believe so. . . I am wondering if swithcing the hierarchy around might be appropriate . . . public class MyClass { string _s; public MyClass(string s, bool b):this(s) { // do something based on b and/or s here } public MyClass(string s) { // default s handling } } or a polymorphic hierarchy - public class MyClass { protected string _s; protected virtual void Manipulate(ref string s) { // default does nothing to s } public MyClass(string s, bool b) { Manipulate(s); _s = s; } } public class MyChildClass:MyClass { protected override void Manipulate(ref string s) { s += ": Manipulated"; } public MyChildClass(string s):base(s,true) { } } Quote Joe Mamma Amendment 4: The right of the people to be secure in their persons, houses, papers, and effects, against unreasonable searches and seizures, shall not be violated, and no warrants shall issue, but upon probable cause, supported by oath or affirmation, and particularly describing the place to be searched, and the persons or things to be seized. Amendment 9: The enumeration in the Constitution, of certain rights, shall not be construed to deny or disparage others retained by the people.
*Experts* Nerseus Posted August 23, 2004 *Experts* Posted August 23, 2004 If it's as simple as appending a string, you can append it in the first example: public MyClass( string s) : this( s + "muh", true ) But why would you be altering the string that the caller is passing in? Why not alter it in the "real" constructor? If it's meant to be a flag, maybe you need yet another constructor to pass in what you really want...? -ner Quote "I want to stand as close to the edge as I can without going over. Out on the edge you see all the kinds of things you can't see from the center." - Kurt Vonnegut
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.