Inheriting members of a class

Posted on 2005-05-09
Last Modified: 2010-04-16
I have a class with the following attribute

      public class customers : System.Web.UI.Page

protected System.Data.SqlClient.SqlConnection customer_connection;

How can I build or inherit a new instance of customer_connection, in this class? Both classes exist in the same namespace.

      "public class orders : System.Web.UI.Page"

Question by:sean-keys
    LVL 9

    Expert Comment

    It seems like you're asking two different things, neither of which are very clear, but I'll give it a shot.

    If you want to instantiate customer_connection from within an instance of the "customers" class you can simply use this:

    private void MyMethod()
       this.customer_connection = new SqlConnection();

    if you want to inherit it in a derived class and instantiate it from there you can do this:

    public class MyDerivedClass : customers
       private void MyMethod()
          this.customer_connection = new SqlConnection();

    customer_connection is visible to MyDerivedClass because it directly inherits from "customers" which provides the member as "protected"

    I'm not sure what the "orders" class is all about.  Perhaps you could clarify your question if I didn't answer it here.

    Author Comment

    I'm trying to use the SqlConnection (customer_connection) on another webform, but I'm not sure what the best way to do it is.

    LVL 9

    Accepted Solution

    You're trying to use it on another page that does not inherit from "customers"?  You can't do that.  But what you can do is put the SqlConnection in another class and access it statically.

    public class DataAccess
       public static SqlConnection Customer_Connection;

    and then you can access it from any other class.  This really isn't the preferred method but unless you're developing an extensive enterprise application it doesn't really matter.

    Write Comment

    Please enter a first name

    Please enter a last name

    We will never share this with anyone.

    Featured Post

    How your wiki can always stay up-to-date

    Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
    - Increase transparency
    - Onboard new hires faster
    - Access from mobile/offline

    Extention Methods in C# 3.0 by Ivo Stoykov C# 3.0 offers extension methods. They allow extending existing classes without changing the class's source code or relying on inheritance. These are static methods invoked as instance method. This…
    Entity Framework is a powerful tool to help you interact with the DataBase but still doesn't help much when we have a Stored Procedure that returns more than one resultset. The solution takes some of out-of-the-box thinking; read on!
    Migrating to Microsoft Office 365 is becoming increasingly popular for organizations both large and small. If you have made the leap to Microsoft’s cloud platform, you know that you will need to create a corporate email signature for your Office 365…
    Hi everyone! This is Experts Exchange customer support.  This quick video will show you how to change your primary email address.  If you have any questions, then please Write a Comment below!

    761 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    10 Experts available now in Live!

    Get 1:1 Help Now