Method advice

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • jez123456

    Method advice

    Hi I have the following code which runs ok from a button click event

    private void btnCompact_Clic k(object sender, System.EventArg s e)
    {
    string mdbComp = @"C:\Compact.md b";
    string mdbTemp = @"C:\tempdb.mdb ";

    JRO.JetEngine jro;
    jro = new JRO.JetEngine() ;
    jro.CompactData base(@"Provider =Microsoft.Jet. OLEDB.4.0;Data Source=" +
    mdbComp + @"", @"Provider=Micr osoft.Jet.OLEDB .4.0;Data Source=" + mdbTemp +
    @";Jet OLEDB:Engine Type=5");

    File.Delete(mdb Comp);
    File.Move(mdbTe mp, mdbComp);

    MessageBox.Show ("Finished Compacting Database!");
    }

    Would this code be better split up so that it calls a method. If so, what is
    the best way, also how would the strings mdbComp and mdbTemp be passed.
  • Olorin

    #2
    Re: Method advice

    hmmmmm...here's my $0.02:

    1~ make the mdbComp and mdbTemp strings private constants of the
    surrounding class, or, if they can be set in a config file, make them
    private fields of the surrounding class and have them initialized in
    the class contructor (or in the static constructor);

    2~ Yes, I think your idea to move the code that actually does stuff
    away from this method is good. I usually like to have my event handler
    only deal with GUI, and calling other methods for the actual function
    implementation. After all, it may be that another button will have to
    call the same functionality.. . so,

    public class MySample
    {
    private const string mdbComp = @"C:\Compact.md b";
    private const string mdbTemp = @"C:\tempdb.mdb ";

    private void btnCompact_Clic k(object sender, System.EventArg s e)
    {
    try
    {
    this.CompactDat abase();
    MessageBox.Show (this, "Database has been compacted.");
    }
    catch(Exception e2)
    {
    MessageBox.Show (this, "An Exception occurred: "+e2.Messag e);
    }
    }
    private void CompactDatabase ()
    {
    try
    {
    JRO.JetEngine jro;
    jro = new JRO.JetEngine() ;
    jro.CompactData base(@"Provider =Microsoft.Jet. OLEDB.4.0;Data Source=" +
    MySample.mdbCom p + @"", @"Provider=Micr osoft.Jet.OLEDB .4.0;Data
    Source=" + MySample.mdbTem p +
    @";Jet OLEDB:Engine Type=5");

    File.Delete(MyS ample.mdbComp);
    File.Move(MySam ple.mdbTemp, MySample.mdbCom p);
    }
    catch(Exception e)
    {
    throw new ApplicationExce ption("An Exception occurred while
    compacting the database:\n"+
    e.Message, e);
    }
    }
    }

    HTH,
    F.O.R.

    Comment

    • jez123456

      #3
      Re: Method advice

      Thanks, that works great.

      The only other thing is that there could be upto 7 different databases to
      compact. Would it be better to pass these names into the CompactDatabase
      method as parameters?

      "Olorin" wrote:
      [color=blue]
      > hmmmmm...here's my $0.02:
      >
      > 1~ make the mdbComp and mdbTemp strings private constants of the
      > surrounding class, or, if they can be set in a config file, make them
      > private fields of the surrounding class and have them initialized in
      > the class contructor (or in the static constructor);
      >
      > 2~ Yes, I think your idea to move the code that actually does stuff
      > away from this method is good. I usually like to have my event handler
      > only deal with GUI, and calling other methods for the actual function
      > implementation. After all, it may be that another button will have to
      > call the same functionality.. . so,
      >
      > public class MySample
      > {
      > private const string mdbComp = @"C:\Compact.md b";
      > private const string mdbTemp = @"C:\tempdb.mdb ";
      >
      > private void btnCompact_Clic k(object sender, System.EventArg s e)
      > {
      > try
      > {
      > this.CompactDat abase();
      > MessageBox.Show (this, "Database has been compacted.");
      > }
      > catch(Exception e2)
      > {
      > MessageBox.Show (this, "An Exception occurred: "+e2.Messag e);
      > }
      > }
      > private void CompactDatabase ()
      > {
      > try
      > {
      > JRO.JetEngine jro;
      > jro = new JRO.JetEngine() ;
      > jro.CompactData base(@"Provider =Microsoft.Jet. OLEDB.4.0;Data Source=" +
      > MySample.mdbCom p + @"", @"Provider=Micr osoft.Jet.OLEDB .4.0;Data
      > Source=" + MySample.mdbTem p +
      > @";Jet OLEDB:Engine Type=5");
      >
      > File.Delete(MyS ample.mdbComp);
      > File.Move(MySam ple.mdbTemp, MySample.mdbCom p);
      > }
      > catch(Exception e)
      > {
      > throw new ApplicationExce ption("An Exception occurred while
      > compacting the database:\n"+
      > e.Message, e);
      > }
      > }
      > }
      >
      > HTH,
      > F.O.R.
      >
      >[/color]

      Comment

      Working...