2

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.

6
  • What error are you getting? Commented Jan 5, 2018 at 12:23
  • Remove static keyword from A0 and A1 Commented Jan 5, 2018 at 12:24
  • Why are the properties static? Commented Jan 5, 2018 at 12:24
  • 1
    Or keep static and reference them as PageSize.A0 etc. Commented Jan 5, 2018 at 12:25
  • No I cant edit pagesize class @FCin Commented Jan 5, 2018 at 12:27

5 Answers 5

2

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.

Sign up to request clarification or add additional context in comments.

7 Comments

No I want to pass this as an argument because value might change as required.
@abhishek So what if the value changes? I fail to see the difference. You seem to be misunderstanding what static really is.
The class which i am using is the system class and I won't be able to change so this question arised
@abhishek I'm having trouble following your train of thought here. What does using custom values have to do with anything? If you need values other than the ones defined in PageSize then use them! How is PageSize even relevant in that scenario?
See method does not know about the pagesize to give so i am passing argument in the method which will tell pagesize, now how to pass this var and convert it to typeof pagesize
|
1

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;

3 Comments

Can i do like this? Var a0=PageSize.A0; before calling method and then pass this var as argument to the method
Yes, but you also can do it directly. But with var a0=PageSize.A0, the effect will be the same.
No actually this method does not know about the pagesize to give so the argument in the method will tell pagesize now how to pass this var and convert it to typeof pagesize
1

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.

Comments

0

You need to access A0 with PageSize.A0 instead of s.A0

Comments

0

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);
}

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.