C# Class Library: multiple classes

G

Guest

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.
 
Z

zacks

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.
 
J

Jon Skeet [C# MVP]

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
 
G

Guest

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 <public> class.

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.Collections.Generic;
using System.Text;

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

this is SQLSERVER.CS

using System;
using System.Collections.Generic;
using System.Text;

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


I am using a WindowsApplication to test the class library:

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

}

private void Form1_Load(object sender, EventArgs e)
{

}


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

Jon Skeet [C# MVP]

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
 
G

Guest

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'.
 
J

Jon Skeet [C# MVP]

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
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top