473,382 Members | 1,332 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,382 software developers and data experts.

C# static, is this correct?

Hello guys, in C#, is using "static" would be the most proper way to
get around calling methods located in different classes? for instance,
a method caller in class A wouldn't see a method in class B unless that
method is declared as public static.

This works fine (i guess!) for me, and i have been doing this for a
long time, just came to my mind that there might be a better or more
professional way to call methods in other classes without sharing the
method for the whole namespace scope, and just came to my mind: what if
this practice slows down execution? as i do have intensive calculations
where a method has to return values in few milliseconds and the overall
performance is vital for my application.

Your opinions are greatly appreciated, Thank you!

Maya.

Jan 24 '06 #1
6 4065
Maya <kf****@gmail.com> wrote:
Hello guys, in C#, is using "static" would be the most proper way to
get around calling methods located in different classes? for instance,
a method caller in class A wouldn't see a method in class B unless that
method is declared as public static.
No, that's not true.

A member (whether it's a field, property, method, whatever) being
static means it's associated with the type rather than with a
particular instance.
This works fine (i guess!) for me, and i have been doing this for a
long time, just came to my mind that there might be a better or more
professional way to call methods in other classes without sharing the
method for the whole namespace scope, and just came to my mind: what if
this practice slows down execution? as i do have intensive calculations
where a method has to return values in few milliseconds and the overall
performance is vital for my application.


Performance isn't impacted by this. You should know that there's no way
of restricting a method to only be callable by other classes in the
same namespace though.

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet Blog: http://www.msmvps.com/jon.skeet
If replying to the group, please do not mail me too
Jan 24 '06 #2
Maya wrote:
Hello guys, in C#, is using "static" would be the most proper way to
get around calling methods located in different classes? for instance,
a method caller in class A wouldn't see a method in class B unless that
method is declared as public static.
Or you could instantiate your class:

MyClass mc = new MyClass();

This works fine (i guess!) for me, and i have been doing this for a
long time, just came to my mind that there might be a better or more
professional way to call methods in other classes without sharing the
method for the whole namespace scope, and just came to my mind: what if
this practice slows down execution? as i do have intensive calculations
where a method has to return values in few milliseconds and the overall
performance is vital for my application.

I was under the impression that static, which loads before run, would be
faster because of that.

The problem with static is that it takes up memory and holds it.
Statics can gum things up if you don't use them right.

Statics are something you want to "keep around" for the run of your
application. Non-statics are things that you want to use and throw
away. Especially if you have to reuse things and reinitialize and
don't want to have to worry about left over values, then you don't want
to use statics.
Jan 24 '06 #3

"Maya" <kf****@gmail.com> wrote in message
news:11*********************@g14g2000cwa.googlegro ups.com...
Hello guys, in C#, is using "static" would be the most proper way to
get around calling methods located in different classes? for instance,
a method caller in class A wouldn't see a method in class B unless that
method is declared as public static.

This works fine (i guess!) for me, and i have been doing this for a
long time, just came to my mind that there might be a better or more
professional way to call methods in other classes without sharing the
method for the whole namespace scope, and just came to my mind: what if
this practice slows down execution? as i do have intensive calculations
where a method has to return values in few milliseconds and the overall
performance is vital for my application.

Your opinions are greatly appreciated, Thank you!

Maya.

I'll start this off and I'm sure you'll get many responses...
The external caller won't "see" the method in class B unless the method is
declared public (FWIW, this is an oversimplification, as it does not address
who "sees" what when other access specifiers are used).
Static does not control exposure, the access specifier does (public, in your
example).
The keyword static indicates that the method is not an instance method, and
thus does not require a caller to first instantiate class B, then qualify
the method call with the object name.
I'm not sure why you think static methods might be a factor in degrading
performance. Most, if not all, major class libraries include utility classes
featuring nothing but static methods. I believe, to make the methods readily
available to developers without an instantiation process.
Jan 24 '06 #4
Static methods are an acceptable way for classes to call methods in
other classes. But typically, as Jon mentioned, it associated with the
type rather than the particular instance.

If you have a class with only static methods and static variables, then
it should typically be Utility class or you might want to rethink your
design a bit.

Two other acceptable ways to solve your initial problem are passing the
instances of the classes to the objects requiring them. For instance,
if you want to call class A in class B, pass class A as a parameter to
class B's constructor.

The other method, is the Singleton pattern. This has a slightly
different intention: Ensure a class has only one instance and provide a
global point of access to it.

You can read more about it here:
http://www.yoda.arachsys.com/csharp/singleton.html

Cheers,
Adam
-------------------------------------------------------------------------------------------
J-Integra Interoperability Solutions
http://j-integra.intrinsyc.com/
high performance interop middleware for java, corba, com & .net

Jan 24 '06 #5
One alternative from static is to past the object reference as you
instantiate new object from several classes. I trhink this is part of
design pattern approach rather than OOP, but I still reading this book.
Here the example which work well on application I'm developing..

namespace Scope
{
ScopeMe Me = new ScopeMe();
ScopeSnooze Object2 = new ScopeSnooze(Me);
ScopeWake Object3 = new ScopeWake(Me);
ScopeEat Object4 = new ScopeEat(Me); ........and so on.
....
}
....
namespace Scope
{
class ScopeEat()
{
ScopeMe Me
public ScopeEat(_Me)
{
Me=_Me;
}
}
}
namespace Scope
{
class ScopeWake()
{
ScopeMe Me
public ScopeWake(_Me)
{
Me=_Me;
}
}
}

Adam Cooper wrote:
Static methods are an acceptable way for classes to call methods in
other classes. But typically, as Jon mentioned, it associated with the
type rather than the particular instance.

If you have a class with only static methods and static variables, then
it should typically be Utility class or you might want to rethink your
design a bit.

Two other acceptable ways to solve your initial problem are passing the
instances of the classes to the objects requiring them. For instance,
if you want to call class A in class B, pass class A as a parameter to
class B's constructor.

The other method, is the Singleton pattern. This has a slightly
different intention: Ensure a class has only one instance and provide a
global point of access to it.

You can read more about it here:
http://www.yoda.arachsys.com/csharp/singleton.html

Cheers,
Adam
-------------------------------------------------------------------------------------------
J-Integra Interoperability Solutions
http://j-integra.intrinsyc.com/
high performance interop middleware for java, corba, com & .net


Jan 25 '06 #6
Maya,

Your question is about C#.

However, doing this as you wrote in the VBNet world would be bad
programming.

It is in fact not OOP it is using the VB module in another way.

I hope this gives an idea, and spare me a discussion writting it in this
way.

Cor
Jan 25 '06 #7

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

27
by: Yang Lee | last post by:
Hi, If I write char *p="hello world"; is this correct in C or do i have to assign memory block and then strcpy the string to pointer. If not correct in C , is it allowable in C++ , i...
0
by: JRozak | last post by:
I want to dynamically allocate an array to store binary data. Is this correct? unsigned char * BinData; unsigned int npts; ..... //Read size of binary data file.read((char *)&npts,4) BinData...
6
by: Maya | last post by:
Hello guys, in C#, is using "static" would be the most proper way to get around calling methods located in different classes? for instance, a method caller in class A wouldn't see a method in class...
2
by: Ravi Uday | last post by:
Hi, Can some-one comment on below snippet ? static int xxx_list_compare (void *ptr1, void *ptr2) { if (ptr1 && ptr2 && (ptr1 == ptr2)) { return (0); } return (1); }
14
by: mathieu | last post by:
I would like to know if the following operation on s2 is legal or not. And if not what should I do instead (my data member are declared at the begining of the struct). I am pretty sure this is...
2
by: pkm | last post by:
In test.hpp ... .. typedef struct _test_ { ... ... } test class test {
10
code green
by: code green | last post by:
I wasn't sure what title to give this, or whether I can explain my dilemma, but here goes. My company sells products that are inter-compatible with each other, mainly four ranges I will call...
0
by: Fredrik Lundh | last post by:
Bruce Pearson wrote: yes: http://docs.python.org/ref/function.html "Default parameter values are evaluated when the function definition is executed." (in bold, even)
1
by: NAVEEN1996 | last post by:
I cant get this java program to work.Can anyone please tell me what the problem about this is and what should I do to correct it. import java.io.*; public class Wishes { public static void...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.