473,399 Members | 3,656 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,399 software developers and data experts.

Protecting classes

Hi,

I want to be able to make a class only visible within the namespace, i.e.
only other classes within the namespace will have access to it.

I have tried putting a protected keyword against it. That doesn't work.

I have tried nesting it inside the class where I want to use it, this sort
of compiles, except some of my methods that rely on the class have
"Inconsistent accessibility:"

Here is an example...

public class Folders
{

class FolderChild
{
}

public static FolderChild[] GetFolders()
{
}

}

Basically, I don't want FolderChild to be visible to anything else except
Folders and its methods. How can I do that?

Thanks.

Best regards,
Dave Colliver.
http://www.AshfieldFOCUS.com
~~
http://www.FOCUSPortals.com - Local franchises available
Jan 19 '07 #1
12 1567
David <da*****************@revilloc.REMOVETHIS.comwrot e:
I want to be able to make a class only visible within the namespace, i.e.
only other classes within the namespace will have access to it.
You can't - there's no such access level in .NET.
--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet Blog: http://www.msmvps.com/jon.skeet
If replying to the group, please do not mail me too
Jan 19 '07 #2
Hi,
Basically, I don't want FolderChild to be visible to anything else except
Folders and its methods. How can I do that?
Then why is the static method that returns an array of FolderChild objects
public? That means that FolderChild must be visible to all assemblies.
Make the method private, since your code example already accomplishes your
goal of having FolderChild only visible to the Folders class - FolderChild
is a private, nested class.

--
Dave Sexton
http://davesexton.com/blog

"David" <da*****************@revilloc.REMOVETHIS.comwrot e in message
news:%2****************@TK2MSFTNGP03.phx.gbl...
Hi,

I want to be able to make a class only visible within the namespace, i.e.
only other classes within the namespace will have access to it.

I have tried putting a protected keyword against it. That doesn't work.

I have tried nesting it inside the class where I want to use it, this sort
of compiles, except some of my methods that rely on the class have
"Inconsistent accessibility:"

Here is an example...

public class Folders
{

class FolderChild
{
}

public static FolderChild[] GetFolders()
{
}

}

Basically, I don't want FolderChild to be visible to anything else except
Folders and its methods. How can I do that?

Thanks.

Best regards,
Dave Colliver.
http://www.AshfieldFOCUS.com
~~
http://www.FOCUSPortals.com - Local franchises available

Jan 19 '07 #3
Hi,

This has confused me now...

The FolderChild[] GetFolders needs to be accessible from outside the class
(so that I can do Folders.GetFolders() ). As you can see, the FolderChild
is actually a class that handles an array, and I don't want direct access to
that (from the outside, the array could be manipulated directly, which is
not what I want.)

I may have misunderstood. If I have, can you expand what you mean?

Thanks.

Best regards,
Dave Colliver.
http://www.AshfieldFOCUS.com
~~
http://www.FOCUSPortals.com - Local franchises available

"Dave Sexton" <dave@jwa[remove.this]online.comwrote in message
news:ee**************@TK2MSFTNGP04.phx.gbl...
Hi,
>Basically, I don't want FolderChild to be visible to anything else except
Folders and its methods. How can I do that?

Then why is the static method that returns an array of FolderChild objects
public? That means that FolderChild must be visible to all assemblies.
Make the method private, since your code example already accomplishes your
goal of having FolderChild only visible to the Folders class - FolderChild
is a private, nested class.

--
Dave Sexton
http://davesexton.com/blog

"David" <da*****************@revilloc.REMOVETHIS.comwrot e in message
news:%2****************@TK2MSFTNGP03.phx.gbl...
>Hi,

I want to be able to make a class only visible within the namespace, i.e.
only other classes within the namespace will have access to it.

I have tried putting a protected keyword against it. That doesn't work.

I have tried nesting it inside the class where I want to use it, this
sort of compiles, except some of my methods that rely on the class have
"Inconsistent accessibility:"

Here is an example...

public class Folders
{

class FolderChild
{
}

public static FolderChild[] GetFolders()
{
}

}

Basically, I don't want FolderChild to be visible to anything else except
Folders and its methods. How can I do that?

Thanks.

Best regards,
Dave Colliver.
http://www.AshfieldFOCUS.com
~~
http://www.FOCUSPortals.com - Local franchises available


Jan 19 '07 #4
Hi David,

If that is the only reason why you want this unusual requirement then its a
easy solution.

Make a method in the class that returns Folders.GetFolders() which is public
.. All other methods and the array make private

Now no outside class will be able to get or access the array directly, and
you can still get the things you need.
"David" <da*****************@revilloc.REMOVETHIS.comwrot e in message
news:Ox**************@TK2MSFTNGP02.phx.gbl...
Hi,

This has confused me now...

The FolderChild[] GetFolders needs to be accessible from outside the class
(so that I can do Folders.GetFolders() ). As you can see, the FolderChild
is actually a class that handles an array, and I don't want direct access
to that (from the outside, the array could be manipulated directly, which
is not what I want.)

I may have misunderstood. If I have, can you expand what you mean?

Thanks.

Best regards,
Dave Colliver.
http://www.AshfieldFOCUS.com
~~
http://www.FOCUSPortals.com - Local franchises available

"Dave Sexton" <dave@jwa[remove.this]online.comwrote in message
news:ee**************@TK2MSFTNGP04.phx.gbl...
>Hi,
>>Basically, I don't want FolderChild to be visible to anything else
except Folders and its methods. How can I do that?

Then why is the static method that returns an array of FolderChild
objects public? That means that FolderChild must be visible to all
assemblies. Make the method private, since your code example already
accomplishes your goal of having FolderChild only visible to the Folders
class - FolderChild is a private, nested class.

--
Dave Sexton
http://davesexton.com/blog

"David" <da*****************@revilloc.REMOVETHIS.comwrot e in message
news:%2****************@TK2MSFTNGP03.phx.gbl...
>>Hi,

I want to be able to make a class only visible within the namespace,
i.e. only other classes within the namespace will have access to it.

I have tried putting a protected keyword against it. That doesn't work.

I have tried nesting it inside the class where I want to use it, this
sort of compiles, except some of my methods that rely on the class have
"Inconsistent accessibility:"

Here is an example...

public class Folders
{

class FolderChild
{
}

public static FolderChild[] GetFolders()
{
}

}

Basically, I don't want FolderChild to be visible to anything else
except Folders and its methods. How can I do that?

Thanks.

Best regards,
Dave Colliver.
http://www.AshfieldFOCUS.com
~~
http://www.FOCUSPortals.com - Local franchises available



Jan 20 '07 #5
Hi David,
The FolderChild[] GetFolders needs to be accessible from outside the class
(so that I can do Folders.GetFolders() ).
That means that the FolderChild class must have public access too. If it
doesn't, then how could you return an array of FolderChild objects from a
public method? This is what the compiler is complaining about. Either the
static method must be private or the FolderChild class must be public.
As you can see, the FolderChild is actually a class that handles an array,
and I don't want direct access to that (from the outside, the array could
be manipulated directly, which is not what I want.)
Actually, your code shows me that the FolderChild class has no members and a
public, static method returns an array of FolderChild instances. I think
you may want to just make the FolderChild class public if you need to access
it from the "outside". Otherwise, Daniel's suggestion to make it private
and provide a public wrapper is a good one, but you'll have to return
IEnumerable<Folderor something like that.

Anyway, why does your Folder class need a FolderChild class? Can't Folder
just return a list of Folders?

2.0 Framework example:

class Folder
{
public IEnumerable<FolderFolders
{
get
{
foreach (Folder folder in GetFoldersSomehow())
yield return folder;
}
}
}

BTW, arrays are immutable, so you wouldn't have to worry about a caller
manipulating the returned FolderChild[] if you decide to stick with that
route. However, your FolderChild classes within the array might be mutable,
depending upon your design, but you should try to make them immutable since
its a nested class (in which case it probably shouldn't be public or else
shouldn't be nested in the first place).

--
Dave Sexton
http://davesexton.com/blog

"David" <da*****************@revilloc.REMOVETHIS.comwrot e in message
news:Ox**************@TK2MSFTNGP02.phx.gbl...
Hi,

This has confused me now...

The FolderChild[] GetFolders needs to be accessible from outside the class
(so that I can do Folders.GetFolders() ). As you can see, the FolderChild
is actually a class that handles an array, and I don't want direct access
to that (from the outside, the array could be manipulated directly, which
is not what I want.)

I may have misunderstood. If I have, can you expand what you mean?

Thanks.

Best regards,
Dave Colliver.
http://www.AshfieldFOCUS.com
~~
http://www.FOCUSPortals.com - Local franchises available

"Dave Sexton" <dave@jwa[remove.this]online.comwrote in message
news:ee**************@TK2MSFTNGP04.phx.gbl...
>Hi,
>>Basically, I don't want FolderChild to be visible to anything else
except Folders and its methods. How can I do that?

Then why is the static method that returns an array of FolderChild
objects public? That means that FolderChild must be visible to all
assemblies. Make the method private, since your code example already
accomplishes your goal of having FolderChild only visible to the Folders
class - FolderChild is a private, nested class.

--
Dave Sexton
http://davesexton.com/blog

"David" <da*****************@revilloc.REMOVETHIS.comwrot e in message
news:%2****************@TK2MSFTNGP03.phx.gbl...
>>Hi,

I want to be able to make a class only visible within the namespace,
i.e. only other classes within the namespace will have access to it.

I have tried putting a protected keyword against it. That doesn't work.

I have tried nesting it inside the class where I want to use it, this
sort of compiles, except some of my methods that rely on the class have
"Inconsistent accessibility:"

Here is an example...

public class Folders
{

class FolderChild
{
}

public static FolderChild[] GetFolders()
{
}

}

Basically, I don't want FolderChild to be visible to anything else
except Folders and its methods. How can I do that?

Thanks.

Best regards,
Dave Colliver.
http://www.AshfieldFOCUS.com
~~
http://www.FOCUSPortals.com - Local franchises available



Jan 20 '07 #6
Dave Sexton wrote:
[...]
BTW, arrays are immutable, so you wouldn't have to worry about a caller
manipulating the returned FolderChild[] if you decide to stick with that
route. However, your FolderChild classes within the array might be mutable,
depending upon your design, but you should try to make them immutable since
its a nested class (in which case it probably shouldn't be public or else
shouldn't be nested in the first place).
Er... since when are arrays immutable? The caller couldn't change the
*length* of the array, but he could certainly change the contents.

Jesse

Jan 20 '07 #7
Hi Jesse,

Lol - that's embarrassing. I didn't think that through all the way - thanks
for correcting me :)

If the OP wants immutability then a generic ReadOnlyCollection should be
returned instead of an Array.

--
Dave Sexton
http://davesexton.com/blog

"Jesse McGrew" <jm*****@gmail.comwrote in message
news:11*********************@11g2000cwr.googlegrou ps.com...
Dave Sexton wrote:
[...]
>BTW, arrays are immutable, so you wouldn't have to worry about a caller
manipulating the returned FolderChild[] if you decide to stick with that
route. However, your FolderChild classes within the array might be
mutable,
depending upon your design, but you should try to make them immutable
since
its a nested class (in which case it probably shouldn't be public or else
shouldn't be nested in the first place).

Er... since when are arrays immutable? The caller couldn't change the
*length* of the array, but he could certainly change the contents.

Jesse

Jan 20 '07 #8
Hi all that have responded. Thank you for taking the time to help me.

I am using C# 1.1.

I have been developing with C# for a couple of years, after coming from a
Classic ASP background of which was 6 years. What I am trying to say is that
whilst I am confident in programming, I am really pushing my boundaries with
this self imposed project. This may make me appear a little slow to
understand what is coming across.

I did note that the sample was in .NET 2.0 with the IEnumerable<Folder>,
which of course, I can't use at the moment.

Back to earlier comments in the thread...

The method from Daniel, I am not sure how to approach. An example will help.

Info from Dave Sexton with the 2.0 framework example, I didn't give all the
code. My FolderChild class has a public constructor which reads a DataRow,
which populates private variables, which are accessed through public
properties...

e.g.

public class FolderChild
{
// Private variables.
private string MyPrivateVar;
...

public FolderChild(object drObj)
{
// Populate the private variables.
MyPrivateVar = WhateverFromDataRow;
}

public string MyVar
{
get { return MyPrivateVar; }
}

}

The idea being to use it as....

foreach (Folder fd in Folder.GetFolders())
{
Response.Write(fd.MyVar);
}

At the moment, this class is not nested. It doesn't need to be nested, just
made so that accessibility to it is not easily available. It is not too big
a deal if it is accessible, as even if it was used improperly, hardly
anything will come of it. It will just make a "virtual" FolderChild which
cannot really be used without the backend support. All I am just trying to
do is to try and remove the possibility of accidental misuse which will lead
to unnecessary support calls. I guess I will have to put in comments in the
code documentation to steer future developers from using it.

Best regards,
Dave Colliver.
http://www.LeedsFOCUS.com
~~
http://www.FOCUSPortals.com - Local franchises available
"Dave Sexton" <dave@jwa[remove.this]online.comwrote in message
news:e3**************@TK2MSFTNGP05.phx.gbl...
Hi Jesse,

Lol - that's embarrassing. I didn't think that through all the way -
thanks for correcting me :)

If the OP wants immutability then a generic ReadOnlyCollection should be
returned instead of an Array.

--
Dave Sexton
http://davesexton.com/blog

"Jesse McGrew" <jm*****@gmail.comwrote in message
news:11*********************@11g2000cwr.googlegrou ps.com...
>Dave Sexton wrote:
[...]
>>BTW, arrays are immutable, so you wouldn't have to worry about a caller
manipulating the returned FolderChild[] if you decide to stick with that
route. However, your FolderChild classes within the array might be
mutable,
depending upon your design, but you should try to make them immutable
since
its a nested class (in which case it probably shouldn't be public or
else
shouldn't be nested in the first place).

Er... since when are arrays immutable? The caller couldn't change the
*length* of the array, but he could certainly change the contents.

Jesse


Jan 20 '07 #9
Hi David,

<snip>
Info from Dave Sexton with the 2.0 framework example, I didn't give all
the code. My FolderChild class has a public constructor which reads a
DataRow, which populates private variables, which are accessed through
public properties...

e.g.

public class FolderChild
{
// Private variables.
private string MyPrivateVar;
...

public FolderChild(object drObj)
{
// Populate the private variables.
MyPrivateVar = WhateverFromDataRow;
}

public string MyVar
{
get { return MyPrivateVar; }
}

}

The idea being to use it as....

foreach (Folder fd in Folder.GetFolders())
{
Response.Write(fd.MyVar);
}
But FolderChild cannot be coerced into a Folder unless it derives from
Folder, which it doesn't in your examples. And if it derives from Folder,
then you should probably just use Folder instead. My example used generics,
but it's not necessary at all if you're using an earlier version of the .NET
Framework. For instance, you can return an ICollection, IEnumerable,
ArrayList or an Array as in your original example. The point is to only use
one class that aggregates a collection of itself. In other words, unless
you have specific semantics or functionality to be encapsulated in a
"FolderChild" class, it isn't useful at all.

Here's an example that assumes you have created a strong-typed collection
that implements the ICollection interface. It's your choice whether you
want to make the implementation a read-only collection:

public class FolderCollection : ICollection
{
...
}

public class Folder
{
public string Name { get { return name; } }

public FolderCollection GetFolders()
{
get { return folders; }
}

private readonly string name;
private readonly FolderCollection folders;

public Folder()
{
// assuming your FolderCollection class takes the "owner"
// as a parameter...
folders = new FolderCollection(this);
}

public Folder(DataRow row)
: this()
{
name = row["Name"] as string;

// TODO: you may want to fill the folders collection here
}
}
This way, you can have an unlimited chain of Folders, not just a single
level deep as in your examples.
At the moment, this class is not nested. It doesn't need to be nested,
just made so that accessibility to it is not easily available. It is not
too big a deal if it is accessible, as even if it was used improperly,
hardly anything will come of it. It will just make a "virtual" FolderChild
which cannot really be used without the backend support. All I am just
trying to do is to try and remove the possibility of accidental misuse
which will lead to unnecessary support calls. I guess I will have to put
in comments in the code documentation to steer future developers from
using it.
I suspect that the FolderChild class isn't necessary at all. (see above)

--
Dave Sexton
http://davesexton.com/blog
Jan 21 '07 #10
I'm not quite sure I understand your problem correctly, but to answer
your initial question, the internal keyword should make objects
invisible to anything outside their namespace.
Sincerely,
Kevin Wienhold

David schrieb:
Hi,

I want to be able to make a class only visible within the namespace, i.e.
only other classes within the namespace will have access to it.

I have tried putting a protected keyword against it. That doesn't work.

I have tried nesting it inside the class where I want to use it, this sort
of compiles, except some of my methods that rely on the class have
"Inconsistent accessibility:"

Here is an example...

public class Folders
{

class FolderChild
{
}

public static FolderChild[] GetFolders()
{
}

}

Basically, I don't want FolderChild to be visible to anything else except
Folders and its methods. How can I do that?

Thanks.

Best regards,
Dave Colliver.
http://www.AshfieldFOCUS.com
~~
http://www.FOCUSPortals.com - Local franchises available
Jan 22 '07 #11
internal applies to assemblies, not namespaces; It sounds like (as the
example shows) you want FolderChild to be private - but in this case
you can't have it as the return value from a public method, as how
would the caller use it? It can't see FolderChild!

Do you mean that the external caller should be able to see the object
but not create them? In this case, perhaps make the FolderChild class
public with an internal constructor? Or alternatively consider an
interface / base class so the caller knows about the properties but
not the FolderChild class (but this seems overkill).

Marc
Jan 22 '07 #12
KWienhold <he******@trashmail.netwrote:
I'm not quite sure I understand your problem correctly, but to answer
your initial question, the internal keyword should make objects
invisible to anything outside their namespace.
No, internal restricts visibility to the *assembly*, which is not the
same as the namespace.

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet Blog: http://www.msmvps.com/jon.skeet
If replying to the group, please do not mail me too
Jan 25 '07 #13

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

24
by: Yang Li Ke | last post by:
Hi guys! Anyone know a way so that users purchasing my scripts would not be able to share them with other people ? Yang
6
by: nell | last post by:
Hi all, I've developed a testing application in python, and should supply it in a way that no one (lets say they are regular users) will understand it and edit it. The application source is all...
12
by: Roland Hall | last post by:
I read Aaron's article: http://www.aspfaq.com/show.asp?id=2276 re: protecting images from linked to by other sites. There is a link at the bottom of that page that references an interesting...
2
by: Pierre Phaneuf | last post by:
At my workplace, we are in the process of migrating to a component system similar to COM (but not COM itself, as we have some space and portability requirements) that uses refcounting for resource...
5
by: Kerem Gümrükcü | last post by:
Hi, how can i protect a assembly against disassembling with ILDASM and other products like that. i have a dll with some encryption methods implemented and i dont want them to be exposed, to...
4
by: KenA | last post by:
Suppose that in the class named the ProductDB.cs I want to protect the method logic from being accessed from other classes, but I still want to let other classes to call the method. The protected...
4
by: Krista Lemieux | last post by:
Hello, I know this is probably a hudge topic to discuss and there are lots of different ways of implementation, but I still would like to ask and hear the most commonly used techniques for this....
12
by: Dr. Edmund M. Hayes | last post by:
I wrote a access program that works well enough that a handful of people would like to buy it from me. My problem is that if I sell it to someone there is no mechanism that I know of to protect...
2
by: Nikolaus Rath | last post by:
Hello, I am really surprised that I am asking this question on the mailing list, but I really couldn't find it on python.org/doc. Why is there no proper way to protect an instance variable...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
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
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...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

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.