NWoodsman
NWoodsman

Reputation: 505

Forming this Generic-Inside-Nongeneric-Interface Call

public interface IMyClassClient
{
    MyClass<T> Default();
    //T cannot be found, error
}

public class MyClass<T>
{
    private IMyClassClient owner;

    private MyClass<T> test;
    public MyClass<T> Test
    {
        get { return _test;}
        set
        {
           if(value ==owner.Default())
           {
              //how to form this call?^^^^^
              MessageBox.Show("Well that's true");
           }
        }
    }

}

The objective is that a class implementing IMyClassClient will handle figuring out a return value for IMyClassClient.Default()

What am I missing here?

EDIT:

After some thought, the following is a better wording of what I need to accomplish:

public interface IMyInterface
{
    //see Instance member in SomeClass, down below? How to declare it here?
}

public class SomeClass:IMyInterface
{
    public MyClass<int> Instance()
    {
         return new myClass<int>(); //for brevity
    }

}

Upvotes: 0

Views: 36

Answers (1)

Maksim Simkin
Maksim Simkin

Reputation: 9679

You should use generic in interface too, if you don't do it, you couldn't write MyClass<T> Default() , you don'T have any T there.

public interface IMyClassClient<T>
{
    MyClass<T> Default();
}

public class MyClass<T>
{
    private IMyClassClient<T> owner;

    private MyClass<T> test;
    public MyClass<T> Test
    {
        get { return test; }
        set
        {
            if (value == owner.Default())
            {
                MessageBox.Show("Well that's true");
            }
        }
    }
}

EDIT: Sure, you could declare geeric function in non generic interface

public interface IMyInterface
{
    MyClass<T> Instance<T>();
}



public class SomeClass : IMyInterface
{
    public MyClass<int> Instance()
    {
        return new myClass<int>(); 
    }
}

Upvotes: 1

Related Questions