Rick
Rick

Reputation: 79

C# Is it possible to call a method from a property within a class?

So I'm pretty new to programming, and I try to learn new things by trying a lot of things. But at the moment I got stuck on something...

Basically what I am trying to accomplish is to be able to use the following line of code: Url.QueryStringParser.HasParam(""); The Url is the class, QueryStringParser a property within the Url class and the HasParam is a method within the QueryStringParser class. And it work, expect for the fact that I get a NullReferenceException on the QueryStringParser propperty. And I can't figure out why this happens...

This is the Url class.

public class Url
{
    public static QueryStringParser QueryStringParser { get; private set;}
}

This is the QueryStringParser class.

public class QueryStringParser
{
    public bool HasParam(string parameter)
    {
        return !string.IsNullOrEmpty(HttpContext.Current.Request[parameter]);
    }
}

This is my asp.net web page.

if (Url.QueryStringParser.HasParam("Id"))
{ 
    // Do stuff.
}

So my question is if it's possible to create something as I've just mentioned above?

Upvotes: 0

Views: 225

Answers (3)

user1902142
user1902142

Reputation:

public class Url
{
    public static QueryStringParser QueryStringParser { get; private set; }
}

This is not right you need to make a instance of the class to access its methods and property. I think it would be a good learning exercise for you to research this and resolve it.

Upvotes: 0

Keith Nicholas
Keith Nicholas

Reputation: 44316

All you need to do is assign it an instance in the static constructor of URL:

public class Url
{
     public static Url()
     { 
         QueryStringParser = new QueryStringParser();
     }

     public static QueryStringParser QueryStringParser { get; private set;}
}

Upvotes: 0

Steve
Steve

Reputation: 216343

Your problem lies in the fact that the property QueryStringParser, to call the method HasParam, needs an instance of an object QueryStringParser (bad choice of names). This instance is never created by your code, thus the NullReferenceException.
So you need to change a bit of code in the get and set of your property

public class Url
{
    private static QueryStringParser _qsp = null;
    public static QueryStringParser QueryStringParser 
    { 
       get
       {
          if(_qsp == null) _qsp = new QueryStringParser();
          return _qsp;
       }
       private set
       { 
           _qsp = value;
       }
    }
}

With this change, when you need the property your code will check if it is null and initializes the internal object used to store the instance of QueryStringParser, then returns this instance.

Another possibility to bypass the exception is to made your HasParam method also static and in this case you don't need an instance of the object QueryStringParser.

By the way, while it works, I find extremely confusing to have a property named as a class. I suggest to choose another name for the property QueryStringParser

Upvotes: 2

Related Questions