Reputation: 127
I have a method in my MVC App that creates a pdf file (takes an object with data to write and path as parameters). I wrote the method in a seperate class and i made it static. In another function on my controler, i call this method like this:
PdfGenerator.GeneratePdfMethod("write this string", "path");
Now if i change this method to non static, i have to instatniate a PdfGenerator object and then call the function on that object:
PdfGenerator p = new PdfGenerator();
p.GeneratePdfMethod("write this string", "path");
Now how can i avoid having to create this object while not making my method static? If it is even possible and advisable?
Upvotes: 1
Views: 808
Reputation: 169150
Now how can i avoid having to create this object while not making my method static? If it is even possible ...?
No. You'll have to create an instance of the class before you can access any of its members, including the GeneratePdfMethod
method.
You can do this on one line though:
new PdfGenerator().GeneratePdfMethod("write this string", "path");
When a member is static
, it doesn't belong to a specific instance but rather to the type itself.
On the opposite, a non-static method belongs to a specific instance which means that you need a reference to this particular instance before you can call the method.
If GeneratePdfMethod
doesn't access any instance data, it should be static
though.
Upvotes: 2