Wrox Programmer Forums
Go Back   Wrox Programmer Forums > .NET > Other .NET > General .NET
General .NET For general discussion of MICROSOFT .NET topics that don't fall within any of the other .NET forum subcategories or .NET language forums.  If your question is specific to a language (C# or Visual Basic) or type of application (Windows Forms or ASP.Net) try an applicable forum category. ** PLEASE BE SPECIFIC WITH YOUR QUESTION ** When posting here, provide details regarding the Microsoft .NET language you are using and/or what type of application (Windows/Web Forms, etc) you are working in, if applicable to the question. This will help others answer the question without having to ask.
Welcome to the p2p.wrox.com Forums.

You are currently viewing the General .NET section of the Wrox Programmer to Programmer discussions. This is a community of software programmers and website developers including Wrox book authors and readers. New member registration was closed in 2019. New posts were shut off and the site was archived into this static format as of October 1, 2020. If you require technical support for a Wrox book please contact http://hub.wiley.com
Old February 28th, 2007, 11:31 AM
Authorized User
Join Date: Aug 2004
Posts: 23
Thanks: 0
Thanked 0 Times in 0 Posts
Default Problem With Implementing an Interface

Hi all,

I have a problem implementing an interface.

I have an object class, an interface class, and a tester class

Object Class:

Public Class MainObject

    Dim intID As Integer = 1
    Dim strName As String = "Brett"

    Public Property ID() As Integer
            Return intID
        End Get
        Set(ByVal value As Integer)
            intID = value
        End Set
    End Property

    Public Property Name() As String
            Return strName
        End Get
        Set(ByVal value As String)
            strName = value
        End Set
    End Property

End Class

Interface Class:

Public Interface InterfaceObject1

    Property ID() As Integer

    Property Name() As String

End Interface

Tester Class:

Public Class Tester

    Public Function GetName() As String
        Dim objMainObject As InterfaceObject1 = New MainObject
        Return objMainObject.Name
    End Function

End Class

When the GetName function is called in the tester class, I get the following error message:

Unable to cast object of type 'MainObject' to type 'InterfaceObject1'.

Can someone please help me, I know something is wrong but don't know what.



Old February 28th, 2007, 11:47 AM
Wrox Author
Join Date: Oct 2005
Posts: 4,104
Thanks: 1
Thanked 64 Times in 64 Posts
Send a message via AIM to dparsons

Hmm, wow. You Interface is pointless in the example you have provided as you don't implement the interface in your MainObject class. (You do implement the properties defined in your interface, however, there isn't an interface enforcing you to implement them)

This is an invalid cast in any case.

What you should be doing is:

Public Class MainObject
Implements InterfaceObject1
End Class

And then:
Dim objMainObject as new MainObject()
return objMainObject.Name

================================================== =========
Read this if you want to know how to get a correct reply for your question:
^^Took that from planoie's profile^^
^^Modified text taken from gbianchi profile^^
================================================== =========
Technical Editor for: Professional Search Engine Optimization with ASP.NET
================================================== =========
Why can't Programmers, program??
================================================== =========

Similar Threads
Thread Thread Starter Forum Replies Last Post
Problem in Implementing ECDH nay_ctg BOOK: Beginning Cryptography with Java 1 August 3rd, 2008 06:28 PM
Problem With RSA Interface Floetic Java GUI 1 March 25th, 2008 06:14 AM
implementing a region in interface mohammadalsayeh .NET Framework 2.0 1 August 27th, 2007 08:28 PM
Implementing The Comparable Interface andyboer Java Basics 4 May 18th, 2007 05:03 AM
Interface IPrincipal Problem nightsun BOOK: ASP.NET Website Programming Problem-Design-Solution 1 December 1st, 2003 06:44 PM

Powered by vBulletin®
Copyright ©2000 - 2020, Jelsoft Enterprises Ltd.
Copyright (c) 2020 John Wiley & Sons, Inc.