| View previous topic :: View next topic |
| Author |
Message |
tanjiajun_34 Grandmaster Cheater
Reputation: 0
Joined: 16 Feb 2006 Posts: 786 Location: Singapore
|
Posted: Sat May 29, 2010 11:07 pm Post subject: C# Method Question |
|
|
I am learning C# in school. I have a question about method.
May I know whats the private for?
| Code: | private bool IsPrime(int num)
{
} |
But
| Code: | bool IsPrime(int num)
{
}
|
Works too?...
|
|
| Back to top |
|
 |
Flyte Peanuts!!!!
Reputation: 6
Joined: 19 Apr 2006 Posts: 1887 Location: Canada
|
Posted: Sat May 29, 2010 11:36 pm Post subject: |
|
|
| In a class everything is private by default. So yes, it is optional in a class but you should add it anyways (at least, I do).
|
|
| Back to top |
|
 |
tanjiajun_34 Grandmaster Cheater
Reputation: 0
Joined: 16 Feb 2006 Posts: 786 Location: Singapore
|
Posted: Sun May 30, 2010 8:35 am Post subject: |
|
|
| Oh. So, if I type in public, what will happen? What the different with private and public?
|
|
| Back to top |
|
 |
TROLOLOLOLOLOLOLOLOLOLOLO Expert Cheater
Reputation: -1
Joined: 27 Dec 2009 Posts: 100
|
Posted: Sun May 30, 2010 11:17 am Post subject: |
|
|
| Public allows it to be accessed by other methods. Private dos not.
|
|
| Back to top |
|
 |
tombana Master Cheater
Reputation: 2
Joined: 14 Jun 2007 Posts: 456 Location: The Netherlands
|
Posted: Sun May 30, 2010 12:24 pm Post subject: |
|
|
| CometJack wrote: | | Public allows it to be accessed by other methods. Private dos not. |
Not completely correct. public allows the methods to be used from other classes, private only allows it to be used from methods in the same class.
|
|
| Back to top |
|
 |
Flyte Peanuts!!!!
Reputation: 6
Joined: 19 Apr 2006 Posts: 1887 Location: Canada
|
Posted: Sun May 30, 2010 12:34 pm Post subject: |
|
|
| tombana wrote: | | CometJack wrote: | | Public allows it to be accessed by other methods. Private dos not. |
Not completely correct. public allows the methods to be used from other classes, private only allows it to be used from methods in the same class. |
There are two other access modifiers as well. 'protected' allows inheriting classes to use the method, and 'internal' is essentially 'public' but only to classes in the same assembly.
|
|
| Back to top |
|
 |
potaters Grandmaster Cheater
Reputation: 72
Joined: 13 Apr 2009 Posts: 969
|
Posted: Wed Jun 02, 2010 2:14 pm Post subject: |
|
|
| CometJack wrote: | | Public allows it to be accessed by other methods. Private dos not. |
I'm pretty sure that's Java.
|
|
| Back to top |
|
 |
TROLOLOLOLOLOLOLOLOLOLOLO Expert Cheater
Reputation: -1
Joined: 27 Dec 2009 Posts: 100
|
Posted: Wed Jun 02, 2010 7:04 pm Post subject: |
|
|
| xen0r wrote: | | CometJack wrote: | | Public allows it to be accessed by other methods. Private dos not. |
I'm pretty sure that's Java. |
Nah, I think it's just about every programming language that has that feature Though I am not 100% correct, as I was corrected in a post above.
|
|
| Back to top |
|
 |
potaters Grandmaster Cheater
Reputation: 72
Joined: 13 Apr 2009 Posts: 969
|
Posted: Wed Jun 02, 2010 7:07 pm Post subject: |
|
|
| CometJack wrote: | | xen0r wrote: | | CometJack wrote: | | Public allows it to be accessed by other methods. Private dos not. |
I'm pretty sure that's Java. |
Nah, I think it's just about every programming language that has that feature Though I am not 100% correct, as I was corrected in a post above. |
I thought it was different for C#, meh, I've only done a little C#.
|
|
| Back to top |
|
 |
TROLOLOLOLOLOLOLOLOLOLOLO Expert Cheater
Reputation: -1
Joined: 27 Dec 2009 Posts: 100
|
Posted: Wed Jun 02, 2010 7:28 pm Post subject: |
|
|
| xen0r wrote: | | CometJack wrote: | | xen0r wrote: | | CometJack wrote: | | Public allows it to be accessed by other methods. Private dos not. |
I'm pretty sure that's Java. |
Nah, I think it's just about every programming language that has that feature Though I am not 100% correct, as I was corrected in a post above. |
I thought it was different for C#, meh, I've only done a little C#. |
It's okay I'm not a master at C# though I've done quite a bit of simple applications and few difficult ones.
You can check some of my released C# sources here -> http://forum.cheatcity.org/forum-52.html
|
|
| Back to top |
|
 |
1929394839292057839194958 Grandmaster Cheater Supreme
Reputation: 130
Joined: 22 Dec 2006 Posts: 1509
|
Posted: Thu Jun 03, 2010 3:40 am Post subject: |
|
|
| xen0r wrote: | | CometJack wrote: | | xen0r wrote: | | CometJack wrote: | | Public allows it to be accessed by other methods. Private dos not. |
I'm pretty sure that's Java. |
Nah, I think it's just about every programming language that has that feature Though I am not 100% correct, as I was corrected in a post above. |
I thought it was different for C#, meh, I've only done a little C#. | C# (according to a fair few experts) is a lot like a mixture of C++ and Java.
|
|
| Back to top |
|
 |
TROLOLOLOLOLOLOLOLOLOLOLO Expert Cheater
Reputation: -1
Joined: 27 Dec 2009 Posts: 100
|
Posted: Sat Jun 05, 2010 4:24 pm Post subject: |
|
|
| konr wrote: | | xen0r wrote: | | CometJack wrote: | | xen0r wrote: | | CometJack wrote: | | Public allows it to be accessed by other methods. Private dos not. |
I'm pretty sure that's Java. |
Nah, I think it's just about every programming language that has that feature Though I am not 100% correct, as I was corrected in a post above. |
I thought it was different for C#, meh, I've only done a little C#. | C# (according to a fair few experts) is a lot like a mixture of C++ and Java. |
Yep
You get all the nice features of C++ with the easy-to-learn/use syntax of Java!
You get the best of both worlds so-to-say.
|
|
| Back to top |
|
 |
atom0s Moderator
Reputation: 205
Joined: 25 Jan 2006 Posts: 8587 Location: 127.0.0.1
|
Posted: Sat Jun 12, 2010 7:02 pm Post subject: |
|
|
'private' does not stop other methods inside the same object from accessing the function in C#. Anything within the same object, in this case a class, is part of the same scope, which has access to each other.
Quick example:
| Code: |
public class TestClass
{
public TestClass()
{
}
private int Method1()
{
return 12345;
}
public int Method2()
{
return Method1();
}
} |
| Code: | TestClass tClass = new TestClass();
int nValue = tClass.Method2();
MessageBox.Show(nValue.ToString()); |
Method2 has access to Method1 regardless of it being private since they are defined within the same object. However, you cannot access Method1 outside of this instance without using Method2 due to Method1 being private.
_________________
- Retired. |
|
| Back to top |
|
 |
tanjiajun_34 Grandmaster Cheater
Reputation: 0
Joined: 16 Feb 2006 Posts: 786 Location: Singapore
|
Posted: Sat Jun 12, 2010 10:10 pm Post subject: |
|
|
| So, to sum up, private methods cannot be access outside the class? Public can?
|
|
| Back to top |
|
 |
atom0s Moderator
Reputation: 205
Joined: 25 Jan 2006 Posts: 8587 Location: 127.0.0.1
|
Posted: Sun Jun 13, 2010 1:26 am Post subject: |
|
|
| tanjiajun_34 wrote: | | So, to sum up, private methods cannot be access outside the class? Public can? |
Yes.
_________________
- Retired. |
|
| Back to top |
|
 |
|