Newbie Question: Help with methods

  • Thread starter Thread starter Keith Smith
  • Start date Start date
K

Keith Smith

I know you guys can help me with this...I think I'm missing something fairly
simple.

I'm trying to simply create a method. Here is my method...

string x()
{
MessageBox.Show("hello");
}

....And then later on in my code I would like to call the method like this...

//code
x();

But I get errors every time I use the above method. Maybe I am putting the
method in the wrong place (I am putting it in "static void Main()"? Or
maybe because I didn't use "static" which would leave my method as an
instance method?

Any help is appreciated!
 
I am putting it in "static void Main()"?

Why?


You can do something like this:

class Whatever
{

static void main
{

}

public (or protected or private etc) String YourFunction()
{

...
}

}

You are also not using the return value of the function. If thats the case,
you can declare your function as void. If you are merely getting and setting
a string, you can use Properties(get/set) instead of using a method(that is
the preferred way).

Take a look at a quick C# tour here:
http://www.codeproject.com/csharp/quickcsharp.asp
 
the "x" method needs to be at the same "level" as your Main() and other
methods/properties. There are really only 3 levels to your source file (in
terms of the structure of the class):

namespace mynamespace
{
class myclass
{
public x()
{
}
static Main()
{
}
public string SomePublicProperty="";
}
}

The namespace is the sort of the "top level", then a class is within that,
then EVERYTHING else (which is: methods, properties, enums, structs) are
withing that.

And also yes, static may be giving you a problem too. Normally, a class is
like a blueprint to a house - it's not the house, it's the blueprint to the
house. So, you have to take the blueprints (the class) and build a house
(create an instance/create an object). When you have a static method, that
is part of that class that DOES exist - and exists once for every class. In
other words, imagine this:

namespace mynamespace
{
class myclass
{
public string SomePublicProperty="";
public static string SomeStaticProperty="crap";
}
}

from your code, if you did this:

myclass x = new myclass();
x.SomePublicProperty = "yes";
myclass y = new myclass();
y.SomePublicProperty = "no";
myclass z = new myclass();
z.SomePublicProperty = "maybe";

First of all, A) you have to create an instance of the class in order to
access SomePublicProperty - and B) each "version", or instance of that class
has it's own set of data to if you printed out x.SomePublicProperty,
y.SomePublicProperty and z.SomePublicProperty - you'd see yes, no and
maybe - respectively.

Since a static variable or method exists all the time and the same one
exists for all instances - you don't create an instance of the class, it's
just "there":

myclass.SomeStaticProperty = "stuff";
myclass.SomeStaticProperty = "stuff2";
myclass.SomeStaticProperty = "stuff3";

and if you print out myclass.SomeStaticProperty - it will equal "stuff3",
because there is only one SomeStaticProperty, because it's static.


So to tie this all together, Main() is a static method, and x() is not. So
you need to create an instance of the class you are in, and then use that
instance to call the x() method.

Hope that helps..
 
Your code contains 2 mistakes:
1) The Method does not return a string. It retunrs nothing. So the
return value ist void.
2)Either you create a instance of the class, which contains the method
or you declare your method as static:

static void x()
{
System.Console.WriteLine("hallo welt");
}
 
Back
Top