473,704 Members | 2,854 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Static class method return non-static object?

Hi,

I have a class which all its attributes, properties and
methods are static.

However, I want a method to return an object that should
be non-static.

Is that possible?

thx,
Pascal
Nov 16 '05 #1
10 6358
Hi,

That's perfectly possible and is, for example, used in implementation of the
Singleton design pattern when a static property returns an instance of the
same class.

--
Sincerely,
Dmitriy Lapshin [C# / .NET MVP]
Bring the power of unit testing to the VS .NET IDE today!
http://www.x-unity.net/teststudio.aspx

"Paschalis Pagonidis" <an*******@disc ussions.microso ft.com> wrote in message
news:4a******** *************** *****@phx.gbl.. .
Hi,

I have a class which all its attributes, properties and
methods are static.

However, I want a method to return an object that should
be non-static.

Is that possible?

thx,
Pascal


Nov 16 '05 #2
Paschalis Pagonidis <an*******@disc ussions.microso ft.com> wrote:
I have a class which all its attributes, properties and
methods are static.

However, I want a method to return an object that should
be non-static.

Is that possible?


Objects themselves aren't static or non-static - the concept just
doesn't apply. Could you give more details about what you're trying to
do?

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

can you give me an example?

for example:
public class MyClass
{
public static object CreateObject()
{
// I want obj to be non-static
object obj = new object();

return obj;
}

thanks,
Pascal
-----Original Message-----
Hi,

That's perfectly possible and is, for example, used in implementation of theSingleton design pattern when a static property returns an instance of thesame class.

--
Sincerely,
Dmitriy Lapshin [C# / .NET MVP]
Bring the power of unit testing to the VS .NET IDE today!
http://www.x-unity.net/teststudio.aspx

"Paschalis Pagonidis" <an*******@disc ussions.microso ft.com> wrote in messagenews:4a******* *************** ******@phx.gbl. ..
Hi,

I have a class which all its attributes, properties and
methods are static.

However, I want a method to return an object that should
be non-static.

Is that possible?

thx,
Pascal


.

Nov 16 '05 #4
I have a Database class that manages all database related
functionality.

For example instead of initializing SqlDataReader
variables each time in my whole project, I use commands
such as the following:

SqlDataReader data = Database.Execut e
("MyStoredProc" , "MyParams") ;

Database class has a static Connection variable which is
used in all forms. Furthermore all Database
constructors/properties/attributes/methods are static.

My problem is that the Execute command returns a static
SqlDataReader object, so I cannot re-execute a sql connand
inside a data.Read() loop:

while (data.Read())
{
data2 = Database.Execut e(...); // <-- runtime error
occurs because data is already open.
}
data.Close();
// now that I closed data I can execute again...

thanks,
Pascal

-----Original Message-----
Paschalis Pagonidis <an*******@disc ussions.microso ft.com> wrote:
I have a class which all its attributes, properties and
methods are static.

However, I want a method to return an object that should be non-static.

Is that possible?


Objects themselves aren't static or non-static - the

concept justdoesn't apply. Could you give more details about what you're trying todo?

--
Jon Skeet - <sk***@pobox.co m>
http://www.pobox.com/~skeet
If replying to the group, please do not mail me too
.

Nov 16 '05 #5
>
My problem is that the Execute command returns a static
SqlDataReader object, so I cannot re-execute a sql connand
inside a data.Read() loop:


no that is not your problem. like Jon said, there is NO such thing as an
object being static. There is absolutely no difference between a DataReader
returned from a static method vs. an instance method. your problem is
probably that DataReader keeps the underlying connection tied up to fetch
data, you'd have to use a second connection to execute another command.

this is also a quite expensive way of doing things, you should look into
joins to get all the data in one go if possible, rather than keep going back
to the database. or use disconnected dataset rather than having a datareader
tying up the connection for a prolonged period of time.
Nov 16 '05 #6
-----Original Message-----

My problem is that the Execute command returns a static
SqlDataReader object, so I cannot re-execute a sql connand inside a data.Read() loop:

no that is not your problem. like Jon said, there is NO

such thing as anobject being static. There is absolutely no difference between a DataReaderreturned from a static method vs. an instance method. your problem isprobably that DataReader keeps the underlying connection tied up to fetchdata, you'd have to use a second connection to execute another command.
this is also a quite expensive way of doing things, you should look intojoins to get all the data in one go if possible, rather than keep going backto the database. or use disconnected dataset rather than having a datareadertying up the connection for a prolonged period of time.
.


thanks Daniel,
I think the most appropriate way (and less "expensive" in
terms of code changes) is to use disconnected datasets.
Nov 16 '05 #7
Paschalis Pagonidis <an*******@disc ussions.microso ft.com> wrote:
I have a Database class that manages all database related
functionality.

For example instead of initializing SqlDataReader
variables each time in my whole project, I use commands
such as the following:

SqlDataReader data = Database.Execut e
("MyStoredProc" , "MyParams") ;

Database class has a static Connection variable which is
used in all forms.
That sounds like a bad idea. Connection pooling should be used to
handle this - I suspect getting rid of this will help you a great deal.
Furthermore all Database
constructors/properties/attributes/methods are static.

My problem is that the Execute command returns a static
SqlDataReader object


As I said before, an object isn't static or non-static. What do you
mean by the above?

--
Jon Skeet - <sk***@pobox.co m>
http://www.pobox.com/~skeet
If replying to the group, please do not mail me too
Nov 16 '05 #8
Paschalis Pagonidis <an*******@disc ussions.microso ft.com> wrote:
I have a Database class that manages all database related
functionality.

For example instead of initializing SqlDataReader
variables each time in my whole project, I use commands
such as the following:

SqlDataReader data = Database.Execut e
("MyStoredProc" , "MyParams") ;

Database class has a static Connection variable which is
used in all forms.
That sounds like a bad idea. Connection pooling should be used to
handle this - I suspect getting rid of this will help you a great deal.
Furthermore all Database
constructors/properties/attributes/methods are static.

My problem is that the Execute command returns a static
SqlDataReader object


As I said before, an object isn't static or non-static. What do you
mean by the above?

--
Jon Skeet - <sk***@pobox.co m>
http://www.pobox.com/~skeet
If replying to the group, please do not mail me too
Nov 16 '05 #9
Yeah, refer to the code as follows

public class MyClass
{
// Private constructor prevents instantiation
private MyClass()
{
i=12;
}
private int i;
static int x=0;
static MyClass ob;
// Because of the private constructor, use this method
// to obtain a reference to this class.
public static MyClass MyMethod()
{
if (x==0){
ob=new MyClass();
x++;}
return ob;
}
}
with regards,
J.V.Ravichandra n
- http://www.geocities.com/
jvravichandran
- http://www.411asp.net/func/search?
qry=Ravichandra n+J.V.&cob=aspn etpro
- http://www.southasianoutlook.com
- http://www.MSDNAA.Net
- http://www.csharphelp.com
- http://www.poetry.com/Publications/
display.asp?ID= P3966388&BN=999 &PN=2
- Or, just search on "J.V.Ravichandr an"
at http://www.Google.com

*** Sent via Developersdex http://www.developersdex.com ***
Don't just participate in USENET...get rewarded for it!
Nov 16 '05 #10

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

Similar topics

3
4527
by: Phil Powell | last post by:
My first time working with a PHP class, and after 6 hours of working out the kinks I am unable to return a value from the class, so now I appeal to the general audience what on earth did I do wrong this time? This is the code the retrieves the values: if (($hasRegistered || $hasPreRegistered) && !empty($uplinenumber)) { // CHECK TO SEE IF UPLINE NUMBER IS A VALID NUMBER $regNumberGenerator = new RegNumberGenerator($uplinenumber,...
5
1887
by: Peter Meier | last post by:
Hello everybody, Stroustrup says he prefer's to declare operators, which do not do anything on the class itself global. Does anybody know the reason for that? Any advantages/disadvantages? Thank you, Peter
21
4075
by: Jon Slaughter | last post by:
I have a class that is basicaly duplicated throughout several files with only members names changing according to the class name yet with virtually the exact same coding going on. e.g. class A { std::vector<B*> Bs; public:
17
3568
by: baibaichen | last post by:
i have written some code to verify how to disable slicing copy according C++ Gotchas item 30 the follow is my class hierarchy, and note that B is abstract class!! class B { public: explicit B(INT32 i =0):i_(i){} virtual ~B(){}
2
4231
by: vilarneto | last post by:
Hello everyone, I'm facing a particular situation about template class derivation. The subject is old -- deriving a non-template class from a template base class -- but my problem is that the base class has a pure virtual method. For instance: template<class T> class A { public:
0
8766
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
8684
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
9271
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
9134
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
0
8975
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
7887
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
6604
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 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 a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
4699
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
3134
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system

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.