Reputation: 25
I am attempting to make an application which gets and inputted URL via textbox, retrieves the HtmlCode of that website when a button is clicked and stores both in a dictionary which is then displayed in a listbox.
However, I haven't been able to implement any of it into the user interface yet due to having an issue when trying to add an entry to the dictionary. To retrieve the HtmlCode I am calling the method GetHtml however, i am getting an error when trying to add the website and the code to the dictionary.
Code Follows
namespace HtmlCheck
{
static class Program
{
/// <summary>
/// The main entry point for the application.
/// </summary>
[STAThread]
static void Main()
{
Application.EnableVisualStyles();
Application.SetCompatibleTextRenderingDefault(false);
Application.Run(new Form1());
var dict = new SortedDictionary<string, WebsiteInfo>();
var list = (from entry in dict
orderby entry.Key
select entry.Key).ToList();
}
private static void addPerson(string websiteUrl)
{
dict.Add(websiteUrl, new WebsiteInfo { WebsiteUrl = websiteUrl, HtmlCode = getHtml(websiteUrl) });
}
private static SortedDictionary<string, WebsiteInfo> dict;
public string getHtml(string websiteUrl)
{
using (WebClient client = new WebClient())
return client.DownloadString(websiteUrl);
}
}
public class WebsiteInfo
{
public string WebsiteUrl;
public string HtmlCode;
public override string ToString()
{
string formated = string.Format("{0}\n---------------------------------- \n{1}", WebsiteUrl, HtmlCode);
return formated;
}
}
}
In the method
private static void addPerson(string websiteUrl)
{
dict.Add(websiteUrl, new WebsiteInfo { WebsiteUrl = websiteUrl, HtmlCode = getHtml(websiteUrl) });
}
the HtmlCode = getHtml(websiteUrl
) is throwing an error:
"An object reference is required for the non-static field, method, or property 'HtmlCheck.Program.getHtml(string)'"
So my question is, why cant I add an entry to the dictionary with this information?
Thanks for your time.
Upvotes: 0
Views: 90
Reputation: 57902
addPerson()
is a static method, which means you can call it any time, without having an instance of the class to call it on. It then tries to call getHtml()
, which is non-static, which means it can only be called through a valid instance of the class.
I suggest you do some research on static methods in C# to get your head around this.
Upvotes: 1
Reputation: 3240
Add "static" to your method;
public static string getHtml(string websiteUrl)
Upvotes: 1
Reputation: 66449
You're getting that error because addPerson()
is a static method (it is called without creating an instance of the Program
class), but the getHtml()
method is not static (so you need an instance of the Program
class to call it).
Easy fix - make it static too:
public static string getHtml(string websiteUrl)
{
...
}
For the sake of completeness, you'd have to otherwise create an instance of the Program
class before calling the getHtml()
method:
private static void addPerson(string websiteUrl)
{
var p = new Program();
dict.Add(websiteUrl, new WebsiteInfo { WebsiteUrl = websiteUrl, HtmlCode = p.getHtml(websiteUrl) });
}
Upvotes: 4