views:

46

answers:

4

i try to call base.Alan(); in HacimBul. But base. dont give intellisense alan method

   public double HacimBul()
        {
            throw new Exception();
            //return base..... --> how can i see base.Alan();
        }

namespace interfaceClass
{
    class Program
    {
        static void Main(string[] args)
        {

        }
    }

    interface Ikenar
    {
       double kenar { get; set; }
    }

    interface Iyukseklik
    {
        double yuksekli {get; set;}
    }
    interface IAlan
    {
        double Alan();
    }
    interface IHacim
    {
        double Hacim();
    }

    class Alan : Ikenar, IAlan
    {
        public double kenar
        {
            get;
            set;
        }

        double IAlan.Alan()
        {
            return kenar * kenar;
        }
    }

    class Hacim : Alan, Iyukseklik
    {

        public double kenar
        {
            get;
            set;
        }

        public double yuksekli
        {
            get;
            set;
        }

        public double HacimBul()
        {
            throw new Exception();
            //return base..... --> how can i see base.Alan();
        }
   }
}

A: 
double IAlan.Alan() // this is explicit interface implementation
{
   return kenar * kenar;
}

By prefixing the method name with the interface, you are telling the user that the method Alan will be usable only through a reference of the interface & not of the class.

Change the above to

double Alan()
{
   return kenar * kenar;
}

inside Alan class.

shahkalpesh
that won't work since the class is now private and hence not accessible to the deriving classes
Rune FS
Well, it would work if it were public rather than private... but he'd also need to rename the class, as it has the same name as the method. It would be worth renaming anyway though...
Jon Skeet
@my self and it should have said that the method is now private
Rune FS
+1  A: 

No, it won't work at the moment. The base implementation is only available on an expression of type IAlan, because of explicit interface implementation.

You can use this though:

return ((IAlan)this).Alan();

Or you can use implicit interface implementation in Alan instead, although that will mean renaming the class or the method.

For others trying to get a grip on this problem, it's more simply expressed like this - in a way which doesn't reuse method names as class names:

public interface IFoo
{
    void Bar();
}

public class BaseFoo : IFoo
{
    void IFoo.Bar()
    {
        // Do something
    }
}

public class DerivedFoo : BaseFoo
{
    void OtherMethod()
    {
        // Doesn't compile due to explicit interface implementation
        base.Bar();

        // Will work
        ((IFoo)this).Bar();
    }
}
Jon Skeet
A: 

You have made an explicit implementation of Alan so you have to options

either change the name of the method or the class called Alan so you dont have a name clash and implement the method as a normal method E.g

public double Alan() //name of class can't be Alan in this case
{
   return kenar * kenar;
}

or

public double HacimBul()
 {
    return ((IAlan)this).Alan();
 }

The reason way you don't get intellisense is that the method is only available when the obejct of the type Alan is declared as IAlan otherwise the Alan method will be hidden

Rune FS
+1  A: 

The method is not public or protected, it is private the way you've defined it, and even more private than just private as well, it is only available if you go through the interface.

This means that in order to call the method you will have to:

  • Change the explicit implementation of your Alan method to an implicit one (making the method public) in Alan:

    public void Alan()
    {
    }
    
  • or, you will have to cast the instance during the call:

    ((IAlan)this).Alan();
    

Unfortunately, with that last syntax there, you won't actually be calling "base.Alan", but rather just "this.Alan", which might make a difference if you also override that method in the descendant class.

Here's an example:

using System;

namespace interfaceClass
{
    public interface ITest
    {
        void Execute();
    }

    public class Base : ITest
    {
        void ITest.Execute()
        {
            Console.Out.WriteLine("Base.ITest.Execute");
        }
    }

    public class Descendant : Base, ITest
    {
        void ITest.Execute()
        {
            Console.Out.WriteLine("Descendant.ITest.Execute");
        }

        public void Test()
        {
            // There's no way to call "base.Execute()" here
            ((ITest)this).Execute();
        }
    }

    class Program
    {
        static void Main(string[] args)
        {
            Descendant d = new Descendant();
            d.Test();
        }
    }
}
Lasse V. Karlsen
thanks Lasse V. Karlsen. how can i meet you : my msn : [email protected] please add me:)
Phsika
the problem with a potential override method from the base class in ((IAlan)this).Alan can be avoid with ((IAlan)base).Alan :)
Rune FS
No, you'll get this compiler error if you try: "Use of keyword 'base' is not valid in this context". You *did* try your suggestion before posting it I assume?
Lasse V. Karlsen
@Phsika, my MSN address is posted in my profile, but I suggest you keep posting questions on Stack Overflow, unless you have questions regarding this particular answer and the question is related to a specific solution, and thus not interesting to anyone else, but in that case I'd suggest you use my email address (same as the MSN address, still posted in my profile.)
Lasse V. Karlsen