1

I have a simple business manager facade that should just persist some inventory information. However I keep getting the error that "Home.Services.InventorySvc" is a type being used as a variable.

My facade code.

using System; using System.Collections.Generic; using System.Linq; using System.Text; using Home.Domain; using Home.Services; namespace Home.Business { public class InventoryMngr { public void Create(CreateInventory inv) { Factory factory = Factory.GetInstance(); InventorySvc invSvc = (InventorySvc)factory.GetService( (InventorySvc).Name); invSvc.CreateInventory(inv); } } } 

The InventorySvc code

using System; using System.Collections.Generic; using System.Linq; using System.Text; using Home; using Home.Domain; namespace Home.Services { public interface InventorySvc : IService { void CreateInventory(CreateInventory createinventory); } } 
3
  • The homework tag is now officially deprecated so we can't use it on new questions unfortunately Commented Sep 30, 2012 at 20:48
  • 1
    Look at (InventorySvc).Name (since this was originally tagged as homework). Presumably, this should be a string from somewhere. You are using the type name, rather than an instance, so there is no object from which to retrieve the Name property. Commented Sep 30, 2012 at 20:50
  • yep, the factory.GetService call looks suspect. Commented Sep 30, 2012 at 20:51

2 Answers 2

3

You are using (InventorySvc).Name which is the same as InventorySvc.Name, so that would be accessing a static member of the InventorySvc class. As it's not a class but an interface, it can't have static members.

Sign up to request clarification or add additional context in comments.

1 Comment

Maybe that should be typeof(InventorySvc).Name. While InventorySvc can't be used as a "variable", typeof(InventorySvc) gives an object of type System.Type, and System.Type does have an instance property called Name (which it inherits from its base class System.Reflection.MemberInfo).
1
InventorySvc invSvc = (InventorySvc)factory.GetService((InventorySvc).Name); 

This code should retrieve an instance of a type which implements InventorySvc. However, it looks like you are attempting to pass an instance of the type, which wouldn't make sense--the syntax is wrong and you haven't yet instantiated the type. Presumably, you don't even know the specific type yet, just the interface type. The factory and the interface have abstracted the implementation (usually a good thing).

In your example, the factory probably expects the name of the type as a string (I won't overcomplicate the answer by debating if that is a good thing or not). In return, the factory will instantiate an instance which implements InventorySvc and return it. Only then can you invoke instance members such as the "Name" property.

As a side note, interface names should start with an I in c#, e.g. IInventorySvc. This makes it much clearer to you and anyone who reads your code that it is an interface.

Also see: https://stackoverflow.com/a/681815/453277

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.