|
-
May 6th, 2006, 08:14 PM
#1
Thread Starter
Hyperactive Member
[RESOLVED] Partial Clases and default Constructors in C# 2
Hello,
I have partial class:
Code:
public partial class MyClass
{
private MyClass()
{
//AUTO GENERATED CODE
}
//OTHER AUTO GENERATED CODE
}
public partial class MyClass
{
private MyClass()
{
this.SomeThingChanged += new System.EventHandler(this.ShowChangeMessage);
}
private void ShowChangeMessage()
{}
}
Like you see I have class separated in two files, because one is autogenerated and can be rewriten.
My question is how to add event handler in my additional class if there already is default constructor in autogenerated class?
I got error: Type 'MyClass' already defines a member called 'MyClass' with the same parameter types
-
May 6th, 2006, 11:02 PM
#2
Re: Partial Clases and default Constructors in C# 2
You should not be handling the event internally. You should be overriding the method that raises the event, which in the case of the SomeThingChanged event would be the OnSomeThingChanged method, e.g.:
Code:
protected overrides void OnSomeThingChanged(EventArgs e)
{
// Place your custom code here.
// It will be executed before the event is raised.
// Invoke the base implementation, which will raise the event.
base.OnSomeThingChanged(e);
}
As for auto-generated code and constructors, the C# IDE puts the default constructor in the user file, not the designer file, so I don't know how you ended up with what you posted unless you wrote that allegedly auto-generated file yourself. I created a Windows app and here are the two files created for a form.
User file (.cs):
Code:
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
namespace WindowsApplication1
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
}
}
Designer file (.Designer.vb):
Code:
namespace WindowsApplication1
{
partial class Form1
{
/// <summary>
/// Required designer variable.
/// </summary>
private System.ComponentModel.IContainer components = null;
/// <summary>
/// Clean up any resources being used.
/// </summary>
/// <param name="disposing">true if managed resources should be disposed; otherwise, false.</param>
protected override void Dispose(bool disposing)
{
if (disposing && (components != null))
{
components.Dispose();
}
base.Dispose(disposing);
}
#region Windows Form Designer generated code
/// <summary>
/// Required method for Designer support - do not modify
/// the contents of this method with the code editor.
/// </summary>
private void InitializeComponent()
{
this.components = new System.ComponentModel.Container();
this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
this.Text = "Form1";
}
#endregion
}
}
As you can see, the default constructor is in the user file.
Last edited by jmcilhinney; May 6th, 2006 at 11:33 PM.
-
May 7th, 2006, 04:38 AM
#3
Re: Partial Clases and default Constructors in C# 2
 Originally Posted by jmcilhinney
Designer file (.Designer.vb)
Are you sure you didn't make that up 
Bill
-
May 7th, 2006, 05:11 AM
#4
Re: Partial Clases and default Constructors in C# 2
My default language is VB. My default state is not thinking.
-
May 7th, 2006, 03:40 PM
#5
Thread Starter
Hyperactive Member
Re: Partial Clases and default Constructors in C# 2
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|