Connecting Tech Pros Worldwide Help | Site Map

public vs private access, quick question-

z_learning_tester
Guest
 
Posts: n/a
#1: Nov 16 '05
Quick question-
What happens if you have a private class with a public static method?
Can you still say the following?
Lets say you are making this call from another class, say class2...

int myVal = class1.method();

It seems that you should not be able to, after all from class2, you should
not be able to see the private class1, so it's public static method is
effectively wasted.

Please correct me if I'm wrong.
Thanks,

Jeff


Daniel O'Connell [C# MVP]
Guest
 
Posts: n/a
#2: Nov 16 '05

re: public vs private access, quick question-



"z_learning_tester" <someone@microsoft.com> wrote in message
news:RIPBc.88157$HG.75569@attbi_s53...[color=blue]
> Quick question-
> What happens if you have a private class with a public static method?
> Can you still say the following?
> Lets say you are making this call from another class, say class2...
>[/color]

There are no private classes(atleast, not at top level, nested classes can
be private). For non-nested classes(whats the term one would use here,
anyway?) there are only two accessbilities: public and internal. A class
without a specifier is internal and accessible to every other class in that
assembly.
[color=blue]
> int myVal = class1.method();
>
> It seems that you should not be able to, after all from class2, you should
> not be able to see the private class1, so it's public static method is
> effectively wasted.
>
> Please correct me if I'm wrong.
> Thanks,
>
> Jeff
>
>[/color]


z_learning_tester
Guest
 
Posts: n/a
#3: Nov 16 '05

re: public vs private access, quick question-


Great, just what I needed.
Thanks!

Jeff

"Daniel O'Connell [C# MVP]" <onyxkirx@--NOSPAM--comcast.net> wrote in
message news:uNluTpCWEHA.712@TK2MSFTNGP11.phx.gbl...[color=blue]
>
> "z_learning_tester" <someone@microsoft.com> wrote in message
> news:RIPBc.88157$HG.75569@attbi_s53...[color=green]
> > Quick question-
> > What happens if you have a private class with a public static method?
> > Can you still say the following?
> > Lets say you are making this call from another class, say class2...
> >[/color]
>
> There are no private classes(atleast, not at top level, nested classes can
> be private). For non-nested classes(whats the term one would use here,
> anyway?) there are only two accessbilities: public and internal. A class
> without a specifier is internal and accessible to every other class in[/color]
that[color=blue]
> assembly.
>[color=green]
> > int myVal = class1.method();
> >
> > It seems that you should not be able to, after all from class2, you[/color][/color]
should[color=blue][color=green]
> > not be able to see the private class1, so it's public static method is
> > effectively wasted.
> >
> > Please correct me if I'm wrong.
> > Thanks,
> >
> > Jeff
> >
> >[/color]
>
>[/color]


Eli Block
Guest
 
Posts: n/a
#4: Nov 16 '05

re: public vs private access, quick question-


Jeff,

Remember that private classes can only be declared INSIDE another
class. The private class member would only be accessible by the class
that it is inside, and in turn, the static public method as well. Here
is a quick example:

public class MainClass
{
public MainClass()
{
// you can call the static public method from here
MyPrivateClass.TestMethod();
}

// private classes can only be declared inside another class
private class MyPrivateClass
{
public MyPrivateClass()
{
// it works here as well of course
TestMethod();
}

public static void TestMethod()
{
}
}
}

public class AnotherClass
{
public AnotherClass()
{
// it doesn't work here, MyPrivateClass isn't accessible
MainClass.MyPrivateClass.TestMethod();
}
}

Hope that clears things up.

"z_learning_tester" <someone@microsoft.com> wrote in message news:<RIPBc.88157$HG.75569@attbi_s53>...[color=blue]
> Quick question-
> What happens if you have a private class with a public static method?
> Can you still say the following?
> Lets say you are making this call from another class, say class2...
>
> int myVal = class1.method();
>
> It seems that you should not be able to, after all from class2, you should
> not be able to see the private class1, so it's public static method is
> effectively wasted.
>
> Please correct me if I'm wrong.
> Thanks,
>
> Jeff[/color]
z_learning_tester
Guest
 
Posts: n/a
#5: Nov 16 '05

re: public vs private access, quick question-


Thanks for the response.
Your example explained my question perfectly :-)

But your example begs another question-
You say "private class MyPrivateClass" then use a public constructor "public
MyPrivateClass()"
Is that right?
Sorry I still get confused on the basics sometimes. I think constructors are
always public even if their class is private, though it seems like a
contradiction...

Oops, then another question- you are calling the method "TestMethod()" from
inside the constructor.
Can you do that?
All I have seen so far inside constructors is variable initializations.
Again, my book is bad. Very bad.

Thanks!

Jeff



"Eli Block" <eblock@gmail.com> wrote in message
news:76c6f39.0406220527.716f5478@posting.google.co m...[color=blue]
> Jeff,
>
> Remember that private classes can only be declared INSIDE another
> class. The private class member would only be accessible by the class
> that it is inside, and in turn, the static public method as well. Here
> is a quick example:
>
> public class MainClass
> {
> public MainClass()
> {
> // you can call the static public method from here
> MyPrivateClass.TestMethod();
> }
>
> // private classes can only be declared inside another class
> private class MyPrivateClass
> {
> public MyPrivateClass()
> {
> // it works here as well of course
> TestMethod();
> }
>
> public static void TestMethod()
> {
> }
> }
>}
>
> public class AnotherClass
> {
> public AnotherClass()
> {
> // it doesn't work here, MyPrivateClass isn't accessible
> MainClass.MyPrivateClass.TestMethod();
> }
> }
>
> Hope that clears things up.
>
> "z_learning_tester" <someone@microsoft.com> wrote in message[/color]
news:<RIPBc.88157$HG.75569@attbi_s53>...[color=blue][color=green]
> > Quick question-
> > What happens if you have a private class with a public static method?
> > Can you still say the following?
> > Lets say you are making this call from another class, say class2...
> >
> > int myVal = class1.method();
> >
> > It seems that you should not be able to, after all from class2, you[/color][/color]
should[color=blue][color=green]
> > not be able to see the private class1, so it's public static method is
> > effectively wasted.
> >
> > Please correct me if I'm wrong.
> > Thanks,
> >
> > Jeff[/color][/color]


Jon Skeet [C# MVP]
Guest
 
Posts: n/a
#6: Nov 16 '05

re: public vs private access, quick question-


z_learning_tester <someone@microsoft.com> wrote:[color=blue]
> Thanks for the response.
> Your example explained my question perfectly :-)
>
> But your example begs another question-
> You say "private class MyPrivateClass" then use a public constructor "public
> MyPrivateClass()"
> Is that right?
> Sorry I still get confused on the basics sometimes. I think constructors are
> always public even if their class is private, though it seems like a
> contradiction...[/color]

No, constructors aren't always public. You can have private
constructors which would only be able to be called within the class. If
a private class has a public (or internal) constructor, then the
enclosing class will be able to call it.
[color=blue]
> Oops, then another question- you are calling the method "TestMethod()" from
> inside the constructor.
> Can you do that?[/color]

You certainly can. It's generally a bad idea to call *virtual* methods
inside constructors though.

--
Jon Skeet - <skeet@pobox.com>
http://www.pobox.com/~skeet
If replying to the group, please do not mail me too
z_learning_tester
Guest
 
Posts: n/a
#7: Nov 16 '05

re: public vs private access, quick question-


Well, I'm definitely learning more than just one 'something new' every day
;-)
Thanks again!

Jeff


"Jon Skeet [C# MVP]" <skeet@pobox.com> wrote in message
news:MPG.1b4285376edc943c98ad58@msnews.microsoft.c om...[color=blue]
> z_learning_tester <someone@microsoft.com> wrote:[color=green]
> > Thanks for the response.
> > Your example explained my question perfectly :-)
> >
> > But your example begs another question-
> > You say "private class MyPrivateClass" then use a public constructor[/color][/color]
"public[color=blue][color=green]
> > MyPrivateClass()"
> > Is that right?
> > Sorry I still get confused on the basics sometimes. I think constructors[/color][/color]
are[color=blue][color=green]
> > always public even if their class is private, though it seems like a
> > contradiction...[/color]
>
> No, constructors aren't always public. You can have private
> constructors which would only be able to be called within the class. If
> a private class has a public (or internal) constructor, then the
> enclosing class will be able to call it.
>[color=green]
> > Oops, then another question- you are calling the method "TestMethod()"[/color][/color]
from[color=blue][color=green]
> > inside the constructor.
> > Can you do that?[/color]
>
> You certainly can. It's generally a bad idea to call *virtual* methods
> inside constructors though.
>
> --
> Jon Skeet - <skeet@pobox.com>
> http://www.pobox.com/~skeet
> If replying to the group, please do not mail me too[/color]


Closed Thread


Similar C# / C Sharp bytes