Results 1 to 5 of 5

Thread: Where should code be placed?

  1. #1

    Thread Starter
    Member
    Join Date
    Mar 2006
    Posts
    41

    Smile Where should code be placed?

    Hi folks, I'm wondering if it's bad practice in Visual C# to have code in the constructor like so

    Code:
    public Form1()
    {
    
    InitializeComponent();
    
    //Some user generated code here like the examples below
    
    method1();
    
    method2();
    
    etc, etc.....
    
    }
    or is it better to have methods called, variables declared, etc, in the app's
    Form1_Load(...){.....} portion of the code as any code here will run when the form loads anyway.

    Thanks for your help.
    Using: VB.net + C#.net 2005 Express Editions + .Net Framework 2.0

    ~ Man Is The Warmest Place To Hide ~

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

    Re: Where should code be placed?

    Microsoft recommend placing as little code as possible in a constructor. I would suggest any simple initialisation be done in the constructor but anything remotely complex, like getting data from any external source, be done in the Load event handler.
    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
    Ex-Super Mod RobDog888's Avatar
    Join Date
    Apr 2001
    Location
    LA, Calif. Raiders #1 AKA:Gangsta Yoda™
    Posts
    60,709

    Re: Where should code be placed?

    Also, anything you place in there should be more related to the presentation or creation of the form vs. obtaining external data or such.
    VB/Office Guru™ (AKA: Gangsta Yoda®)
    I dont answer coding questions via PM. Please post a thread in the appropriate forum.

    Microsoft MVP 2006-2011
    Office Development FAQ (C#, VB.NET, VB 6, VBA)
    Senior Jedi Software Engineer MCP (VB 6 & .NET), BSEE, CET
    If a post has helped you then Please Rate it!
    Reps & Rating PostsVS.NET on Vista Multiple .NET Framework Versions Office Primary Interop AssembliesVB/Office Guru™ Word SpellChecker™.NETVB/Office Guru™ Word SpellChecker™ VB6VB.NET Attributes Ex.Outlook Global Address ListAPI Viewer utility.NET API Viewer Utility
    System: Intel i7 6850K, Geforce GTX1060, Samsung M.2 1 TB & SATA 500 GB, 32 GBs DDR4 3300 Quad Channel RAM, 2 Viewsonic 24" LCDs, Windows 10, Office 2016, VS 2019, VB6 SP6

  4. #4

    Thread Starter
    Member
    Join Date
    Mar 2006
    Posts
    41

    Smile Re: Where should code be placed?

    Thanks folks, I've taken note of what you've both said for future reference.
    Using: VB.net + C#.net 2005 Express Editions + .Net Framework 2.0

    ~ Man Is The Warmest Place To Hide ~

  5. #5
    Addicted Member
    Join Date
    Nov 2006
    Posts
    132

    Re: Where should code be placed?

    Quote Originally Posted by The Thing
    Thanks folks, I've taken note of what you've both said for future reference.
    The main purpose of a constructor is to initialize the state of the object. Anything your form needs for this purpose is appropriate to place in the constructor. However, there are a number of things you can't do with the form in the construtor, like setting focus on a control, so you will want to familiarize yourself with what can be done. A little experimentation might be helpful.

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