1

Can someone correct my code? I want to create an interface object and use it methods but Im stuck with this code..

Visual Studio Error prompt : Use of unassigned local variable 'MyInterface'

IBattleNetSettings MyInterface;
        MyInterface.Username = "MyUsername";
        BattleNetClient cs = new BattleNetClient(MyInterface); 

btw , if you want the interface class here's the code: IBattleNetSettings

and for the BattleNetClient class : BattleNetClient <-- Take a look at this code.Its constructor requires an instance of IBattleNetSettings interface.

I just want to create an instance of BattleNetClient class but Im stucked since It requires an instance from IBattleNetSettings and I dont know how to do it.

How can I create instance of BattleNetClient class?

Thanks in Advance : )

2 Answers 2

4

You can't create instances of interfaces. You should create (or otherwise obtain) an instance of a class that implements that interface:

IBattleNetSettings settings = new BattleNetSettings();

If the project doesn't provide an implementation you can use then you may have to write one yourself:

public class BattleNetSettings : IBattleNetSettings 
{
    public string Username { get; set; }
    // etc...
}
Sign up to request clarification or add additional context in comments.

4 Comments

ye , Im quite aware of it but the problem is that I just got it from a dll file that i've downloaded from net.. Im not the one who write that class. I jsut want to implement it in my program.
@user848682: Then you can implement it yourself. You can use the code I have shown in my answer.
The problem is Im not the one who create the dll file.. Is there any other way to achieve it? I just want to create an intance of BattleNetSettings class but the problem is that its constructor requires an Instance of IbattleNetSettings interface
@user848682: It doesn't matter if you are not the one who created the DLL. You can still implement the class yourself, regardless of who made the DLL. See the code in my answer. Have you even tried it?
2

Consider assigning to it?

An interface is just that... An interface. You will want to use a concrete implementation.

For example, you may wish to use a class named BattleNetSettings which implements IBattleNetSettings, you can use:

IBattleNetSettings MyInterface = new BattleNetSettings();
MyInterface.Username = "MyUsername";
BattleNetClient cs = new BattleNetClient(MyInterface);

However, the use of the name "MyInterface" is very confusing, I suggest using a more appropriate name. :)

3 Comments

I tried that code but it gives me a new error : The type or namespace name 'BattleNetSettings' could not be found (are you missing a using directive or an assembly reference?)
@user848682: The class name I provided was an example. Is there a particular implementation you wish to use? If so, USE IT!
@user848682: If an implementation is not provided, you are most likely expected to write the implementation yourself.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.