Results 1 to 5 of 5

Thread: [RESOLVED] Partial Clases and default Constructors in C# 2

  1. #1

    Thread Starter
    Hyperactive Member
    Join Date
    Jul 2000
    Location
    Europe, Lithuania
    Posts
    309

    Resolved [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

  2. #2
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,221

    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.
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

  3. #3
    Frenzied Member conipto's Avatar
    Join Date
    Jun 2005
    Location
    Chicago
    Posts
    1,175

    Re: Partial Clases and default Constructors in C# 2

    Quote Originally Posted by jmcilhinney
    Designer file (.Designer.vb)
    Are you sure you didn't make that up

    Bill
    Hate Adobe Acrobat? My Codebank Sumbissions - Easy CodeDom Expression evaluator: (VB / C# ) -- C# Scrolling Text Display

    I Like to code when drunk. Don't say you weren't warned.

  4. #4
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,221

    Re: Partial Clases and default Constructors in C# 2

    My default language is VB. My default state is not thinking.
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

  5. #5

    Thread Starter
    Hyperactive Member
    Join Date
    Jul 2000
    Location
    Europe, Lithuania
    Posts
    309

    Resolved Re: Partial Clases and default Constructors in C# 2

    Thank you. It helped.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width