Welcome to APLNext Sign in | Join | Help
in
Community Website
Blogs Forums

Problem implementing interfaces

Last post 09-17-2007, 12:09 PM by Chris.McIntosh. 2 replies.
Sort Posts: Previous
  • Problem implementing interfaces

     09-14-2007, 7:03 PM

    I'm trying to implement an interface I created, in a VisualAPL class.  When I instantiate the class directly, the methods and properties work.  When I instantiate the class and cast it to the interface, the methods and properties throw exceptions.

    The code is below.  Any ideas what is wrong?

    The interface, written in C#:

    using System;
    using System.Collections.Generic;
    using System.Text;
    namespace TestInterface3{
      public interface ITest{
        Int32 LeftArg {get; }
        Int32 RightArg {get; }
        Int32 AddArgs(Int32 dummy);
      }
    }

    The class, written in VisualAPL

    #region Using directives

    using System;

    using System.Collections.Generic;

    using System.Text;

    refbyfile @"c:\temp\testinterface3.dll";

    using TestInterface3;

    #endregion

    namespace VisualAPLTest{

    public class VisualClass : ITest{

    // Private variables.

    private Int32 m_leftarg;

    private Int32 m_rightarg;

     

    // The constructor.

    public VisualClass(Int32 left, Int32 right){

     

    // Store the variables.

    m_leftarg ← left

    m_rightarg ← right

    }

     

    // The two properties.

    public property LeftArg {

    get { return m_leftarg; }

    }

     

    public property RightArg {

    get { return m_rightarg; }

    }

     

    // The public method.

    public Int32 AddArgs(Int32 dummy){

    return m_leftarg + m_rightarg;

    }

    }

    }

     

    The calling code that works is:

    VisualAPLTest.VisualClass testClass = new VisualAPLTest.VisualClass(3, 5);

     

    The calling code that fails is:

    TestInterface3.ITest testClass = new VisualAPLTest.VisualClass(3, 5);

     

    I'm getting the error An unhandled exception of type 'System.Exception'

    occurred in VisualAPLTest.dll

     

    Thanks for your help. Chris.

  • Re: Problem implementing interfaces

     09-15-2007, 12:35 PM

    I don't think I understand what you are doing; must look it up!

    It is time this forum allowed file uploads so that problems can be easily reproduced.

    At a guess, I think you are missing some casts.

       // The two properties.

        public property LeftArg {

          get { (int32) return m_leftarg; }

        }

     

        public property RightArg {

          get { return (Int32) m_rightarg; }

        }

     

        // The public method.

        public Int32 AddArgs(Int32 dummy){

          return (Int32) m_leftarg + m_rightarg;

    The changes are on the 2  'get' and the one 'return' lines.

  • Re: Problem implementing interfaces

     09-17-2007, 12:09 PM

    Ajay thanks for this advice.  You were right about the missing casts, but not quite right about where they needed to be.  I hadn't cast my property declarations.  The correct way to do this is:

        // The two properties.
        public property Int32 LeftArg {
          get { return m_leftarg; }
        }

        public property Int32 RightArg {
          get { return m_rightarg; }
        }

    You can leave the casts in the { return } statements, but they are not necessary.

    I've been learning a little about Design Patterns and am trying to use them.  These recommend programming to interfaces rather than implementations (concrete objects - instantiations of classes).

View as RSS news feed in XML
Powered by Community Server, by Telligent Systems