I have a program in which the user enters a username and a date and then clicks a button which inputs the data into a database.
The problem I am having is that I cannot seem to pass the text box date data as an argument in a method to my BOL class. I get the following error: “Member ‘P90XPRogramt.ProgramLoginBOL.TestParsing(string)’ cannot be accessed with an instance reference; qualify it with a type name instead”
Here is the code from my UI layer:
public partial class ProgramLoginForm : Form
{
private ProgramLoginBOL busObject =
new ProgramLoginBOL();
//default constructor
public ProgramLoginForm(string dayNo)
{
InitializeComponent();
busObject.InitializeConnection();
}
private void btnBeginProgram_Click(object sender, EventArgs e)
{
busObject.UserName = txtEnterName.Text;
busObject.TestParsing(txtStartDate.Text);
}
//event handler to close program
private void btnExitProgram_Click(object sender, EventArgs e)
{
Application.Exit();
}
}
Here is the code for my BOL class:
class ProgramLoginBOL
{
//declare variables
string userName = "";
//instantiate a connecttion object to Access database
OleDbConnection aConnection =
new OleDbConnection(
"Provider=Microsoft.ACE.OLEDB.12.0;" +
"Data Source=P90XDatabase.accdb;");
public ProgramLoginBOL()
{
}
//property for userName variable
public string UserName
{
get { return userName; }
set { userName = value; }
}
public static DateTime TestParsing(string datetext)
{
DateTime dt;
if (DateTime.TryParseExact(datetext, "d", null, 0, out dt))
{
Console.WriteLine("Parsed to {0}", dt);
}
else
{
Console.WriteLine("Bad date");
}
return dt;
}
public void InitializeConnection()
{
ProgramLoginDAL.InitializeConnection(aConnection);
}
}
I’m not sure what it is telling me to do. By type name I assume they mean data type, so I tried the following:
string busObject.TestParsing(txtStartDate.Text);
string.busObject.TestParsing(txtStartDate.Text);
string.TestParsing(txtStartDate.Text);
busObject.TestParsing(string txtStartDate.Text);
None of these have worked. I also tried using DateTime instead of string. As I said, I’m at a loss as to what it wants me to do.
Static methods can’t be called with instances of a class exactly as error message says.
For future errors try searching for error code first – normally MSDN article contains explanation and sample, like in this case CS0176