interface/static in VS2005

C

Colin Basterfield

Hi,

I have some factory classes that all do a common thing, generate a query, so
I thought I would have each of them implement an interface, however it
doesn't seem to let me do it

interface IQueryByCriteriaFactory
{

MySqlCommand Generate(DomainToDBMap dbMap, QueryObject queryObject);

}

and the class that implements it



public class QueryByCriteriaFactory : IQueryByCriteriaFactory

{

#region IQueryByCriteriaFactory Members

public static MySqlCommand IQueryByCriteriaFactory.Generate(DomainToDBMap
dbMap, QueryObject queryObject)

{

return new MySqlCommand(

"select " + dbMap.FullColumnList("", "", false, false)

+ " from " + dbMap.TableName

);

}

#endregion

}

If i do this it says public and static aren't valid for this item.

If I take these out it can't find the method that the caller wants

Obviously I could instantiate this class, but I kinda like the idea of all
factories to use static methods



thoughts anyone?

many thanks

Colin
 
A

Adam Calderon (MCSD, MCAD, MCP)

Ok,
I think what is going on here is that you are using "instance"
metaphors which require vtable binding in a non-instance situation.
What this means is that static methods do not require an instance of
the class to be created, you use the class name and method name to
invoke the method. When using inheritance and interfaces you are using
a vtable lookup mechanism which requires an instance of the class
using the new operator. In short I don't believe this is not going to
work and you should just put the method in the factory. If anyone sees
this differently please chime in.


Adam Calderon (MCSD,MCAD,MCP)
 
G

Guest

If i do this it says public and static aren't valid for this item.

Interface methods cannot be static, so you are stuck.
If I take these out it can't find the method that the caller wants

You also used the interface name on the method implementation - that is why
the compiler is complaining about the "public" part - this is a separate
issue not related to the problem with static. If you use this technique, the
method can only be invoked through an interface reference (which would also
require an object to be instantiated of course).
 
C

Colin Basterfield

Thanks for that Mark
Colin

MarkT said:
Interface methods cannot be static, so you are stuck.


You also used the interface name on the method implementation - that is
why
the compiler is complaining about the "public" part - this is a separate
issue not related to the problem with static. If you use this technique,
the
method can only be invoked through an interface reference (which would
also
require an object to be instantiated of course).
 

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