We help IT Professionals succeed at work.

We've partnered with Certified Experts, Carl Webster and Richard Faulkner, to bring you two Citrix podcasts. Learn about 2020 trends and get answers to your biggest Citrix questions!Listen Now

x

Virtual method

Mr_Shaw
Mr_Shaw asked
on
Medium Priority
215 Views
Last Modified: 2012-05-06
I have created a Virtual method and world like to re-use it by means of overriding in another class. Howerver it does not seem possible to do the following

public override DataSet listTasks()
{
stored_procedure_name="test";
productID="1";
}

I assume that the syntax is all wrong!
public abstract class baseclass
{
	
  #region properties
    private string stored_procedure_name;
    public string Stored_Procedure_Name
    {
 
        get
        {
            return stored_procedure_name;
        }
        set
        {
            stored_procedure_name = value;
        }
    }
 
    private string productID;
    public string ProductID
    {
 
        get
        {
            return productID;
        }
        set
        {
            productID = value;
        }
    }
#endregion
 
#region list tasks
    public virtual DataSet listTasks()
    {
 
        DataSet ResultsDataset = new DataSet();
        ConnectionStringSettings settings;
        settings = ConfigurationManager.ConnectionStrings["REQ"];
        using (OleDbConnection conn = new OleDbConnection(settings.ConnectionString))
        {
            conn.Open();
 
            //Select stored procedure
            OleDbCommand cmd = new OleDbCommand("Req1." + stored_procedure_name, conn);
            cmd.CommandType = CommandType.StoredProcedure;
 
            cmd.Parameters.Add("@ProductID", SqlDbType.VarChar).Value = productID.ToString();
            
            //Create a new DataAdapter in order to load the query results into a dataset.
            OleDbDataAdapter newdataAdapter = new OleDbDataAdapter(cmd);
            newdataAdapter.Fill(ResultsDataset);
            conn.Close();
 
        }
        return ResultsDataset;
    }
    #endregion
}

Open in new window

Comment
Watch Question

Author

Commented:
the article is a bit complicated for a someone trying to learn thinngs for the first time.

Technical Manager
Commented:
i know the article is a bit criptic but it explains the complexity the new keyword puts into the overridden scenario

if you want to read more then please go throught this link
http://www.java2s.com/Tutorial/CSharp/0140__Class/0460__virtual.htm

Not the solution you were looking for? Getting a personalized solution is easy.

Ask the Experts
Fernando SotoRetired
CERTIFIED EXPERT
Distinguished Expert 2017

Commented:
Hi Mr_Shaw;

From Microsoft Documentation:
Use the abstract modifier in a class declaration to indicate that a class is intended only to be a base class of other classes. Members marked as abstract, or included in an abstract class, must be implemented by classes that derive from the abstract class.

Here is a class MySubclass which derives from the abstract class baseclass.

public class MySubclass : baseclass
{
    // Create extra fields and motheds
    public string x;
   
    // This is the method that override the baseclass listTasks correctly
    public override DataSet listTasks()
    {
        // Do some extra processing
       
        // Class the baseclass version of listTasks
        return base.listTasks();
    }
}

Now somewhere in your application you can create an instance of MySubclass.

Fernando
Fernando SotoRetired
CERTIFIED EXPERT
Distinguished Expert 2017
Commented:
Sorry; this

        // Class the baseclass version of listTasks
        return base.listTasks();

Should read as:

        // Call the baseclass version of listTasks
        return base.listTasks();
Tony McCreathTechnical SEO Consultant
Commented:
Are you trying to have the original (base) method run using the values you specify in the override? See snippet #1




public override DataSet listTasks()
{
  stored_procedure_name="test";
  productID="1";
 
  return base.listTasks();
}

Open in new window

Author

Commented:
thanks
Access more of Experts Exchange with a free account
Thanks for using Experts Exchange.

Create a free account to continue.

Limited access with a free account allows you to:

  • View three pieces of content (articles, solutions, posts, and videos)
  • Ask the experts questions (counted toward content limit)
  • Customize your dashboard and profile

*This site is protected by reCAPTCHA and the Google Privacy Policy and Terms of Service apply.

OR

Please enter a first name

Please enter a last name

8+ characters (letters, numbers, and a symbol)

By clicking, you agree to the Terms of Use and Privacy Policy.