C# Class Library: multiple classes

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • =?Utf-8?B?QUEyZTcyRQ==?=

    #1

    C# Class Library: multiple classes

    I have a class library which contains 3 classes; one exposes all its
    behaviours and the other two are used internally by the public class. My
    problem is that although I can create an instance of the subservient classes,
    I cannot see the methods in them from the public class.

    Could someone guide me on this or give me a template?
    Thanks.
  • zacks@construction-imaging.com

    #2
    Re: C# Class Library: multiple classes

    On Oct 4, 8:29 am, AA2e72E <AA2e...@discus sions.microsoft .comwrote:
    I have a class library which contains 3 classes; one exposes all its
    behaviours and the other two are used internally by the public class. My
    problem is that although I can create an instance of the subservient classes,
    I cannot see the methods in them from the public class.
    >
    Could someone guide me on this or give me a template?
    Thanks.
    Sounds like the methods in the subservient classes are defined as
    Private. To see them from the primary class in the class library, they
    will need to be either Public or Friend.

    Comment

    • Jon Skeet [C# MVP]

      #3
      Re: C# Class Library: multiple classes

      On Oct 4, 1:29 pm, AA2e72E <AA2e...@discus sions.microsoft .comwrote:
      I have a class library which contains 3 classes; one exposes all its
      behaviours and the other two are used internally by the public class. My
      problem is that although I can create an instance of the subservient classes,
      I cannot see the methods in them from the public class.
      >
      Could someone guide me on this or give me a template?
      The two subservient classes should be declared as "internal", and any
      method you want to use from other classes in the same assembly should
      also be declared as "internal".

      Jon

      Comment

      • =?Utf-8?B?QUEyZTcyRQ==?=

        #4
        Re: C# Class Library: multiple classes

        Jon, I think I am still missing something. Here's the gist of the code ...
        please lookout for the comments for the questions I have. Thank you very much
        for your help.

        The function below is the <publicclass.

        public string TargetDB
        {
        get { return xTargetDB; }
        set
        {
        xTargetDB = value.ToUpper() ;
        switch (xTargetDB)
        {
        case "ORACLE":
        {
        TDB = new ORACLE();
        break;
        }
        default:
        {
        TDB = new SQLSERVER();
        break;
        }
        }
        }
        }

        public string xyz()
        {

        // I expected test() to be exposed below but it isn't

        return TDB.ToString() ; // I used ToString() just to have the
        project compile

        // I want to be able to run this statement: return TDB.test() ;
        // HOW?


        }

        ORACLE and SQLSERVER are the subservient classes:

        this is ORACLE.CS

        sing System;
        using System.Collecti ons.Generic;
        using System.Text;

        namespace ACM
        {
        internal class ORACLE
        {
        internal string test()
        {
        return "Oracle";
        }
        }
        }

        this is SQLSERVER.CS

        using System;
        using System.Collecti ons.Generic;
        using System.Text;

        namespace ACM
        {
        internal class SQLSERVER
        {
        internal string test()
        {
        return "SQLServer" ;
        }
        }
        }


        I am using a WindowsApplicat ion to test the class library:

        namespace ACMTESTER
        {
        public partial class Form1 : Form
        {
        Main CI = new ACM.Main();
        public Form1()
        {
        InitializeCompo nent();

        }

        private void Form1_Load(obje ct sender, EventArgs e)
        {

        }


        private void button1_Click(o bject sender, EventArgs e)
        {
        CI.TargetDB = "ORACLE";
        MessageBox.Show (CI.xyz());
        }
        }

        Comment

        • Jon Skeet [C# MVP]

          #5
          Re: C# Class Library: multiple classes

          On Oct 4, 3:28 pm, AA2e72E <AA2e...@discus sions.microsoft .comwrote:
          Jon, I think I am still missing something. Here's the gist of the code ...
          "The gist of the code" isn't terribly helpful, unfortunately. We've no
          idea what TDB is, for instance - a field, a property? What's its
          scope?

          You've got a problem in that your SQLSERVER and ORACLE classes (it
          would be good to start complying with naming conventions by the way) -
          they don't implement a common interface. You should define an
          interface containing the test() method, and make both classes
          implement that interface. TDB should be declared to be of that
          interface type, at which point your problems are likely to go away.
          I'm guessing that TDB is currently declared as type object - but
          unfortunately I can only guess at the moment.

          Jon

          Comment

          • =?Utf-8?B?QUEyZTcyRQ==?=

            #6
            Re: C# Class Library: multiple classes

            Yes, TDB is declared as object.
            I tried

            ORACLE TDB = new ORACLE();

            and SQLSERVER TDB = new SQLSERVER();

            but could not get test() exposed.

            Thanks for the quidance; I'll work on the 'common interface'.

            Comment

            • Jon Skeet [C# MVP]

              #7
              Re: C# Class Library: multiple classes

              On Oct 4, 4:01 pm, AA2e72E <AA2e...@discus sions.microsoft .comwrote:
              Yes, TDB is declared as object.
              I tried
              >
              ORACLE TDB = new ORACLE();
              >
              and SQLSERVER TDB = new SQLSERVER();
              >
              but could not get test() exposed.
              Either of those would have worked in terms of exposing the test()
              method, but then your TargetDB property's setter would fail, as you
              wouldn't be able to assign an ORACLE reference to a variable of type
              SQLSERVER or vice versa.
              Thanks for the quidance; I'll work on the 'common interface'.
              Righto. If you have any more problems, please post a short but
              *complete* program demonstrating them. See http://pobox.com/~skeet/csharp/complete.html

              Jon

              Comment

              Working...