|
-
July 29th, 2005, 06:39 AM
#1
package class
how to declare a class( or a method ) that is only accessible through package (namespace)?
i.e.
Assume that
X is a class in package A
Y is a class in package A
Z is a class in package B
Y can access X
Z can not access X
how to declare such X class?
This is easy in Java. juct declare it as
"class X {}" instead of "public class X {}".
How do we do it in C#?
-
July 29th, 2005, 06:55 AM
#2
Re: package class
You have to make sure that your packages are in different assemblies. Then you can use the internal keyword like:
Code:
internal class myClassA {}
Useful or not? Rate my posting. Thanks.
-
July 29th, 2005, 06:57 AM
#3
Re: package class
Welcome to world of C#. Analogical thinking is good but sometimes leads us into problems. We use namespace instead. You could read about them here. Here is a small example of declaring a class in a namespace.
Code:
namespace MyProjectNamespace
{
public class MyClass
{
public string GetMessage() {
return "Hello, world";
}
}
}
To access the class you need to specify the namespace too or else you need to write with the using declaration. Like this:
Code:
MyProjectNamespace.MyClass objOfMyClass = new MyProjectNamespace.MyClass();
To avoid the lengthy names at all the place you could include the whole namespace within the current/different one with the using declaration. Like this:
Code:
using System;
using MyProjectNamespace;
Look out for the access specifies too. I think there is one more thing that is different here. They keyword "internal". It is used to restrict the scope to the assembly where the class is defined. There's more to it. You can find details here.
Can you help me with my homework assignment?, Before you post!, Use code tags, How to post!, Codeguru technical FAQs, C++ FAQ Lite, Stroustrup: C++ Style and Technique FAQ, Guru of the Week, Comeau C and C++ FAQs, Comeau C++ Templates FAQs, CUJ @ DDJ, Spam threshold
My Blogs : Learning C++ is fun | Abnegator's reflections
Open Threads : C++ Aha! Moments | Nature of work in C++?
-
July 29th, 2005, 07:14 AM
#4
Re: package class
thanks for your posts..
I need a simple info too, after your post:
- how to create/declare different assemblies?..
- does this approach provide compile-time-safety or run-time?
( lets assume that I have 3 different namespaces which each have so many classes and i want to define them in 3 different assemblies.... )
Last edited by the one; July 29th, 2005 at 07:24 AM.
-
July 29th, 2005, 07:38 AM
#5
Re: package class
 Originally Posted by the one
thanks for your posts..
I need a simple info too, after your post:
- how to create/declare different assemblies?..
- does this approach provide compile-time-safety or run-time?
Assemblies AFAIK are just the DLLs that you build in a C# project. You dont need to declare it. You must be familiar with what a DLL is or else look out for the various definitions and their advantages with a search.
 Originally Posted by the one
( lets assume that I have 3 different namespaces which each have so many classes and i want to define them in 3 different assemblies.... )
I have never come across such a conditions until you mentioned it. Just build the three modules with the classes within each namespace seperate out into the 3 different DLLs. In simple words, if classes A, B, C, D are in namespance NS1 then keep NS1 in Assembly/DLL1. And hence put the others in other 2. One more thing to mention - we should better avoid defining more than one class in one .cs file. Seperate them into individual ones. One .cs for one class and go ahead. Take a small tutorial somewhere with a search..
Can you help me with my homework assignment?, Before you post!, Use code tags, How to post!, Codeguru technical FAQs, C++ FAQ Lite, Stroustrup: C++ Style and Technique FAQ, Guru of the Week, Comeau C and C++ FAQs, Comeau C++ Templates FAQs, CUJ @ DDJ, Spam threshold
My Blogs : Learning C++ is fun | Abnegator's reflections
Open Threads : C++ Aha! Moments | Nature of work in C++?
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|