Want to protect your cyber security and still get fast solutions? Ask a secure question today.Go Premium

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 217
  • Last Modified:

Can a static method return an instance of the class its defined in?

If i've got a class, can i have a static method in that class that returns an instace of the class itself?

I'm trying to build a static method on the class that can act as a sort of database lookup - i.e. people could call
Subscription.returnExistingSubscriptionFromDatabase("test006") - and get a Subscription object with its properties populated from the database.  Can i do this?

eg.

private class Subscription()
{
    private string property1
    private string property2
   
    // static method taht could be used to return an instance of this class
    public static subscription returnExistingSubscriptionFromDatabase(String uniqueIdentifier)
    {
         // look up database to find the data for property1 and property2
         // return by calling the class constructor
         ....
         return subscription(valueFromDatabase1, valueFromDatabase2)
    }

    public subscription(string prop1, string prop2)
    {
        this.property1 = prop1
        this.property2 = prop2
     }


}
0
kenshaw
Asked:
kenshaw
1 Solution
 
mpf1748Commented:
Yeah, you should have no problem doing that. You have a few syntax errors, but once you get those worked out, you should be fine. In particular some semicolons (;) and the class definition.
0

Featured Post

Free Tool: SSL Checker

Scans your site and returns information about your SSL implementation and certificate. Helpful for debugging and validating your SSL configuration.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Tackle projects and never again get stuck behind a technical roadblock.
Join Now