I have been learning C# for the past few days for use with ASP.NET to create websites.
I am very new to C# but I have been thinking about how I should go about writing my code to make it as reusable as possible.
As a quick example, lets say I wanted to create a piece of code to check a users login details which I could just drop into another site at any time, and have it work with the data it gets given.
Remembering that I have no idea how I should layout my code to do this, this is the idea I came up with (I will keep it short with some kind of pseudo code):
First I create a class:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
namespace Module {
public class Login {
public static bool check_login(string usernameCheck = "", string passwordCheck = "") {
if(usernameCheck == "user" && passwordCheck == "password") {
return true;
}
return false;
}
}
}
Then I would have an aspx page where the login form would go, for example:
<asp:Content ContentPlaceHolderID="column1" runat="server">
<asp:TextBox ID="usernameInput" runat="server"></asp:TextBox>
<asp:TextBox ID="passwordInput" runat="server"></asp:TextBox>
<asp:Button OnClick="check_login" Text="Login" runat="server" />
</asp:Content>
And the code behind file would look like this:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
namespace Module {
public partial class _default : System.Web.UI.Page {
protected void Page_Load(object sender, EventArgs e) {
}
protected void check_login(object sender, EventArgs e) {
if(Login.check_login(usernameInput.Text, passwordInput.Text)) {
Response.Redirect("some other place");
}
}
}
}
This works as expected, but what I want to know is:
- Is there a better way to create reusable code?
- How do you design your reusable code?
I'm sure there must be a better way for me to do this, but I just can't think of it on my own.