2

Got this class with lots of properties. There`s constructor which sets properties to its default values and Clear method. (Clear method here is just an example)

public class Person
{
    public string A;
    public string B;
    public string C;
    ...
    public string Z;

    public Person()
    {
        this.A = "Default value for A";
        this.B = "Default value for B";
        this.C = "Default value for C";
        ...
        this.Z = "Default value for Z";
    }

    public void Clear()
    {
        this = new Person(); // Something like this ???
    }
}

How can I reinitialize class through Clear method?

I mean:

Person p = new Person();

p.A = "Smething goes here for A";
p.B = "Smething goes here for B";
...
// Here do stuff with p
...

p.Clear(); // Here I would like to reinitialize p through the Clear() instead of use p = new Person();

I know I could write a function with all the default values settings and use it in constructor and in Clear method. But... is there a "proper" way instead of workarounds?

2
  • 1
    Do you have any specific reason, why are you trying reuse the instance? Commented Feb 27, 2014 at 11:15
  • avoid memory allocation I guess : which is quite a noble purpose :) Commented Feb 27, 2014 at 11:20

2 Answers 2

5

I'd rather implement initializer:

public class Person
{
    public string A;
    public string B;
    public string C;
    ...
    public string Z;

    private void Ininialize() {
      this.A = "Default value for A";
      this.B = "Default value for B";
      this.C = "Default value for C";
      ...
      this.Z = "Default value for Z";
    }

    public Person()
    {
       Ininialize();
    }

    public void Clear()
    {
       Ininialize();
    }
}

....

Person p = new Person();
...
p.A = "Something goes here for A";
p.B = "Something goes here for B";
...
p.Clear(); // <- return A, B..Z properties to their default values
Sign up to request clarification or add additional context in comments.

4 Comments

Yes... or put Initialize code into Clear method and directly call Clear from constructor : you would avoid one function (which is not a big deal, I agree)
@Kek: I'd rather separate Initialize() and Clear(): often you have to do something additional on Clear(): e.g. ask a permission, invoke an event(s), say "Clearing" and "Cleared" etc.
@Dmitry Thanks for anwser. Basically if I rename Initialize to SomethingElse then its just what i said earlier :) I could write function and use it in constructor and Clear method. I was just **curious** if theres "intended way"; like this in parameters or so..
@majk86: In good old times (Delphi, C++), when "this" was just a pointer you could have done some pretty tricks with it; but now in managed .Net environment we can't.
1

I don't know what you want , but I would do it like this :

   public class Person
{
    public string A;
    public string B;
    public string C;
    ...
    public string Z;

    public Person()
    {
        ResetToDefault();
    }

    public void ResetToDefault()
    {
        this.A = "Default value for A";
        this.B = "Default value for B";
        this.C = "Default value for C";
        ...
        this.Z = "Default value for Z";
    }
}

well , at some point you must give the parameters their values.

When you want to reset it to default.. just do this :

Person person = new Person();
//do your stuff here .....
//when reset it:
person.ResetToDefault();

1 Comment

Basically same as Dmitry wrote.. but thanks anyway :)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.