Reputation: 4182
This question is kind of anecdotical but still interesting to me; I was wondering why Visual Studio 2008 is not loving the following use of constants:
public class Service101 : ServiceBase
{
/// <remarks>
/// Shown at Start -> Settings -> Control Panel -> Administrative Tools -> Services
/// </remarks>
internal const string SERVICE_NAME = "WinSvc101";
/// <remarks>
/// Shown at Start -> Settings -> Control Panel -> Administrative Tools -> Services
/// </remarks>
internal const string DISPLAY_NAME = "Windows Service 101";
/// <summary>
/// Public constructor for Service101.
/// </summary>
public Service101()
{
InitializeComponent();
}
private void InitializeComponent()
{
this.ServiceName = Service101.SERVICE_NAME;
this.EventLog.Source = Service101.DISPLAY_NAME;
this.EventLog.Log = "Application";
if (!EventLog.SourceExists(Service101.DISPLAY_NAME))
{
EventLog.CreateEventSource(Service101.DISPLAY_NAME, "Application");
}
}
#region Events
/// <summary>
/// Dispose of objects that need it here.
/// </summary>
/// <param name="disposing">Whether or not disposing is going on.</param>
protected override void Dispose(bool disposing)
{
// TODO: Add cleanup code here (if required)
base.Dispose(disposing);
}
As it's showing the following Warning at design time:
Warning 1 The designer cannot process the code at line 68:
if (!EventLog.SourceExists(DISPLAY_NAME))
{
EventLog.CreateEventSource(DISPLAY_NAME, "Application");
}
The code within the method 'InitializeComponent' is generated by the designer and should not be manually modified. Please remove any changes and try opening the designer again. E:\Proyectos\beanstalk\dotnetfx\trunk\WinSvc101\WinSvc101\Service101.cs 69 0
Any comment would be quite appreciated. Thanks much in advance.
Upvotes: 3
Views: 4152
Reputation: 25369
Slightly off the point, but I don't really see why you are using constants (and public ones at that), anyway. Couldn't you just do this?
private void InitializeComponent()
{
this.ServiceName = "WinSvc101";
this.EventLog.Source = "Windows Service 101";
// ....
}
Upvotes: 1
Reputation: 161831
It actually told you. That code is generated by the designer. The designer needs it to be the way it left it. Do not change that code, unless you want the designer to do unpleasant things with it.
There's a sort of equilibrium between what you see in a visual designer and the code that it has generated.
Now, let's say you make some modification to the generated code. Unless you make that change in exactly the same way the designer would have done, it will not recognize the change. Your change will not show on the design surface. Next time there's a change or save, the designer will regenerate the code without your changes.
So, if you don't want to lose your changes to generated code, then don't make any changes to generated code.
Upvotes: 5
Reputation: 2564
Seems quite clear to me. It's telling you that you have modified automatically generated code and you shouldn't do that.
In the worst case, you probably could lose your changes. In the best, it finds some unexpected code and it doesn't change anything so you don't lose your changes. But it also can't understand your code. You should place your constants in any other location.
Upvotes: 3
Reputation: 351758
The designer is not happy when you add code to InitializeComponent()
. Try something like this instead:
public Service101()
{
InitializeComponent();
this.createEventSource();
}
private void InitializeComponent()
{
this.ServiceName = SERVICE_NAME;
this.EventLog.Source = DISPLAY_NAME;
this.EventLog.Log = "Application";
}
void createEventSource()
{
if (!EventLog.SourceExists(DISPLAY_NAME))
{
EventLog.CreateEventSource(DISPLAY_NAME, "Application");
}
}
Upvotes: 3