473,468 Members | 1,713 Online
Bytes | Software Development & Data Engineering Community
Create Post

Home Posts Topics Members FAQ

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 4309
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

8
by: Scott J. McCaughrin | last post by:
The following program compiles fine but elicits this message from the linker: "undefined reference to VarArray::funct" and thus fails. It seems to behave as if the static data-member:...
15
by: Samee Zahur | last post by:
Question: How do friend functions and static member functions differ in terms of functionality? I mean, neither necessarily needs an object of the class to be created before they are called and...
5
by: Mountain Bikn' Guy | last post by:
How would I do this? public sealed class UtilityClass { public static MyObject Object1;//see note below about importance of static object names in this class public static MyObject Object2;...
3
by: Jay | last post by:
Why are there static methods in C#. In C++ static was applied to data only (I believe) and it meant that the static piece of data was not a part of the object but only a part of the class (one...
9
by: Laban | last post by:
Hi, I find myself using static methods more than I probably should, so I am looking for some advice on a better approach. For example, I am writing an app that involves quite a bit of database...
12
by: Joe Narissi | last post by:
I know how to create and use static constructors, but is there a such thing as a static destructor? If not, then how do you deallocate memory intialized in the static constructor? Thanks in...
55
by: Zytan | last post by:
I see that static is more restricted in C# than in C++. It appears usable only on classes and methods, and data members, but cannot be created within a method itself. Surely this is possible in...
1
by: Sandro Bosio | last post by:
Hello everybody, my first message on this forum. I tried to solve my issue by reading other similar posts, but I didn't succeed. And forgive me if this mail is so long. I'm trying to achieve the...
9
by: Steve Richter | last post by:
in a generic class, can I code the class so that I can call a static method of the generic class T? In the ConvertFrom method of the generic TypeConvert class I want to write, I have a call to...
14
by: Jess | last post by:
Hello, I learned that there are five kinds of static objects, namely 1. global objects 2. object defined in namespace scope 3. object declared static instead classes 4. objects declared...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
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...
0
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,...
0
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...
1
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
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...
0
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,...
0
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...
0
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?

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.