| Welcome to Webforumz.com. |
|
Nov 14th, 2007, 21:17
|
#1 (permalink)
|
|
Administrator
Join Date: Jul 2007
Location: Webforumz 24/7
Age: 15
Posts: 4,102
|
[SOLVED] [C#] Having some problems.
Creating a web application that finds some tags and displays them
I've run into some errors and I'm completely stuck!
Here is my code:
- Code: Select all
using System;
using System.Text.RegularExpressions;
struct info
{
public string output;
public Match header;
public Match title;
public string URL;
public void results()
{
Console.WriteLine("-=-RESULTS-=-");
Console.WriteLine("Website Page Title (title): {0}", title);
Console.WriteLine("Website header (h1): {0}", header);
}
public static Match find(string reg) {
Regex r = new Regex(reg);
Match m = r.Match(output);
return m;
}
}
class WebApp
{
public static void Main()
{
info web;
System.Console.Write("Enter a URL to analyze then press enter.\n e.g. http://www.alexgeek.co.uk \n >");
string input = System.Console.ReadLine();
if (input == null)
{
web.URL = "http://www.alexgeek.co.uk";
}
else
{
web.URL = input;
}
web.output = new System.Net.WebClient().DownloadString(web.URL);
web.header = web.find(@"<h1>(.)+</h1>");
web.results();
}
}
And the errors:
- Code: Select all
fileWrite.cs(19,27): error CS0120: An object reference is required for the
nonstatic field, method, or property 'info.output'
fileWrite.cs(7,19): (Location of symbol related to previous error)
fileWrite.cs(40,22): error CS0176: Static member 'info.find(string)' cannot be
accessed with an instance reference; qualify it with a type name instead
fileWrite.cs(17,25): (Location of symbol related to previous error)
I've not a clue what do! Please help
__________________
Languages: PHP, mySQL (queries), C#, (X)html, CSS, JS.
|
|
|
Nov 15th, 2007, 06:46
|
#2 (permalink)
|
Join Date: Apr 2007
Location: Willich, Germany
Age: 20
Posts: 612
|
Re: [C#] Having some problems.
You've got a couple of problems.
1. You didn't instantiate your "web" variable. You have to say:
- Code: Select all
info web=new info();
2. If you want to call a function using an instance of your struct (web.find("//")) then the function can't be static.
That's all the errors I could find, but while I'm at it thought I'd also drop a few comments about your programming logic (I'm feeling a bit patronizing this morning....).
If I was you I wouldn't have "info" be a struct, a class would make more sense. I would then make the "find" function private and the class would then find and store the page elements. Elements would be written into private string variables using functions and then use getters/setters to make then visible (read-only). Output would then be done in the main program, not the in the class.
I haven't tested this, but it does compile....:
- Code: Select all
class info {
private string h1;
public string H1 {
get {
return h1;
}
}
public void findH1() {
h1=find(@"<h1>(.)+</h1>");
}
private string find(string reg) {
Regex r=new Regex(reg);
Match m=r.Match(output);
return m.ToString();
}
}
__________________
Web design is the creation of digital environments that facilitate and encourage human activity; reflect or adapt to individual voices and content; and change gracefully over time while always retaining their identity.
~ www.c010depunkk.com ~ the hang-out of a web developer
|
|
|
Nov 15th, 2007, 07:23
|
#3 (permalink)
|
|
Administrator
Join Date: Jul 2007
Location: Webforumz 24/7
Age: 15
Posts: 4,102
|
Re: [C#] Having some problems.
I've got it fixed thanks  I may try your method if I have chance! thanks.
needed to instantiate it and remove static from function.
Do you know how I could put all the matches of <h2>(.)+</h2> into an array (public header2)?
I'm pretty stumped 
__________________
Languages: PHP, mySQL (queries), C#, (X)html, CSS, JS.
|
|
|
Nov 15th, 2007, 12:20
|
#4 (permalink)
|
Join Date: Apr 2007
Location: Willich, Germany
Age: 20
Posts: 612
|
Re: [C#] Having some problems.
Arrays are not so cool in C#. Check out ArrayLists or Lists.
__________________
Web design is the creation of digital environments that facilitate and encourage human activity; reflect or adapt to individual voices and content; and change gracefully over time while always retaining their identity.
~ www.c010depunkk.com ~ the hang-out of a web developer
|
|
|
Nov 15th, 2007, 16:32
|
#5 (permalink)
|
|
Administrator
Join Date: Jul 2007
Location: Webforumz 24/7
Age: 15
Posts: 4,102
|
Re: [C#] Having some problems.
Uh what's the difference?
__________________
Languages: PHP, mySQL (queries), C#, (X)html, CSS, JS.
|
|
|
Nov 15th, 2007, 17:40
|
#6 (permalink)
|
Join Date: Apr 2007
Location: Willich, Germany
Age: 20
Posts: 612
|
Re: [C#] Having some problems.
ArrayLists are dynamic collections of objects. You can add and remove elements on the fly. Arrays have a set size that can't be changed once they've been initialized. A List is like an ArrayList but it has a Type (string, int, double).
__________________
Web design is the creation of digital environments that facilitate and encourage human activity; reflect or adapt to individual voices and content; and change gracefully over time while always retaining their identity.
~ www.c010depunkk.com ~ the hang-out of a web developer
|
|
|
Nov 15th, 2007, 17:48
|
#7 (permalink)
|
|
Administrator
Join Date: Jul 2007
Location: Webforumz 24/7
Age: 15
Posts: 4,102
|
Re: [SOLVED] [C#] Having some problems.
Got it working thanks to you and some other people on other forums 
Thanks +rep
__________________
Languages: PHP, mySQL (queries), C#, (X)html, CSS, JS.
|
|
|
| Thread Tools |
|
|
| Rate This Thread |
|
|
Posting Rules
|
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts
HTML code is Off
|
|
|
|
|
|