Jump to content
Xtreme .Net Talk

Recommended Posts

Posted

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

Posted

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

I 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)
 {
 }
}

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*
Posted

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

"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

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