I know you can’t static members from instance members.
But lets say I have in one assembly this:
public class ClassA
{
public List<order> GetOrders(int orderID)
{
...
}
}
Then in another assembly this:
public static ClassB
{
private static void DoSomethingElse(int orderID)
{
List<order> orderList = ClassA.GetOrders(orderID);
...rest of code
}
}
Is there any way to still get at that method in Class A some other way…some work around to this?
You certainly can access static members from instance members… but you should understand why you can’t access instance members without an instance.
Your class basically says that each instance of
ClassAallows you to get a list of orders associated with a particular ID. Now, different instances ofClassAmight give different results – for example, they might be connecting to different databases. Which results do you want to get inDoSomethingElse?To give a simpler example, suppose we had a
Personclass, and each person had a name:Does it make sense to ask “What is
Person.Name?” No – because you haven’t specified which person you’re talking about.You should either make
ClassA.GetOrdersstatic – if it doesn’t involve any per-instance information, including virtual members – or makeClassBaware of the instance ofClassAto use when finding out the orders.If you could let us know more realistic names for these classes, we could give guidance as to which solution is more likely to be appropriate… personally I would generally favour the latter approach, as static members generally lead to less testable code.