PDA

Click to See Complete Forum and Search --> : [RESOLVED] Partial Clases and default Constructors in C# 2


Norkis
May 6th, 2006, 08:14 PM
Hello,


I have partial class:


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 :confused:

jmcilhinney
May 6th, 2006, 11:02 PM
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.: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):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):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.

conipto
May 7th, 2006, 04:38 AM
Designer file (.Designer.vb)

Are you sure you didn't make that up ;)

Bill

jmcilhinney
May 7th, 2006, 05:11 AM
My default language is VB. My default state is not thinking. :)

Norkis
May 7th, 2006, 03:40 PM
Thank you. It helped.