Warnings when using EventLog

o-mheien@hdsoftware.no

Active member
Joined
Feb 6, 2007
Messages
27
Programming Experience
10+
Hi folks
I'm trying to utilize the Eventlog class but get warnings saying "Access of Shared member, constant member, enum member or nested type through an instance; qualifying expression will not be evaluated."

This is what I do, as explained in the help files
VB.NET:
Dim Eventlogger as new System.Diagnistics.EventLog
if Not Eventlogger.SourceExists("Some String") then
 
End if
the warning is outlineing the "Eventlogger.SourceExists"

Hellp please. Have no clue what I'm doing wrong here.

Regards
Ole
 
EventLog.SourceExists is a shared/static method. Static methods do not require an instance in order to be run and to enforce the fact that you understand this, you are required to use the classname not the instance variable name.

If youre new to OO and not sure of the difference between a static entity (usually a class or method) and an instantiated one, have a read of a tutorial on the topic

examples:

VB.NET:
Dim s as New String = "hello world"
Dim t as String = s.Replace("e", "a") 'replace is not static, requires instance
Dim u as String = String.Format("message is {0}", s) 'Format is static, you must not use an instance name s, t or u.
 
But isnt that the same as changing my code to just say:
DIM Eventlogger as System.Diagnistics.Eventlog

Because that gives the same result. And allso, that is NOT what the documentation say. It says I should use NEW

btw - I'm not new to OOP and I know what an Instance is. But when the error says "Qualifying expression will not be evaluated" I'm lost
 
But you where perfectly right. I changed my code to this and it just works:
VB.NET:
if not System.Diagnostics.Eventlog.SourceExists(.......

Thanx anyway :)
 
Its just my eyes that are messing with me :-(

Just saw in the helper a second time and the example code shows that your
are right on the spot. But it is a bad example and its easy to
missunderstand if not paying attantion. The example goes like this:

VB.NET:
If Not EventLog.SourceExists("MyApp1") Then   
 EventLog.CreateEventSource("MyApp1", "Application")
End If
EventLog1.Source = "MyApp1"EventLog1.WriteEntry("This is a simple event log entry")
Its easy to miss the "1" in the instance. The example should have named
the instance as EventloggerInstance to make sure to point out the
difference.
 
The instance (here) is the qualifying expression for the method call, but for a shared method you don't access it by instance, shared methods require by definition instance-less (type) calls.

The message is a warning, not an error, this mean the expression will be carried out and the complier will consider it a shared type call and ignore your programmatic error, but no reason not to correct yourself and remove the warning.

(I know you have "fixed" it, but the explanation wouldn't hurt)
 
But isnt that the same as changing my code to just say:
DIM Eventlogger as System.Diagnistics.Eventlog

No, this is a declaration of a typed variable. I usually advocate people use the longer format:

Dim x as System.Diagnostics.EventLog = New System.Diagnostics.EventLog()


This means "declare a variable of name x capable of holding and object of type System.Diagnostics.EventLog, and initialise its value to be a brand new EventLog object. You could also write:

Dim x as System.Diagnostics.EventLog = myOldEventLogObjectAlreadyCreated

Basically, you have to tell VB what type of object you will put in a variable, thats what Dim NAME as TYPE is for. The Dim x as New EventLog is a shorthand for the above Dim NAME as TYPE = New TYPE

Because that gives the same result.
As?

Declaring a variable but not filling it with an instance will cause errors as soon as you try and use it.. A bit like buying a toilet flush handle from the hardware store; this handle is capable of flushing a toilet, but until you attach it to a toilet, it will never sucessfully flush a toilet.

And allso, that is NOT what the documentation say. It says I should use NEW
Youre now talking about something completely different - declaring a variable and giving it a value is nothing to do with running a static method.

btw - I'm not new to OOP and I know what an Instance is.

Then you must know that it is possible to make something static, and only one copy of that entity will ever exist when the program is running. In old VB6 we called these MODULES and everything in them was static

But when the error says "Qualifying expression will not be evaluated" I'm lost
It means there is no point running a static method on an instance, and to signify that you understand the method to be static and the implications therein, you must run it from the type directly
 
Back
Top