Casting question

  • Thread starter Thread starter Steven Blair
  • Start date Start date
S

Steven Blair

Hi,

I have the follwoing code:

( (cSqlProviderFactory)m_factory ).GetConnectionString();
( (cODBCProviderFactory)m_factory ).GetConnectionString();

I want this to be one line only (I have quite few different classes)

Is it possible, in C# to do soemthing like this:

Type myType = m_Factory.GetType();
( (myType)m_factory ).GetConnectionString();

Regards,

Steven
 
Going blind here as you don't say what the relationship between the factory classes is

abstract class ProviderFactory
{
public abstract string GetConnectionString();
}

class cSqlProviderFactory : ProviderFactory
{
public override string GetConnectionString();
{
return .....;
}
}

class

class cODBCProviderFactory : ProviderFactory
{
public override string GetConnectionString();
{
return .....;
}
}

class Foo
{
ProviderFactory m_factory;
public void UseFactory()
{
string connStr = m_factory.GetConnectionString();
}
}

This uses polymorphism to get the appropriate connection string only knowing about the base factory

Whether this does what you want depends on your implementation however, if this doesn't meet you requirements you'll have to give us more information.

Regards

Richard Blewett - DevelopMentor
http://www.dotnetconsult.co.uk/weblog
http://www.dotnetconsult.co.uk

Hi,

I have the follwoing code:

( (cSqlProviderFactory)m_factory ).GetConnectionString();
( (cODBCProviderFactory)m_factory ).GetConnectionString();

I want this to be one line only (I have quite few different classes)

Is it possible, in C# to do soemthing like this:

Type myType = m_Factory.GetType();
( (myType)m_factory ).GetConnectionString();

Regards,

Steven
 
I have defined an interface which all my DB classes inherit from:

public interface IDbProviderFactory
{
IDbConnection CreateConnection( string connectionString );
}


And a class inherits from this:

public class cSqlProviderFactory : IDbProviderFactory
{
public IDbConnection CreateConnection( string c )
{
return new SqlConnection( connectionString );
}

I want each inheritted class to have its own GetConnectionString, but
each version has the possibility of different parameters being passed
in.

Hope this is more helpful.

Regards,

Steven
 
Well I don't know if this was intentional but your design is almost how ADO.NET 2.0 is architected for the next release of .NET. Here they define a ConnectionStringFactory class that has all the standard things like server, database, etc and also a hashtable to put the other key/value pairs. You can override ToSTring in your version to return the complete provider specific connection string. So GetConnectionString returns the factory which is used to create the bits of teh connection string.

Regards

Richard Blewett - DevelopMentor
http://www.dotnetconsult.co.uk/weblog
http://www.dotnetconsult.co.uk

I have defined an interface which all my DB classes inherit from:

public interface IDbProviderFactory
{
IDbConnection CreateConnection( string connectionString );
}


And a class inherits from this:

public class cSqlProviderFactory : IDbProviderFactory
{
public IDbConnection CreateConnection( string c )
{
return new SqlConnection( connectionString );
}

I want each inheritted class to have its own GetConnectionString, but
each version has the possibility of different parameters being passed
in.

Hope this is more helpful.

Regards,

Steven
 
Then you should be casting to the Interface rather than to the class
name:

( (IDbProviderFactory)m_factory ).GetConnectionString();



And, by the way, your classes do not inherit from the interface, they
implement it.

Cheers
 
Back
Top