Connecting Tech Pros Worldwide Forums | Help | Site Map

Static class method return non-static object?

Paschalis Pagonidis
Guest
 
Posts: n/a
#1: Nov 16 '05
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

Dmitriy Lapshin [C# / .NET MVP]
Guest
 
Posts: n/a
#2: Nov 16 '05

re: Static class method return non-static object?


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" <anonymous@discussions.microsoft.com> wrote in message
news:4a0901c4c65a$5b8055b0$a601280a@phx.gbl...[color=blue]
> 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[/color]

Jon Skeet [C# MVP]
Guest
 
Posts: n/a
#3: Nov 16 '05

re: Static class method return non-static object?


Paschalis Pagonidis <anonymous@discussions.microsoft.com> wrote:[color=blue]
> 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?[/color]

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 - <skeet@pobox.com>
http://www.pobox.com/~skeet
If replying to the group, please do not mail me too
Paschalis Pagonidis
Guest
 
Posts: n/a
#4: Nov 16 '05

re: Static class method return non-static object?


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
[color=blue]
>-----Original Message-----
>Hi,
>
>That's perfectly possible and is, for example, used in[/color]
implementation of the[color=blue]
>Singleton design pattern when a static property returns[/color]
an instance of the[color=blue]
>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"[/color]
<anonymous@discussions.microsoft.com> wrote in message[color=blue]
>news:4a0901c4c65a$5b8055b0$a601280a@phx.gbl...[color=green]
>> 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[/color]
>
>.
>[/color]
Paschalis Pagonidis
Guest
 
Posts: n/a
#5: Nov 16 '05

re: Static class method return non-static object?


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.Execute
("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.Execute(...); // <-- runtime error
occurs because data is already open.
}
data.Close();
// now that I closed data I can execute again...

thanks,
Pascal

[color=blue]
>-----Original Message-----
>Paschalis Pagonidis <anonymous@discussions.microsoft.com>[/color]
wrote:[color=blue][color=green]
>> I have a class which all its attributes, properties and
>> methods are static.
>>
>> However, I want a method to return an object that[/color][/color]
should[color=blue][color=green]
>> be non-static.
>>
>> Is that possible?[/color]
>
>Objects themselves aren't static or non-static - the[/color]
concept just[color=blue]
>doesn't apply. Could you give more details about what[/color]
you're trying to[color=blue]
>do?
>
>--
>Jon Skeet - <skeet@pobox.com>
>http://www.pobox.com/~skeet
>If replying to the group, please do not mail me too
>.
>[/color]
Daniel Jin
Guest
 
Posts: n/a
#6: Nov 16 '05

re: Static class method return non-static object?


>[color=blue]
> 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:
>[/color]

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.
Paschalis Pagonidis
Guest
 
Posts: n/a
#7: Nov 16 '05

re: Static class method return non-static object?


[color=blue]
>-----Original Message-----[color=green]
>>
>> My problem is that the Execute command returns a static
>> SqlDataReader object, so I cannot re-execute a sql[/color][/color]
connand[color=blue][color=green]
>> inside a data.Read() loop:
>>[/color]
>
>no that is not your problem. like Jon said, there is NO[/color]
such thing as an[color=blue]
>object being static. There is absolutely no difference[/color]
between a DataReader[color=blue]
>returned from a static method vs. an instance method.[/color]
your problem is[color=blue]
>probably that DataReader keeps the underlying connection[/color]
tied up to fetch[color=blue]
>data, you'd have to use a second connection to execute[/color]
another command.[color=blue]
>
>this is also a quite expensive way of doing things, you[/color]
should look into[color=blue]
>joins to get all the data in one go if possible, rather[/color]
than keep going back[color=blue]
>to the database. or use disconnected dataset rather than[/color]
having a datareader[color=blue]
>tying up the connection for a prolonged period of time.
>.
>[/color]

thanks Daniel,
I think the most appropriate way (and less "expensive" in
terms of code changes) is to use disconnected datasets.
Jon Skeet [C# MVP]
Guest
 
Posts: n/a
#8: Nov 16 '05

re: Static class method return non-static object?


Paschalis Pagonidis <anonymous@discussions.microsoft.com> wrote:[color=blue]
> 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.Execute
> ("MyStoredProc", "MyParams");
>
> Database class has a static Connection variable which is
> used in all forms.[/color]

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.
[color=blue]
> Furthermore all Database
> constructors/properties/attributes/methods are static.
>
> My problem is that the Execute command returns a static
> SqlDataReader object[/color]

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

--
Jon Skeet - <skeet@pobox.com>
http://www.pobox.com/~skeet
If replying to the group, please do not mail me too
Jon Skeet [C# MVP]
Guest
 
Posts: n/a
#9: Nov 16 '05

re: Static class method return non-static object?


Paschalis Pagonidis <anonymous@discussions.microsoft.com> wrote:[color=blue]
> 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.Execute
> ("MyStoredProc", "MyParams");
>
> Database class has a static Connection variable which is
> used in all forms.[/color]

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.
[color=blue]
> Furthermore all Database
> constructors/properties/attributes/methods are static.
>
> My problem is that the Execute command returns a static
> SqlDataReader object[/color]

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

--
Jon Skeet - <skeet@pobox.com>
http://www.pobox.com/~skeet
If replying to the group, please do not mail me too
Ravichandran J.V.
Guest
 
Posts: n/a
#10: Nov 16 '05

re: Static class method return non-static object?


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.Ravichandran
- http://www.geocities.com/
jvravichandran
- http://www.411asp.net/func/search?
qry=Ravichandran+J.V.&cob=aspnetpro
- 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.Ravichandran"
at http://www.Google.com

*** Sent via Developersdex http://www.developersdex.com ***
Don't just participate in USENET...get rewarded for it!
Ravichandran J.V.
Guest
 
Posts: n/a
#11: Nov 16 '05

re: Static class method return non-static object?


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.Ravichandran
- http://www.geocities.com/
jvravichandran
- http://www.411asp.net/func/search?
qry=Ravichandran+J.V.&cob=aspnetpro
- 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.Ravichandran"
at http://www.Google.com

*** Sent via Developersdex http://www.developersdex.com ***
Don't just participate in USENET...get rewarded for it!
Closed Thread