Question about methods

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

Keith Smith

Current I have a method I use like this...

public static void WriteToComp(string x)
{
}

It is located in the code for Form1.

Whenever I want to use this code from any other form I have to do this...

Form1.WriteToDatabase("data");

Is there a better way I should be doing this? It seems like there should be
a place I can put a method where I don't have to put "Form1." in front of it
in order to use it.
 
As far as I know, a method must be contained in a class when using .Net
platform.
 
Hi,

You have to use that syntax when you are calling it from outside a method
of Form1 , inside the methods of Form1 you can remove it.

do you understand that the "static" keyword means?

cheers,
 
You have to use that syntax when you are calling it from outside a method
of Form1 , inside the methods of Form1 you can remove it.

do you understand that the "static" keyword means?

As I understand it, "static" means that there can be only one copy of the
method and that there cannot be individual instances created.

So is there not a "general" place that a method can be placed in which it
can be accessed by any form without using the additional syntax?
 
"Static" means two things:
1. The method can be called on the class directly, rather than requiring an
instance of the class. For example:

x = myClass.someStaticMethod(100); // this is fine.
myClass M = new myClass;
x = M.someInstanceMethod(100); // this is fine.
x = myClass.someInstanceMethod(100); // this is not allowed

Although I believe that you can also call static methods via an instance of
the class.

2. All objects of a given class share the same copy of the static method.
Because of this, a static method cannot make use of "this" like an instance
method can. A static method can access any static fields of the class, but
not anything that would have to have been initialized in a constructor.

Generally, static methods are good if you want people to have easy access to
some version of your class that might otherwise take several steps to
create, or that would require knowledge of the inner details of the class.
For example, let's say you had a Matrix class for representing 2-d matrices.
You might want to make it easy for people to get an Identity matrix of a
given size. One way would be to make them say:

Matrix M = new Matrix(5); // I want a 5x5 matrix
for(int i = 0; i < 5; i++)
for(int j=0; j < 5; j++)
if(i==j)
M[i,j] = 1; // we'll assume that Matrix has a 2-d indexer
property.
else
M[i,j] = 0;

Well, that's kind of a pain for them. It would be better, if in your class,
you had a method like this:

public class Matrix {
// code for creating a matrix

public static Matrix Identity(int size) {
Matrix M = new Matrix(size);
// same nested for-loop as above
return M
}
}

That way, someone using your class who wants a 5x5 Identity matrix can
simply say:

Matrix M = Matrix.Identity(5);

Much simpler.
 
MS News (taruntius) said:
"Static" means two things:
1. The method can be called on the class directly, rather than requiring an
instance of the class. For example:

x = myClass.someStaticMethod(100); // this is fine.
myClass M = new myClass;
x = M.someInstanceMethod(100); // this is fine.
x = myClass.someInstanceMethod(100); // this is not allowed

Although I believe that you can also call static methods via an instance of
the class.

No you can't, thankfully. That's one of the improvements C# has over
Java. For instance, the following code is legal in Java:

Thread t = new Thread(myRunnable);
t.start();
t.sleep(5000);

That looks like it starts a new thread and then makes it sleep - when
in fact it starts a new thread and then makes the *current* thread
sleep for 5 seconds.
2. All objects of a given class share the same copy of the static method.

I don't like this way of putting it, because it sort of implies that
there has to be an instance *somewhere*. It's more that the static
method belongs to the type itself rather than to any particular
instance.
Generally, static methods are good if you want people to have easy access to
some version of your class that might otherwise take several steps to
create, or that would require knowledge of the inner details of the class.

My view is that static methods are good if they logically don't act on
any particular instance of a class, or for factory methods (such as
your example).
 
Back
Top