[SOLVED] [C#] Having some problems.

This is a discussion on "[SOLVED] [C#] Having some problems." within the Other Programming Languages section. This forum, and the thread "[SOLVED] [C#] Having some problems. are both part of the Program Your Website category.


 Subscribe in a reader

Go Back   Webforumz.com > Main Forums > Program Your Website > Other Programming Languages

Notices




Reply
 
LinkBack Thread Tools
  #1  
Old Nov 14th, 2007, 21:17
alexgeek's Avatar
Moderator

SuperMember
Join Date: Jul 2007
Location: Webforumz 24/7
Age: 15
Posts: 3,812
Blog Entries: 9
Thanks: 2
Thanked 2 Times in 2 Posts
[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
Last Blog Entry: 3D Chess in your browser! (Mar 14th, 2008)
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!Spurl this Post!Reddit! Wong this Post!
Reply With Quote

  #2  
Old Nov 15th, 2007, 06:46
Highly Reputable Member
Join Date: Apr 2007
Location: Willich, Germany
Age: 20
Posts: 593
Blog Entries: 2
Thanks: 0
Thanked 0 Times in 0 Posts
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();
    }
}
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!Spurl this Post!Reddit! Wong this Post!
Reply With Quote
  #3  
Old Nov 15th, 2007, 07:23
alexgeek's Avatar
Moderator

SuperMember
Join Date: Jul 2007
Location: Webforumz 24/7
Age: 15
Posts: 3,812
Blog Entries: 9
Thanks: 2
Thanked 2 Times in 2 Posts
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
Last Blog Entry: 3D Chess in your browser! (Mar 14th, 2008)
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!Spurl this Post!Reddit! Wong this Post!
Reply With Quote
  #4  
Old Nov 15th, 2007, 12:20
Highly Reputable Member
Join Date: Apr 2007
Location: Willich, Germany
Age: 20
Posts: 593
Blog Entries: 2
Thanks: 0
Thanked 0 Times in 0 Posts
Re: [C#] Having some problems.

Arrays are not so cool in C#. Check out ArrayLists or Lists.
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!Spurl this Post!Reddit! Wong this Post!
Reply With Quote
  #5  
Old Nov 15th, 2007, 16:32
alexgeek's Avatar
Moderator

SuperMember
Join Date: Jul 2007
Location: Webforumz 24/7
Age: 15
Posts: 3,812
Blog Entries: 9
Thanks: 2
Thanked 2 Times in 2 Posts
Re: [C#] Having some problems.

Uh what's the difference?
Last Blog Entry: 3D Chess in your browser! (Mar 14th, 2008)
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!Spurl this Post!Reddit! Wong this Post!
Reply With Quote
  #6  
Old Nov 15th, 2007, 17:40
Highly Reputable Member
Join Date: Apr 2007
Location: Willich, Germany
Age: 20
Posts: 593
Blog Entries: 2
Thanks: 0
Thanked 0 Times in 0 Posts
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).
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!Spurl this Post!Reddit! Wong this Post!
Reply With Quote
  #7  
Old Nov 15th, 2007, 17:48
alexgeek's Avatar
Moderator

SuperMember
Join Date: Jul 2007
Location: Webforumz 24/7
Age: 15
Posts: 3,812
Blog Entries: 9
Thanks: 2
Thanked 2 Times in 2 Posts
Re: [SOLVED] [C#] Having some problems.

Got it working thanks to you and some other people on other forums
Thanks +rep
Last Blog Entry: 3D Chess in your browser! (Mar 14th, 2008)
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!Spurl this Post!Reddit! Wong this Post!
Reply With Quote
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On

Similar Threads
Thread Thread Starter Forum Replies Last Post
[SOLVED] Fly-out menu problems karinne Web Page Design 13 Jan 30th, 2008 13:45
[SOLVED] Problems with arrays Scream JavaScript Forum 2 Jan 10th, 2008 16:15
[SOLVED] Problems with centering in CSS. mcdanielnc89 Web Page Design 26 Nov 1st, 2007 06:07
[SOLVED] Php code problems longstand PHP Forum 3 Oct 15th, 2007 10:53
[SOLVED] Server Problems businessservicesuk Webforumz Cafe 11 Oct 8th, 2007 15:01


All times are GMT. The time now is 11:22.


Powered by vBulletin®
Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Search Engine Optimization by vBSEO 3.2.0 RC8
© 2003-2008 Webforumz.com : All Rights Reserved

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42