+ Post New Thread
Results 1 to 3 of 3
Coding Thread, VB .Net best practice regarding classes in Coding and Web Development; I'm a relative self taught noob when it comes to VB .Net and I have which is probably a very ...
  1. #1
    Duke5A's Avatar
    Join Date
    Jul 2010
    Posts
    807
    Thank Post
    83
    Thanked 132 Times in 115 Posts
    Blog Entries
    8
    Rep Power
    32

    VB .Net best practice regarding classes

    I'm a relative self taught noob when it comes to VB .Net and I have which is probably a very basic question. I'm coding an application that will import a CSV and add students to Active Directory. I've actually done this in the past with VB6, but I'm doing a total rewrite in .Net this time and designing it so its not customized to our AD structure. The goal is to have something universal that can work for just about anyone. Part of this goal involves loading and saving settings. In my main form I've got a few subs that will both load and save settings to an XML file. There is also another sub that will create the XML from scratch with default values if it is missing. Everything works, but the form is getting rather crowded and I was wondering if it would be considered best practice to take the existing subs and push them off into their own classes for the sake of organization. Or am I looking at this the wrong way? Thanks guys...

  2. #2

    Steve21's Avatar
    Join Date
    Feb 2011
    Location
    Swindon
    Posts
    2,698
    Thank Post
    335
    Thanked 516 Times in 484 Posts
    Rep Power
    180
    Well you could always create an "XML" module, and just link the public functions across to the main form. Would clear it up nicely.

    Steve

  3. #3
    ChrisMoore's Avatar
    Join Date
    Mar 2012
    Location
    Scotland
    Posts
    12
    Thank Post
    1
    Thanked 2 Times in 2 Posts
    Rep Power
    6
    Breaking it up seems a sensible approach.

    I have a base settings class. This deals with opening or creating settings files, adding/amending/deleting settings by name and handling security. Then I inheret from this class to make a specific example which includes the settings I need as properties. These properties can then be bound to boxes and controls on the form either manually or via binding.

    This makes the code in my form very small (my code is C# but it should be easy to translate)

    Form create :

    Code:
    Settings sett = new Settings(fileName);

    Read settings :
    Code:
    try
    {
       sett.ReadSettings();
    
       txtUserName.Text = sett.UserName;
       txtDepartment.Text = sett.Department;
       :
       :
    }
    catch(Exception ex)
    {
        MessageBox.Show("Error when reading settings - " + ex.Message);
    }
    and going the other way, Save Settings:

    Code:
    try
    {
       sett.UserName = txtUserName.Text;
       sett.Department = txtDepartment.Text;
       :
       :
       sett.SaveSettings();
    }
    catch(Exception ex)
    {
        MessageBox.Show("Error when saving settings - " + ex.Message);
    }
    Instead of manually populating the fields you could use binding by implementing IPropertyNotified in your base settings class and have each property call OnPropertyChanged(<propertyname>) on each setter :

    Code:
    private string _UserName;
    public string UserName
    {
        get
        {
           return _UserName;
        }
    
       set
       {
           _UserName = value;
           OnPropertyChanged("UserName");
       }
    }
    If you don't then any changes made in the form won't get back to the underlying settings class.

SHARE:
+ Post New Thread

Similar Threads

  1. Replies: 1
    Last Post: 25th May 2011, 01:14 PM
  2. VB.Net Help
    By mloveless in forum Coding
    Replies: 2
    Last Post: 29th June 2007, 04:24 PM
  3. DFS/FRS: best practice?
    By Oops_my_bad in forum Windows
    Replies: 3
    Last Post: 4th June 2007, 11:18 AM
  4. DHCP best practice and advice again
    By tosca925 in forum Windows
    Replies: 11
    Last Post: 23rd November 2006, 08:49 PM
  5. Implementing best practice ICT management and support
    By FITS in forum Courses and Training
    Replies: 16
    Last Post: 8th September 2005, 02:24 PM

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Posting Permissions

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