0

Is there any way I can mark more than 1 method as public without using the keyword multiple times?

at the moment, I have to do it for every method:

class foo
{
public void doSomething();
public void doAnotherthing();
public int counter;
}

But this get's pretty frustrating if you need to create more than 150 method declarations for a class, so, is there any other way?

Hatted Rooster
  • 35,759
  • 6
  • 62
  • 122
  • 3
    Do not create classes with 150 methods ? (For your question, answer is no). Use the power of intellisense to type as low as possible. – Raphaël Althaus Dec 17 '13 at 17:43
  • 6
    If you need to create class with 150 public methods, then you are doing something wrong – AlwaysAProgrammer Dec 17 '13 at 17:44
  • 1
    I fear you are in for a world of hurt if copying and pasting `public` is troublesome. – TyCobb Dec 17 '13 at 17:49
  • Alas, if you are struggling with adding `public` to classes then the rest is going to be a really nasty shock. (sometimes you have to write `protected internal static` in from of those class statements). You'd better start doing some finger exercises. – BanksySan Dec 17 '13 at 17:57
  • I wouldn't exactly recommend it, but you could come up with a regular expression to look for method declarations not starting with "public" and adding them in. Of course, that would screw up any methods you want to be private. That could possibly be remedied by including the methods you want public in a region and making the regex aware of that. To reiterate what everyone else has said, the short answer is "no". – Sven Grosen Dec 17 '13 at 18:16
  • @ledbutter If the op is unwilling to put in the effort to right public before methods do you think it likely he will be willing to write a regEx? – ford prefect Dec 17 '13 at 19:20
  • @inquisitiveIdiot i try not to ask such questions, just answer the questions as they are posed... – Sven Grosen Dec 17 '13 at 19:39

3 Answers3

1

No the public keyword is required if you want the equivalent of public access. This question should help: What is the difference between Public, Private, Protected, and Nothing?

Also the people who are saying that 150 methods is too many for a single class are right. A large part of the benefit of OOP is modularity. Each class should have a specific purpose and only contain methods specific to that purpose.

Community
  • 1
  • 1
ford prefect
  • 7,096
  • 11
  • 56
  • 83
  • 1
    This is a C# question, so a C# link would be better. Although the concept is the same, there are for example also the `internal` modifier in C#. – Guffa Dec 17 '13 at 17:46
  • 1
    @Guffa my bad you are absolutely right. Really the important takeaway though is that you should include a keyword for each method anyway for readability at very least. – ford prefect Dec 17 '13 at 17:47
0

No because the default modifier is private for the language.

Mike Perrenoud
  • 66,820
  • 29
  • 157
  • 232
0

No, class with 150 method is called Elephant class. If you have 150 methods in class I recommend you to start reading object oriented programming.

By default if you write nothing with method then it is private for public you need to explicitly declare it public.

Zaheer Ahmed
  • 28,160
  • 11
  • 74
  • 110