Trying to learn C# using abstract, override, ..., data layers...
I created a DAL and a BLL folder and added the classes but a running into some problems. This is what I have (just one file example):
DAL\Agent.cs
namespace DAL
{
public class Agent
{
public override int AddAgent(PLAgent newAgent) <---- DAL.Agent.AddAgent(...): no suitable method found to override {
OracleCommand oraCmd = new OracleCommand();
....
ExecuteOraNonQuery(oraCmd, this.ConnectionString);
}
}
}
DAL\AgentProvider.cs
namespace DAL
{
public abstract class AgentProvider : DataAccess
{
public abstract int AddAgent(PLAgent newAgent); }
}
BLL/AddAgent
namespace BLL
{
public class PLAgent
{
public static int AddAgent(PLAgent newAgent)
{
return Agent.AddAgent(newAgent);
}
}
}
In your Agent class, it is not inheriting from any other (except for System.Object), so you cannot override a method that doesn't have a method to override, i.e., a method for its hierarchy that has been marked as virtual.
It would appear that the Agent class needs to inherit from the AgentProvider class, but I'm not completely certain.
Christopher Reed, MCT, MCPD, MCTS, Microsoft Specialist, MTA
"The oxen are slow, but the earth is patient."
Thanks Careed. That got rid of the error but now I have a different problem which has to do with property/method access. This is a simplified version of two files in DAL and one in BLL folders:
AgentProvider.cs in DAL:
using BLL;
namespace DAL
{
public abstract class AgentProvider : DataAccess
{
public abstract int AddAgent(PLAgent newAgent);
....
}
}
Agent.cs in DAL:
using BLL;
namespace DAL
{
public class Agent : AgentProvider
{
public override int AddAgent(PLAgent newAgent)
{
Oracle queries to add an agent to database
can access "newAgent.AgentName"
}
}
}
Agent.cs in BLL:
using DAL;
namespace BLL
{
public class PLAgent
{
public int AgentID;
public string AgentName;
....
public static int AddAgent(PLAgent newAgent)
{
return Agent.AddAgent(newAgent); <---- Problem!
}
}
}
Cannot access Agent.AddAgent method of DAL. When I type "Agent." intellisense just shows "Equals" and "referenceEqulas". Can only do PLAgent.AddAgent(newAgent) but it is just the same method I am already in!
I solved the access issue using the following method in AgentProvider.cs file. What I am not sure is if I have to do this for every xxxProvider.cs file (I have dozen or so) or if this can be made into a generic method:
public static DataAccess GetDataAccess()
{
string dataAccessStringType = "DAL.AgentProvider";
Type dataAccessType = Type.GetType(dataAccessStringType);
if (dataAccessType == null)
{
throw (new NullReferenceException("DataAccessType can not be found"));
}
Type tp = Type.GetType("DAL.AgentProvider");
if (!tp.IsAssignableFrom(dataAccessType))
{
throw (new ArgumentException("DataAccessType does not inherits from DAL.AgentProvider"));
}
DataAccess dc = (DataAccess)Activator.CreateInstance(dataAccessType);
return (dc);
}
And then in Agent.cs (BLL folder):
public static int AddAgent(PLAgent newAgent)
{
Agent DALLayer = Agent.GetDataAccess();
return DALLayer.AddAgent();
}
Marked as answer by Angie xu - MSFT on Dec 11, 2012 06:35 AM
NoBullMan
Participant
1019 Points
780 Posts
Help with abstract/override
Dec 04, 2012 08:40 PM|LINK
Trying to learn C# using abstract, override, ..., data layers...
I created a DAL and a BLL folder and added the classes but a running into some problems. This is what I have (just one file example):
DAL\Agent.cs
namespace DAL { public class Agent { public override int AddAgent(PLAgent newAgent) <---- DAL.Agent.AddAgent(...): no suitable method found to override { OracleCommand oraCmd = new OracleCommand(); .... ExecuteOraNonQuery(oraCmd, this.ConnectionString); } } }DAL\AgentProvider.cs
namespace DAL { public abstract class AgentProvider : DataAccess { public abstract int AddAgent(PLAgent newAgent); } }BLL/AddAgent
namespace BLL { public class PLAgent { public static int AddAgent(PLAgent newAgent) { return Agent.AddAgent(newAgent); } } }Can you tell me why I get these errors?
Thanks.
Careed
All-Star
18764 Points
3637 Posts
Re: Help with abstract/override
Dec 04, 2012 09:22 PM|LINK
In your Agent class, it is not inheriting from any other (except for System.Object), so you cannot override a method that doesn't have a method to override, i.e., a method for its hierarchy that has been marked as virtual.
It would appear that the Agent class needs to inherit from the AgentProvider class, but I'm not completely certain.
"The oxen are slow, but the earth is patient."
NoBullMan
Participant
1019 Points
780 Posts
Re: Help with abstract/override
Dec 05, 2012 05:29 PM|LINK
Thanks Careed. That got rid of the error but now I have a different problem which has to do with property/method access. This is a simplified version of two files in DAL and one in BLL folders:
AgentProvider.cs in DAL:
using BLL; namespace DAL { public abstract class AgentProvider : DataAccess { public abstract int AddAgent(PLAgent newAgent); .... } }Agent.cs in DAL:
using BLL; namespace DAL { public class Agent : AgentProvider { public override int AddAgent(PLAgent newAgent) { Oracle queries to add an agent to database can access "newAgent.AgentName" } } }Agent.cs in BLL:
using DAL; namespace BLL { public class PLAgent { public int AgentID; public string AgentName; .... public static int AddAgent(PLAgent newAgent) { return Agent.AddAgent(newAgent); <---- Problem! } } } Cannot access Agent.AddAgent method of DAL. When I type "Agent." intellisense just shows "Equals" and "referenceEqulas". Can only do PLAgent.AddAgent(newAgent) but it is just the same method I am already in!NoBullMan
Participant
1019 Points
780 Posts
Re: Help with abstract/override
Dec 05, 2012 06:27 PM|LINK
I solved the access issue using the following method in AgentProvider.cs file. What I am not sure is if I have to do this for every xxxProvider.cs file (I have dozen or so) or if this can be made into a generic method:
public static DataAccess GetDataAccess() { string dataAccessStringType = "DAL.AgentProvider"; Type dataAccessType = Type.GetType(dataAccessStringType); if (dataAccessType == null) { throw (new NullReferenceException("DataAccessType can not be found")); } Type tp = Type.GetType("DAL.AgentProvider"); if (!tp.IsAssignableFrom(dataAccessType)) { throw (new ArgumentException("DataAccessType does not inherits from DAL.AgentProvider")); } DataAccess dc = (DataAccess)Activator.CreateInstance(dataAccessType); return (dc); }And then in Agent.cs (BLL folder):
public static int AddAgent(PLAgent newAgent) { Agent DALLayer = Agent.GetDataAccess(); return DALLayer.AddAgent(); }