473,395 Members | 1,668 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,395 software developers and data experts.

Using NameSpace members In another namespace

Hi guys, I have come yet again to ask another question. :D

The scenario is like this.

Expand|Select|Wrap|Line Numbers
  1. Using Parser;
  2. namespace Crawler
  3. {
  4.    public void Main()
  5. {
  6.      MyUri uri = new MyUri();
  7.      Parser p = new Parser();
  8.      p.GetPage(uri);
  9. }
  10.  
  11. }
  12.  
And I have another namespace which is the parser
Expand|Select|Wrap|Line Numbers
  1. namespace Parser
  2. {
  3.  
  4. class FormParser
  5. {
  6.      public void GetPage(url)
  7.      {
  8.     //whatever coding here...
  9.       }
  10.  
  11. }
  12. }
  13.  
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. :)
Sep 18 '09 #1
2 7783
GaryTexmo
1,501 Expert 1GB
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...

Expand|Select|Wrap|Line Numbers
  1. FormParser p = new FormParser();
Or, if you didn't have the using statement and/or wanted to be more specific...

Expand|Select|Wrap|Line Numbers
  1. Parser.FormParser p = new Parser.FormParser();
Sep 18 '09 #2
tlhintoq
3,525 Expert 2GB
Expand|Select|Wrap|Line Numbers
  1. Using Parser;
  2. namespace Crawler
  3. {
  4.    public void Main()
  5. {
  6.      MyUri uri = new MyUri();
  7.      Parser p = new Parser();
  8.      p.GetPage(uri);
  9. }
  10.  
  11. }
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()

Expand|Select|Wrap|Line Numbers
  1. namespace Parser
  2. {
  3.  
  4. class FormParser
  5. {
  6.      public void GetPage(url)
  7.      {
  8.     //whatever coding here...
  9.       }
  10.  
  11. }
  12. }
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...

Expand|Select|Wrap|Line Numbers
  1. Using Parser;
  2. namespace Crawler
  3. {
  4.    public void Main()
  5. {
  6.      MyUri uri = new MyUri();
  7.      FormParser fp = new FormParser();
  8.      p.GetPage(uri);
  9. }
  10.  
  11. }
  12.  
  13. namespace Parser
  14. {
  15.  
  16. public class FormParser
  17. {
  18.      public void GetPage(url)
  19.      {
  20.     //whatever coding here...
  21.       }
  22.  
  23. }
  24. }
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?

Expand|Select|Wrap|Line Numbers
  1. Using yllanger.web;
  2. public void Main()
  3. {
  4.      // This is the main of  your app, it doesn't need it's own namespace
  5.      // It will already be in the namespace of your project name.
  6.      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
  7.      yllanger.web.FormParser fp = new yllanger.web.FormParser(); // just to be specific
  8.      p.GetPage(uri);
  9. }
  10.  
  11. namespace yllanger.web
  12. {
  13. public class FormParser
  14. {
  15.      public void GetPage(url)
  16.      {
  17.     //whatever coding here...
  18.       } 
  19. }
  20.  
  21. public class MyUri
  22. {
  23.     // build your 'MyURI class
  24. }
  25. // put all your other 'web' classes inside this namespace
  26. }
  27.  
  28. namespace yllanger.Windows.Forms
  29. {
  30.   // put all your custom windows forms classes here
  31.   //  Notice how we are keeping synchronous with
  32.   //  System.Windows.Forms   just so we don't confuse ourselves
  33.   //  6 months from now, wondering where we put a given class
  34. }
  35.  
  36.  
  37.  
You are now on your way to your own framework.
Sep 18 '09 #3

Sign in to post your reply or Sign up for a free account.

Similar topics

2
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,...
11
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...
7
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...
5
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...
7
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;
3
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...
1
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...
6
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...
1
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...
0
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...
0
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
0
BarryA
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...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
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...
0
marktang
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,...
0
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...
0
jinu1996
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...
0
tracyyun
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...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.