Hi guys, I have come yet again to ask another question. :D
The scenario is like this. -
Using Parser;
-
namespace Crawler
-
{
-
public void Main()
-
{
-
MyUri uri = new MyUri();
-
Parser p = new Parser();
-
p.GetPage(uri);
-
}
-
-
}
-
And I have another namespace which is the parser -
namespace Parser
-
{
-
-
class FormParser
-
{
-
public void GetPage(url)
-
{
-
//whatever coding here...
-
}
-
-
}
-
}
-
And I would like to be able to use uri in Parser. However when I try that, I get this error message: Member "'Parser.FormParser.GetPage(Crawler.MyUri) cannot be accessed with an instance reference; qualify it with a type instead"
I would like to know if there is a better way of doing what I am trying to do. Any help, comments and suggestions would be greatly appreciated. Thank you. :)
2 7783
I think your problem is that parser is both a type and a namespace. Line 7 of the first code block defines a variable of type Parser, but that's your namespace, defined on line 1 of the second code block.
In your main file, you'll need a "using Parser;" statement as well as a project reference to whatever project FormParser is in (if it is in a separate file). Then line 7 would become... - FormParser p = new FormParser();
Or, if you didn't have the using statement and/or wanted to be more specific... - Parser.FormParser p = new Parser.FormParser();
- Using Parser;
-
namespace Crawler
-
{
-
public void Main()
-
{
-
MyUri uri = new MyUri();
-
Parser p = new Parser();
-
p.GetPage(uri);
-
}
-
-
}
The problem here is that you shouldn't make a new Parser. Parser is a namespace, not a class object. You would make a new Parser.FormParser. You can either specify "Parser.FormParser" (just to make sure it is not confused with any other namespac'es "FormParser" class, or since you have already specified you are *using* the namespace Parser you can just make a new FormParser() - namespace Parser
-
{
-
-
class FormParser
-
{
-
public void GetPage(url)
-
{
-
//whatever coding here...
-
}
-
-
}
-
}
You need to make the class FormParser *public* if you want other classes to be able to see it and make new instances of them.
Putting that all together should look something like this... - Using Parser;
-
namespace Crawler
-
{
-
public void Main()
-
{
-
MyUri uri = new MyUri();
-
FormParser fp = new FormParser();
-
p.GetPage(uri);
-
}
-
-
}
-
-
namespace Parser
-
{
-
-
public class FormParser
-
{
-
public void GetPage(url)
-
{
-
//whatever coding here...
-
}
-
-
}
-
}
Let me make another suggest: Not so many namespaces and not such generic namespaces. I'll guarantee you're not the first guy to make a Parser or Crawler namespace. Hopefully you will enjoy coding so much that these are not the only classes you make. This scheme will quickly get out of hand if you don't plan now how you will grow your common code. What about this sort of thing? - Using yllanger.web;
-
public void Main()
-
{
-
// This is the main of your app, it doesn't need it's own namespace
-
// It will already be in the namespace of your project name.
-
MyUri uri = new MyUri();// your *using* statement keeps you from having to specify this as yllanger.web.MyURI but that is what it really is
-
yllanger.web.FormParser fp = new yllanger.web.FormParser(); // just to be specific
-
p.GetPage(uri);
-
}
-
-
namespace yllanger.web
-
{
-
public class FormParser
-
{
-
public void GetPage(url)
-
{
-
//whatever coding here...
-
}
-
}
-
-
public class MyUri
-
{
-
// build your 'MyURI class
-
}
-
// put all your other 'web' classes inside this namespace
-
}
-
-
namespace yllanger.Windows.Forms
-
{
-
// put all your custom windows forms classes here
-
// Notice how we are keeping synchronous with
-
// System.Windows.Forms just so we don't confuse ourselves
-
// 6 months from now, wondering where we put a given class
-
}
-
-
-
You are now on your way to your own framework.
Sign in to post your reply or Sign up for a free account.
Similar topics
by: JohnnySparkles |
last post by:
Hi everyone,
I'm currently writing an application which uses the XmlSerializer class to
serialize/deserialize objects to/from xml.
Now when deserializing an XmlDocument back into the object,...
|
by: Brent Ritchie |
last post by:
Hello all,
I have been using C# in my programming class and I have grown quite fond
of C# properties. Having a method act like a variable that I can control
access to is really something. As...
|
by: Alan Foxmore |
last post by:
I understand about public, internal, protected, etc.
Is there a way to ensure members are accessible within the same
namespace only? In other words, I want to prohibit access to members
outside...
|
by: Mike in Santa Rosa |
last post by:
I'm trying to get a simple c# app built that can launch/manipulate an excel
workbook, sheet. I've chased down several examples and can't any of them to
work. So I must be doing somethnig obviouslt...
|
by: zahy[dot]bnaya[At]gmail[dot]com |
last post by:
Hi all,
Since I am always confusing this, I want to know once and for all what
is the right way of doing this.
I've noticed that some programs use:
std::cout<< "yadayada"<<endl;
|
by: Schizoid Man |
last post by:
Hi,
I'm a novice whose just about migrating to C++ after cutting my teeth on
C for a few years.
To start with I'm using Microsoft's out-of-the-box Visual C++ Express
Edition compiler and I...
|
by: RichWade |
last post by:
Yes, I'm a noob with .NET looking for help.
My goal is to eventually write a managed DLL in C++ that is a wrapper for an unmanaged C DLL, then use the managed DLL in C#, VB, etc.
Initially, I...
|
by: Rich |
last post by:
Yes, I'm a noob with .NET looking for help.
My goal is to eventually write a managed DLL in C++ that is a wrapper
for an unmanaged C DLL, then use the managed DLL in C#, VB, etc.
Initially, I...
|
by: ThunderMusic |
last post by:
Hi,
I have many classes a user may need to call methods on my webservice. Some
classes are "published" and some are not... I mean, when we do a Web
reference from another project, we don't have...
|
by: ryjfgjl |
last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
|
by: emmanuelkatto |
last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud.
Please let me know.
Thanks!
Emmanuel
|
by: BarryA |
last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
|
by: nemocccc |
last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
|
by: Sonnysonu |
last post by:
This is the data of csv file
1 2 3
1 2 3
1 2 3
1 2 3
2 3
2 3
3
the lengths should be different i have to store the data by column-wise with in the specific length.
suppose the i have to...
|
by: marktang |
last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
|
by: Hystou |
last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
|
by: jinu1996 |
last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
|
by: tracyyun |
last post by:
Dear forum friends,
With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
| |