Hey guys,

I have some overloaded functions that I want to get out of my main form.cs - is there an easy way to do this? I know in C++ you can just #include somefile.h and whatnot, but I can't find a way to do this using C#? Basically, all I want is to move a large chunk of code from my main_form.cs to a separate code file, so my main_form.cs isn't so cluttered:

private void SQL_Update(string name, DateTime checkinDate, DateTime checkoutDate, string user, bool isCheckedOut)
{
    cmd = new SqlCommand("UPDATE Laptops SET Name=@name, isCheckedOut = @ischeckedout, Checkout_Date=@checkout, Checkin_Date=@checkin, Checked_Out_By=@checkedoutby", conn);

    cmd.Parameters.AddWithValue("@name", name);
    cmd.Parameters.AddWithValue("@checkin", checkinDate.Date);
    cmd.Parameters.AddWithValue("@checkout", checkoutDate.Date);
    cmd.Parameters.AddWithValue("@checkedoutby", user);
    cmd.Parameters.AddWithValue("@ischeckedout", isCheckedOut);
}

private void SQL_Update(string name, DateTime checkinDate, DateTime checkoutDate, string user, bool isCheckedOut, string authorizedby)
{
    cmd = new SqlCommand("UPDATE Laptops SET Name=@name, isCheckedOut = @ischeckedout, Checkout_Date=@checkout, Checkin_Date=@checkin, Checked_Out_By=@checkedoutby, Authorized_By=@authorizedby", conn);

    cmd.Parameters.AddWithValue("@name", name);
    cmd.Parameters.AddWithValue("@checkin", checkinDate.Date);
    cmd.Parameters.AddWithValue("@checkout", checkoutDate.Date);
    cmd.Parameters.AddWithValue("@checkedoutby", user);
    cmd.Parameters.AddWithValue("@authorizedby", authorizedby);
    cmd.Parameters.AddWithValue("@ischeckedout", isCheckedOut);
}

private void SQL_Update(string name, DateTime checkinDate, DateTime checkoutDate, string user, bool isCheckedOut, string authorizedby, string notes)
{
    cmd = new SqlCommand("UPDATE Laptops SET Name=@name, isCheckedOut = @ischeckedout, Checkout_Date=@checkout, Checkin_Date=@checkin, Checked_Out_By=@checkedoutby, Authorized_By=@authorizedby, Notes=@notes", conn);

    cmd.Parameters.AddWithValue("@name", name);
    cmd.Parameters.AddWithValue("@checkin", checkinDate.Date);
    cmd.Parameters.AddWithValue("@checkout", checkoutDate.Date);
    cmd.Parameters.AddWithValue("@checkedoutby", user);
    cmd.Parameters.AddWithValue("@authorizedby", authorizedby);
    cmd.Parameters.AddWithValue("@notes", notes);
    cmd.Parameters.AddWithValue("@ischeckedout", isCheckedOut);
}

private void SQL_Update(string name, DateTime checkinDate, DateTime checkoutDate, string user, string notes, bool isCheckedOut)
{
    cmd = new SqlCommand("UPDATE Laptops SET Name=@name, isCheckedOut = @ischeckedout, Checkout_Date=@checkout, Checkin_Date=@checkin, Checked_Out_By=@checkedoutby, Notes=@notes", conn);

    cmd.Parameters.AddWithValue("@name", name);
    cmd.Parameters.AddWithValue("@checkin", checkinDate.Date);
    cmd.Parameters.AddWithValue("@checkout", checkoutDate.Date);
    cmd.Parameters.AddWithValue("@checkedoutby", user);
    cmd.Parameters.AddWithValue("@notes", notes);
    cmd.Parameters.AddWithValue("@ischeckedout", isCheckedOut);
}

private void SQL_Update(DateTime checkinDate, bool isCheckedOut, string authorizedby)
{
    cmd = new SqlCommand("UPDATE Laptops SET isCheckedOut = @ischeckedout, Checkin_Date=@checkin, Authorized_By=@authorizedby", conn);

    cmd.Parameters.AddWithValue("@checkin", checkinDate.Date);
    cmd.Parameters.AddWithValue("@ischeckedout", isCheckedOut);
    cmd.Parameters.AddWithValue("@authorizedby", authorizedby);
}

Edited 6 Years Ago by Duki: n/a

Not sure that this is the best way to go about it, but I just added a c# class for my SQL command - this seems to work fine:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data.SqlClient;

namespace REG_Asset_Checkout
{
    class SQL_cmd
    {

        public SqlCommand cmd;
        public SqlConnection conn;

        public void SQL_Update(string name, DateTime checkinDate, DateTime checkoutDate, string user, bool isCheckedOut)
        {
            cmd = new SqlCommand("UPDATE Laptops SET Name=@name, isCheckedOut = @ischeckedout, Checkout_Date=@checkout, Checkin_Date=@checkin, Checked_Out_By=@checkedoutby", conn);

            cmd.Parameters.AddWithValue("@name", name);
            cmd.Parameters.AddWithValue("@checkin", checkinDate.Date);
            cmd.Parameters.AddWithValue("@checkout", checkoutDate.Date);
            cmd.Parameters.AddWithValue("@checkedoutby", user);
            cmd.Parameters.AddWithValue("@ischeckedout", isCheckedOut);
        }

        public void SQL_Update(string name, DateTime checkinDate, DateTime checkoutDate, string user, bool isCheckedOut, string authorizedby)
        {
            cmd = new SqlCommand("UPDATE Laptops SET Name=@name, isCheckedOut = @ischeckedout, Checkout_Date=@checkout, Checkin_Date=@checkin, Checked_Out_By=@checkedoutby, Authorized_By=@authorizedby", conn);

            cmd.Parameters.AddWithValue("@name", name);
            cmd.Parameters.AddWithValue("@checkin", checkinDate.Date);
            cmd.Parameters.AddWithValue("@checkout", checkoutDate.Date);
            cmd.Parameters.AddWithValue("@checkedoutby", user);
            cmd.Parameters.AddWithValue("@authorizedby", authorizedby);
            cmd.Parameters.AddWithValue("@ischeckedout", isCheckedOut);
        }

        public void SQL_Update(string name, DateTime checkinDate, DateTime checkoutDate, string user, bool isCheckedOut, string authorizedby, string notes)
        {
            cmd = new SqlCommand("UPDATE Laptops SET Name=@name, isCheckedOut = @ischeckedout, Checkout_Date=@checkout, Checkin_Date=@checkin, Checked_Out_By=@checkedoutby, Authorized_By=@authorizedby, Notes=@notes", conn);

            cmd.Parameters.AddWithValue("@name", name);
            cmd.Parameters.AddWithValue("@checkin", checkinDate.Date);
            cmd.Parameters.AddWithValue("@checkout", checkoutDate.Date);
            cmd.Parameters.AddWithValue("@checkedoutby", user);
            cmd.Parameters.AddWithValue("@authorizedby", authorizedby);
            cmd.Parameters.AddWithValue("@notes", notes);
            cmd.Parameters.AddWithValue("@ischeckedout", isCheckedOut);
        }

        public void SQL_Update(string name, DateTime checkinDate, DateTime checkoutDate, string user, string notes, bool isCheckedOut)
        {
            cmd = new SqlCommand("UPDATE Laptops SET Name=@name, isCheckedOut = @ischeckedout, Checkout_Date=@checkout, Checkin_Date=@checkin, Checked_Out_By=@checkedoutby, Notes=@notes", conn);

            cmd.Parameters.AddWithValue("@name", name);
            cmd.Parameters.AddWithValue("@checkin", checkinDate.Date);
            cmd.Parameters.AddWithValue("@checkout", checkoutDate.Date);
            cmd.Parameters.AddWithValue("@checkedoutby", user);
            cmd.Parameters.AddWithValue("@notes", notes);
            cmd.Parameters.AddWithValue("@ischeckedout", isCheckedOut);
        }

        public void SQL_Update(DateTime checkinDate, bool isCheckedOut, string authorizedby)
        {
            cmd = new SqlCommand("UPDATE Laptops SET isCheckedOut = @ischeckedout, Checkin_Date=@checkin, Authorized_By=@authorizedby", conn);

            cmd.Parameters.AddWithValue("@checkin", checkinDate.Date);
            cmd.Parameters.AddWithValue("@ischeckedout", isCheckedOut);
            cmd.Parameters.AddWithValue("@authorizedby", authorizedby);
        }

    }
}

Use a class with public static methods. This is not exactly what you are looking for but is a workaround that I often use. When you add a blank file to the project you will need to add the namespace. If you add a class file the namespace will be added for you. So you could add a MyLib class, here are two code snippets from two different files.

// This is in the FormProducts.cs file
namespace LockApp_Dashboard
{
    public partial class FormProducts : Form
    {
        public FormProducts()
        {
            InitializeComponent();
            MyLib.fu();
        }
}


// This is in the MyLib.cs file
namespace LockApp_Dashboard
{
    public class MyLib
    {
        public static void fu()
        {

        }

    }
}

When a class is marked partial (as forms are) you can have as many source files as you like for an object.

Just copy the same patern as the Form.designer.cs file.
E.G.
Form1SQLCodeFile.cs

namespace WindowsApplication1
{
    partial class Form1
    {
	// any code here is part of WindowsApplication1.Form1
    }
}

Form1ExtraCodeFile.cs

namespace WindowsApplication1
{
    partial class Form1
    {
	// any code here is part of WindowsApplication1.Form1
    }
}

Edited 6 Years Ago by nick.crane: n/a

This article has been dead for over six months. Start a new discussion instead.