Menu inside console application

  • Thread starter Thread starter Arjen
  • Start date Start date
A

Arjen

Hello,

I have made a main class. (main.cs)
I also have made a menu class. (menu.cs)
The reason that I separate is is just for the code overview.

Now I want to see the menu calling from the main.
The problem is is that I don't want to make an object...
Is there not a way that I call the menu as procedure or function?

Thanks!
 
More information about my question.

using System;
namespace Test {
static void Main(string[] args) {
// How to get my menu??? like showmenu();

}
}



using System;
namespace Test
{
/// <summary>
/// Summary description for showMenu.
/// </summary>
public showmenu() {
// some code
}
}


Must this be public? static?

I just want to access it like I said above.

Thanks!
 
Arjen said:
Hello,

I have made a main class. (main.cs)
I also have made a menu class. (menu.cs)
The reason that I separate is is just for the code overview.

Now I want to see the menu calling from the main.
The problem is is that I don't want to make an object...
Is there not a way that I call the menu as procedure or function?

Thanks!

Well, you could implement static methods in your menu class and call
them, without having to make an instance of your menu class.

e.g.:

class Menu
{
public static StaticMethod()
{
}
}

//...

static void Main()
{
Menu.StaticMethod();
}
 
Arjen said:
More information about my question.

using System;
namespace Test {
static void Main(string[] args) {
// How to get my menu??? like showmenu();

}
}



using System;
namespace Test
{
/// <summary>
/// Summary description for showMenu.
/// </summary>
public showmenu() {
// some code
}
}

You can't do that in C# -- namespaces are for holding classes only.

You'd have to do something like this instead:

namespace Test
{

public class Menu
{
public static Show()
{
}
}

}

Note that the name has changed from "ShowMenu" to "Show", since "Show"
is implemented in the "Menu" class.

Then call this static method with: Menu.Show();

<snip>
 
C# doesn't knows functions, procedures and contructors?

Thanks!




C# Learner said:
Arjen said:
More information about my question.

using System;
namespace Test {
static void Main(string[] args) {
// How to get my menu??? like showmenu();

}
}



using System;
namespace Test
{
/// <summary>
/// Summary description for showMenu.
/// </summary>
public showmenu() {
// some code
}
}

You can't do that in C# -- namespaces are for holding classes only.

You'd have to do something like this instead:

namespace Test
{

public class Menu
{
public static Show()
{
}
}

}

Note that the name has changed from "ShowMenu" to "Show", since "Show"
is implemented in the "Menu" class.

Then call this static method with: Menu.Show();

<snip>
 
Arjen said:
C# doesn't knows functions, procedures and contructors?

Thanks!

In Delphi (formerly known as Object Pascal), a function is a routine
that explicitly returns a value, and a procedure is a routine which
doesn't. I'm presuming this is what you mean here.

In C#, you can only define *methods*, which are functions or routines
of a class.

You can define either instance methods or static methods. Instance
methods are called upon an instance of a class, whereas static methods
aren't.

Static methods are, in a sense, functions/procedures which need to be
called with the *class name* as a prefix.

Here:

namespace MyNamespace
{

public class MyClass
{
public static void Procedure()
{
}

public static int Function()
{
}
}

You can call either of these like:

MyClass.Procedure();
MyClass.Function();

Note that you are *not* creating an instance of MyClass here, you are
simply calling its static methods.

As for constructors, C# has them, and their meaning is similar to
those of other languages:

public class MyClass
{
void MyClass()
{
Console.WriteLine("I am a constructor of MyClass!");
}
}

This is similar to the following Delphi code:

interface

type TMyClass = class
public
constructor Create;
end;

implementation

constructor TMyClass.Create;
begin
WriteLn("I am a constructor of TMyClass!");
end;
 
In Delphi (formerly known as Object Pascal), a function is a routine
that explicitly returns a value, and a procedure is a routine which
doesn't. I'm presuming this is what you mean here.
Yes. ;-)

Thanks for your answer!
Arjen
 
Back
Top