0

For example, I have a static class that contain all default methods. What if I want to generate a properties and simultaneously generate a default static method---

static class Default
{
    //Auto-Generated
    static int DEFAULT_foo1()
    {
        //Do something
    }
    static float DEFAULT_var2
    {
        //Do something
    }

}

class Other
{
    //Code-Snippet
    int var1
    {
        get
        {
            return Default.DEFAULT_var1();
        }
    }
    float var2
    {
        get
        {
            return Default.DEFAULT_var2();
        }
    }
}
1

2 Answers 2

2

I think standard inheritance be a good solution.

class OtherBase
{
    //Code-Snippet
    int var1
    {
        get
        {
            return Default.DEFAULT_var1();
        }
    }
    float var2
    {
        get
        {
            return Default.DEFAULT_var2();
        }
    }
}

Derived class:

class Other : OtherBase
{
}
Sign up to request clarification or add additional context in comments.

2 Comments

What I am trying to ask is when I use code-snippet to create template code on Other's class, how would I make it so that it will generate another piece of code on the Default static class simultaneously. This really has nothing to do with the hierarchy design, just want to know if code-snippet would allow me to auto-generate piece of code on another class while I am creating properties for this class.
If your code snippet could include the code directly (which means duplicate code... eewww...) or it could simply include a base class which implements code.
0

Change the class Default to be a singleton instead of being static. Now you can implement the method as well as the property in the same class with a code snippet. Other classes can derive from Default and inherit the properties automatically.

class Default 
{
    public static readonly Default Instance = new Default();

    protected Default ()
    {
    }

    public static int DoFoo1() 
    { 
        //Do something 
    }

    public int Foo1 { get { return DoFoo1(); } }

    public static float DoVar2 
    { 
        //Do something 
    } 

    public float Var2 { get { return DoVar2(); } }
} 

class Other : Default
{
    // Inherits Foo1 and Var2 automatically
}

Use of Default and Other

int x = Default.DoFoo1();
int y = Default.Instance.Foo1;
Other other = new Other();
int z = other.Foo1;

Comments

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.