Reputation: 315
I am trying access properties of particular inbulit class in c# but i am not getting how to pass a class static variable and pass as an argument to a method.
Here is the class I want to pass as argument:
public class PageSize
{
//
public static readonly Rectangle A0;
//
public static readonly Rectangle A1;
}
Now I have method where i want to pass the variables of the above class as argument like this:
public void NewExportToPDFforLetter(StringWriter sw, string FileName, PageSize s )
{
//s = new PageSize();
StringReader sr = new StringReader(sw.ToString());
Document pdfDoc = new Document(s.A0, 30f, 30f, 30f, 30f);
}
I am getting error at s.A0. Any help will be appreciated.
Upvotes: 2
Views: 4916
Reputation: 315
This is the way you can do this
var size= PageSize.A4;
NewExportToPDFforLetter(stringWriter, "Application Approval Letter", HttpContext.Current.Response,(Rectangle)size);
public void NewExportToPDFforLetter(StringWriter sw, string FileName, HttpResponse Response, Rectangle s )
{
Document pdfDoc = new Document(s, 30f, 30f, 30f, 30f);
}
Upvotes: 0
Reputation: 33186
If you want to keep the properties static, you can access them directly using PageSize.A0
and PageSize.A1
.
If you want to access them from an instance, you will have to remove the static declaration of the properties.
Upvotes: 1
Reputation: 32750
Now I have method where i want to pass the variables of the above class as argument like this:
public void NewExportToPDFforLetter(StringWriter sw, string FileName, PageSize s )
And why would you want to do that? PageSize
is already accesible inside NewExportToPDFforLetter
without the need to pass it as an argument:
public void NewExportToPDFforLetter(
StringWriter sw,
string FileName)
{
var a0 = PageSize.A0; //accesible as long as `Page` is accesible:
//public class, internal in same assembly, etc.
//...
}
If PageSize
is not accesible from wherever NewExportToPDFforLetter
is implemented, then you wouldn't be able to pass it as an argument to begin with, so the option you are considering is simply unecessary.
Upvotes: 2
Reputation: 15357
Since static class variables are not part of an instance, you should not use the instance.property syntax but classname.property.
Thus change
Document pdfDoc = new Document(s.A0, 30f, 30f, 30f, 30f);
to
Document pdfDoc = new Document(PageSize.A0, 30f, 30f, 30f, 30f);
However, it's not visible, but probably you do not want to use static. In that case instead of the above change, you have to remove the statics by changing
public static readonly Rectangle A0;
//
public static readonly Rectangle A1;
to
public readonly Rectangle A0;
//
public readonly Rectangle A1;
Upvotes: 1