469,358 Members | 1,659 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 469,358 developers. It's quick & easy.

To use static or to not use static?

.....that is the question.

Any who, I have an application that only a handful of people will being
using. One of the features allows them to retrieve of a listing of
people into a datagrid. I'd like to make the method that does this
static. Once the data is retreived it is munipulated locally in a
dataview. They won't be updating it. That occurs in another place.

Would making these retreival methods static cause any problems? I
don't have a need for instances. However, will the method being static
cause the 2nd person to see results retreived by the 1st person? I
don't want any crossing over of data. I know instances will build a
wall between each user. I'm just not sure if users will end up sharing
data via the static method.

Thanks,
Brett

Dec 6 '05 #1
17 4224
Should not be a problem.

"Brett Romero" <ac*****@cygen.com> wrote in message
news:11*********************@f14g2000cwb.googlegro ups.com...
....that is the question.

Any who, I have an application that only a handful of people will being
using. One of the features allows them to retrieve of a listing of
people into a datagrid. I'd like to make the method that does this
static. Once the data is retreived it is munipulated locally in a
dataview. They won't be updating it. That occurs in another place.

Would making these retreival methods static cause any problems? I
don't have a need for instances. However, will the method being static
cause the 2nd person to see results retreived by the 1st person? I
don't want any crossing over of data. I know instances will build a
wall between each user. I'm just not sure if users will end up sharing
data via the static method.

Thanks,
Brett

Dec 6 '05 #2
Ok. How is it than that each user still more or less has their own
instance of data from their specific retreival? I always thought of
the static method as sending back the same results to every one. I
guess if the DB is changing and two people call the static method at
different times, they will get different results because the results
are dynamic. Is that valid logic?

For example, U1 retreives data. U2 adds a person to the DB. U3
retreives data and has one more person in the result set than U1. Can
that happen?

Than how is the above any different from using instances?

Thanks,
Brett

Dec 6 '05 #3
There is little you can do to prevent concurrency issues such as if data is
added after someone has already retrieved it.

I think you are confusing two issues. The data that a static method returns
is its own copy within the process or app domain that it is running in.
Presumably each user will have a different instance of your application.

Your scenario with Ux definitely can happen. That is simply the nature of
databases. Now, what is your goal? Do you want all people to see that data
updated when changed in the database? If you load data locally, it is no
longer connected to the database so changes will not be affected until your
app refreshes the data.
"Brett Romero" <ac*****@cygen.com> wrote in message
news:11**********************@g44g2000cwa.googlegr oups.com...
Ok. How is it than that each user still more or less has their own
instance of data from their specific retreival? I always thought of
the static method as sending back the same results to every one. I
guess if the DB is changing and two people call the static method at
different times, they will get different results because the results
are dynamic. Is that valid logic?

For example, U1 retreives data. U2 adds a person to the DB. U3
retreives data and has one more person in the result set than U1. Can
that happen?

Than how is the above any different from using instances?

Thanks,
Brett

Dec 6 '05 #4
Ok, I'm not worried about concurrency and the static methods will do
fine. Now, in the limited case I've described, is there any reason to
use instances?

I have an access question related to the static methods. In projectA,
I have a reference to event.dll. event.dll has
Event.class1.staticmethod. staticmethod is public. I see this in
object browser from ProjectA. Looks fine. However, in project A, I
type:
Event
and the class or methods do not appear in intellisense. The class and
method are public. I've recompiled both the DLL project and projectA
(both are in a solution). Still, I don't see the class or method in
Event namespace. Any ideas?

Thanks,
Brett

Dec 6 '05 #5
Instances are only required if you maintain state so if you have nothing
that persists between calls to the method, then there is no problem making
them static.

Two things to make sure about for:
1) Make sure you are actually referencing the correct assembly. If they
are two projects in the same solutions, make sure you reference the project,
not the DLL.
2) Make sure the spelling is correct. Just the other day I was working
with someone (through IM so I could not interact with their computer) and
they had the same problem. I listed these common issues and was assured
things were as they should. Then later in the day, the person told me it
was a spelling issue, the first letter of the namespace was different [case]
from the first letter of the assembly.

Do you see any other classes in the assembly? If you use the Event
namespace without actually having it popup, does the compiler complain?

"Brett Romero" <ac*****@cygen.com> wrote in message
news:11**********************@g43g2000cwa.googlegr oups.com...
Ok, I'm not worried about concurrency and the static methods will do
fine. Now, in the limited case I've described, is there any reason to
use instances?

I have an access question related to the static methods. In projectA,
I have a reference to event.dll. event.dll has
Event.class1.staticmethod. staticmethod is public. I see this in
object browser from ProjectA. Looks fine. However, in project A, I
type:
Event
and the class or methods do not appear in intellisense. The class and
method are public. I've recompiled both the DLL project and projectA
(both are in a solution). Still, I don't see the class or method in
Event namespace. Any ideas?

Thanks,
Brett

Dec 6 '05 #6
I only have one class in Event with static and non static methods. If
I instantiate the class, I see the non static method. Otherwise (using
static), I don't see any methods. I do have them each in different
solutions. I'm referencing the Event.dll and not the project.

Thanks,
Brett

Dec 7 '05 #7
Here's another scenario related to static methods.
EventLog.WriteEntry() requires an instance. However, if I want to
create a DLL specifically for error catching and have all its methods
static, how do I get around the WriteEntry() issue? I can't put it
into a static method.

I want applications to use the error catching DLL statically. No need
for instances.

Thanks,
Brett

Dec 8 '05 #8
Brett Romero <ac*****@cygen.com> wrote:
Here's another scenario related to static methods.
EventLog.WriteEntry() requires an instance. However, if I want to
create a DLL specifically for error catching and have all its methods
static, how do I get around the WriteEntry() issue? I can't put it
into a static method.

I want applications to use the error catching DLL statically. No need
for instances.


EventLog.WriteEntry needs an instance of *EventLog* - it doesn't need
an instance of your class. You can call it from a static method without
any problem, so long as you've got an instance of EventLog.

--
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
Dec 8 '05 #9
Will it work to have declare a class (myClass) level instance of
EventLog and use the instance in the static method of myClass?

In the app that will reference the error DLL, it should then only need
a static reference to the DLL namespace.class.staticmethod()?

Thanks,
Brett

Dec 8 '05 #10
Brett Romero wrote:
Will it work to have declare a class (myClass) level instance of
EventLog and use the instance in the static method of myClass?
Yes.
In the app that will reference the error DLL, it should then only need
a static reference to the DLL namespace.class.staticmethod()?


Exactly.

Jon

Dec 8 '05 #11
Well, here's what I keep running into:

private EventLog _eventLog = new EventLog();

public static void LogError(Exception e, string customMessage)
{
string logContent = e.ToString() + "\r\n" + "\r\n" + "Inner
Exception: " + e.InnerException + "\r\n" + "\r\n" + "Message: " +
e.Message + "\r\n" + "\r\n" + "Custom Message: " + customMessage;
_eventLog.WriteEntry (logContent);
}

which gives:
C:\Projects\ErrorHandling\ErrorHandler.cs(48):
'ErrorHandling.LogErrors._eventLog' denotes a 'field' where a 'class'
was expected

I don't understand that. EventLog is a class.
Now, if I try this:
public static void LogError(Exception e, string customMessage)
{
string logContent = e.ToString() + "\r\n" + "\r\n" + "Inner
Exception: " + e.InnerException + "\r\n" + "\r\n" + "Message: " +
e.Message + "\r\n" + "\r\n" + "Custom Message: " + customMessage;
EventLog.WriteEntry (logContent);
}

I get this:
C:\Projects\ErrorHandling\ErrorHandler.cs(48): An object reference is
required for the nonstatic field, method, or property
'System.Diagnostics.EventLog.WriteEntry(string)'

Any ideas?

Thanks,
Brett

Dec 8 '05 #12
Brett Romero <ac*****@cygen.com> wrote:
Well, here's what I keep running into:

private EventLog _eventLog = new EventLog();

public static void LogError(Exception e, string customMessage)
{
string logContent = e.ToString() + "\r\n" + "\r\n" + "Inner
Exception: " + e.InnerException + "\r\n" + "\r\n" + "Message: " +
e.Message + "\r\n" + "\r\n" + "Custom Message: " + customMessage;
_eventLog.WriteEntry (logContent);
}

which gives:
C:\Projects\ErrorHandling\ErrorHandler.cs(48):
'ErrorHandling.LogErrors._eventLog' denotes a 'field' where a 'class'
was expected

I don't understand that. EventLog is a class.


Well, I'm slightly surprised you got that message, but the above
shouldn't compile. You've got _eventLog as an *instance* variable, but
you're trying to use it from a static method. You should make it a
static variable.

--
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
Dec 8 '05 #13
Even if I make _eventLog static, I get the field error.

Here's what does work, I use the static overload for WriteEntry():
EventLog.WriteEntry("mysource", logContent);

In the app that will use this class, it will need to pass the source in
each time. I'm going to set up source enumerations in a non form class
startup object. That will give static site wide access to the source
name. Does that seem sound?

Thanks for all the help Jon (as always :)

Brett

Dec 8 '05 #14
Brett Romero <ac*****@cygen.com> wrote:
Even if I make _eventLog static, I get the field error.


Could you post a short but complete program which demonstrates the
problem?

See http://www.pobox.com/~skeet/csharp/complete.html for details of
what I mean by that.

Looking at the error message you've got, it suggests something quite
different is wrong.

--
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
Dec 8 '05 #15
Also, do I need to do this in every one of the static error methods
since they don't maintain state:
if(!EventLog.SourceExists(eventLogSource))
{
EventLog.CreateEventSource(eventLogSource, eventLogName);
EventLog.WriteEntry(eventLogSource, "Event log processing start for
" + eventLogSource);
}

By the way, do you know of a good 3rd part prog that will post from
VS.NET into a textbox with good formatting? I left the above exactly
as it was posted from VS.NET. I always have to reformat.

Thanks,
Brett

Dec 8 '05 #16
Brett Romero <ac*****@cygen.com> wrote:
Also, do I need to do this in every one of the static error methods
since they don't maintain state:
if(!EventLog.SourceExists(eventLogSource))
{
EventLog.CreateEventSource(eventLogSource, eventLogName);
EventLog.WriteEntry(eventLogSource, "Event log processing start for
" + eventLogSource);
}
Well, I'd certainly wrap that in a method you can call simply, but yes,
you potentially need to do that. It's not clear where eventLogSource
has come from in this case though.
By the way, do you know of a good 3rd part prog that will post from
VS.NET into a textbox with good formatting? I left the above exactly
as it was posted from VS.NET. I always have to reformat.


Well, I've got code which reformats in terms of replacing tabs with
spaces and left-aligning everything - as it happens, that's part of
reformatting it for HTML, but it would probably help anyway. It doesn't
wrap things to 80 columns, however...

See http://aspspider.net/jskeet/CodeFormatter.aspx

(You really need to add three tabs to the left of the "if" to make it
as nice as possible 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
Dec 8 '05 #17
Actually, I put the tab size as one and got this:
private static void checkEventLogSource()
{
if (_eventSource.Length == 0)
throw new ArgumentNullException("Event source not created. Please
call ErrorLog.CreateLogSourceEntry first.");

}

Very nice. Cool tool. Thanks,
Brett

Dec 8 '05 #18

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

8 posts views Thread by Scott J. McCaughrin | last post: by
15 posts views Thread by Samee Zahur | last post: by
3 posts views Thread by Jay | last post: by
12 posts views Thread by Joe Narissi | last post: by
14 posts views Thread by Jess | last post: by
reply views Thread by zhoujie | last post: by
1 post views Thread by Marylou17 | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.