By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
455,458 Members | 1,313 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 455,458 IT Pros & Developers. It's quick & easy.

Question about methods

P: n/a
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.

Nov 16 '05 #1
Share this Question
Share on Google+
5 Replies


P: n/a
As far as I know, a method must be contained in a class when using .Net
platform.
--
Ravi Ambros Wallau
r w a l l a u @ s p r i n g w i r e l e s s . n e t

"Keith Smith" <ke*********@verizon.net> wrote in message
news:_qZVd.21693$QQ3.16792@trnddc02...
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.

Nov 16 '05 #2

P: n/a
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,

--
Ignacio Machin,
ignacio.machin AT dot.state.fl.us
Florida Department Of Transportation

"Keith Smith" <ke*********@verizon.net> wrote in message
news:_qZVd.21693$QQ3.16792@trnddc02...
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.

Nov 16 '05 #3

P: n/a
> 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?

Nov 16 '05 #4

P: n/a
"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.

"Keith Smith" <ke*********@verizon.net> wrote in message
news:sfjWd.55775$EL5.2836@trnddc05...
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?

Nov 16 '05 #5

P: n/a
MS News (taruntius) <ta*******@hotmail.com> wrote:
"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).

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet
If replying to the group, please do not mail me too
Nov 16 '05 #6

This discussion thread is closed

Replies have been disabled for this discussion.