Reputation: 123
I'm relatively new to C# coding in the .NET framework. I'm using Visual studios 10. I feel like this is a simple problem but whatever I seem to try with the visability, it doesn't work. This is the "try" with the least errors. I wanted to split the classes to be separate from the windows application form code. Maybe that's why it isn't working properly as when I ran a first try all in one (messy) windows app form code, it worked. Now splitting it into a neater package it won't. Please take a look at the code and tell me what I'm overlooking. I have searched the questions of course as well as googling, but the problem is that mostly this error has to do with arrays. For me it does NOT.
Here is the error:
'adm_LogOn' is a 'variable' but is used like a 'method' DemoTestEditor\DemoTestEditor\Form1.cs
private void buttonAdmLogOn_Click(object sender, EventArgs e)
{
// The console commands (above and below Writes to the Output.
// Messagebox also works but I think it's too "in your face"
Console.WriteLine("Admin Log On Button pressed.");
AdminClasses adm_LogOn = new AdminClasses(); // Creates a new object instance myALogOn
this.admTicket = adm_LogOn(userName.Text, passWord.Text);
if (this.admTicket != null)
{
Console.WriteLine("Got Ticket: " + this.admTicket);
this.buttonAdmLogOn.Enabled = true;
this.buttonAdmLogOff.Enabled = false;
}
}
And this is the class it's calling within the separate Admin.classes.cs code.
public string adm_LogOn(string username, string password)
{
// Initialize a (new object) adm service to work with.
SmartConnectionAdmin.SmartConnectionAdminService adm = new SmartConnectionAdmin.SmartConnectionAdminService();
// Setting up the arguments for the webservice (LogOnRequest) parameters
string ticket = null;
string server = null;
string clientName = "C# Client Suus";
string domain = null;
string clientAppName = "C# Tester Suus";
string clientAppVersion = null;
string clientAppSerial = null;
string clientAppCode = null;
try
{
// Place call with the above arguments
adm.LogOn(username, password, ref ticket, server, clientName, domain, clientAppName, clientAppVersion, clientAppSerial, clientAppCode);
// Displays the retrieved ticket
Console.WriteLine("The following ticket has been received: {0}", ticket);
}
catch (Exception e)
{
MessageBox.Show("Error: " + e.Message, "Oops!", MessageBoxButtons.OK, MessageBoxIcon.Error);
}
Console.WriteLine("UserName checked.");
return ticket;
}
}
Upvotes: 0
Views: 5093
Reputation: 460138
Since adm_LogOn
is a method in the class AdminClasses
, you should use it on an instance of it:
AdminClasses adm = new AdminClasses();
this.admTicket = adm.adm_LogOn(userName.Text, passWord.Text);
Note that i've renamed the variable adm_LogOn
to adm
, you've used the same variable name as the method. That works but is not good practise since it is confusing and prone to errors.
Upvotes: 3
Reputation: 20320
AdminClasses adm_LogOn = new AdminClasses();
myALogOn this.admTicket = adm_LogOn(userName.Text, passWord.Text);
Assuming adm_logon is a function of AdminClasses
the second line should be
this.admTicket = adm_logon.adm_LogOn(userName.Text, passWord.Text);
You really need to do something with your variable class and method names.
Upvotes: 1
Reputation: 48568
You are getting this error here
AdminClasses adm_LogOn = new AdminClasses(); // Creates a new object instance myALogOn
this.admTicket = adm_LogOn(userName.Text, passWord.Text);
Because adm_LogOn
is an object instance(variable) but you are calling it like a method.
It is preferred that object name and a method in class should be different.
AdminClasses adm = new AdminClasses(); // Creates a new object instance myALogOn
this.admTicket = adm.adm_LogOn(userName.Text, passWord.Text);
Upvotes: 1
Reputation: 7945
You might want to write:
AdminClasses adm = new AdminClasses();
this.admTicket = adm.LogOn(userName.Text, passWord.Text);
Be careful with copy/paste. The problem might have been caused with it.
Upvotes: -1
Reputation: 56697
AdminClasses adm_LogOn = new AdminClasses(); // Creates a new object instance myALogOn
this.admTicket = adm_LogOn(userName.Text, passWord.Text);
This is where the problem is. The AdminClasses
class contains the adm_LogOn
method. You need to call it like this:
this.admTicket = adm_LogOn.adm_LogOn(userName.Text, passWord.Text);
To make things more clear:
AdminClasses adm = new AdminClasses(); // Creates a new object instance myALogOn
this.admTicket = adm.adm_LogOn(userName.Text, passWord.Text);
Upvotes: 1
Reputation: 3500
this.admTicket = adm_LogOn(userName.Text, passWord.Text);
This is the problem line. You are trying to run a method that doesn't exist. Is there a sub method in that class that you are calling, or is it a constructor (in which case it has been run in the line above)?
Upvotes: -1
Reputation: 46425
public string adm_LogOn(string username, string password)
isn't a class definition, it is a method (within a class). You will need to create the surrounding object if it doesn't exist, then you can jsut call this method from the instance of that class.
If this method is within the class you are currently using, you can just call the method, i.e. you can remove this line:
AdminClasses adm_LogOn = new AdminClasses();
What you need to do is change it to this:
AdminClasses admClass = new AdminClasses(); // Creates a new object instance myALogOn
this.admTicket = admClass.adm_LogOn(userName.Text, passWord.Text);
Upvotes: 4