Static function

  • Thread starter Thread starter csharpula csharp
  • Start date Start date
C

csharpula csharp

Hello,
I am parsing some main arguments and calling to functions according to
it. Is it possible not to define the called functions as static? How can
I design it better?
Thanks
 
You can create a helper class containing those methods, then
instantiate the helper class in the main() method, e.g.

public class Program {
public static void Main(string[] args) {
ArgumentHandler handler = new ArgumentHandler(args);
}
}

public class ArgumentHandler {
public ArgumentHandler(string[] args) {
ProcessArgs(args);
}

private void ProcessArgs(string[] args) {
// do something with the command-line arguments
}
}
 
Hi csharpula,

why do you think, the function shouldn't be static. Your post is very
unclear about, what you want to acomplish. Maybe a static method is simply
the best solution in your case.
 
Main is a static function, so the function called by Main should be
statis. It's very clear.
 
Hooyoo said:
Main is a static function, so the function called by Main should be
statis. It's very clear.

Not really - it's perfectly possible to call non-static methods from
Main; you just need to create an instance of something on which to call
the instance method. For example:

using System;

class Counter
{
int limit;

Counter (int limit)
{
this.limit = limit;
}

void Start()
{
for (int i=0; i < limit; i++)
{
Console.WriteLine (i);
}
}

static void Main(string[] args)
{
Counter counter = new Counter(int.Parse(args[0]));
counter.Start();
}
}
 
Aha, I knew that. I mean Main call some functions not some methods of
a object, that is the question.
Jon said:
Hooyoo said:
Main is a static function, so the function called by Main should be
statis. It's very clear.

Not really - it's perfectly possible to call non-static methods from
Main; you just need to create an instance of something on which to call
the instance method. For example:

using System;

class Counter
{
int limit;

Counter (int limit)
{
this.limit = limit;
}

void Start()
{
for (int i=0; i < limit; i++)
{
Console.WriteLine (i);
}
}

static void Main(string[] args)
{
Counter counter = new Counter(int.Parse(args[0]));
counter.Start();
}
}
 
Hooyoo said:
Aha, I knew that. I mean Main call some functions not some methods of
a object, that is the question.

Well, I had assumed that "function" was being used as a synonym for
"method" seeing as Main is a static *method* and there's no such thing
as a "function" in C#.
 
You misunderstood me, see the following snippet:
using System;


class Counter
{
int limit;


Counter (int limit)
{
this.limit = limit;
}


void Start()
{
for (int i=0; i < limit; i++)
{
Console.WriteLine (i);
}
}


static void Main(string[] args)
{
/*Start menthod is non-static method, so you cannot write like
this:
Start();
so some smart guys will write like this right way:
*/
Counter counter = new Counter(int.Parse(args[0]));
counter.Start(); //You cannot call
//* Like passing "this" pointer to static member functions in
c++
}

}
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Back
Top