473,473 Members | 1,469 Online
Bytes | Software Development & Data Engineering Community
Create Post

Home Posts Topics Members FAQ

C# - Interface Class

Hi,

I'm new to C# and are having difficultly implementing an interface class.
Basically I want to define an interface class, then inherit it. I want some
of the methods to be public, and some not. Below is an example of what I'm
trying to do;

using System;

public interface iTCPInterface : IDisposable

{

void Connected();

void Close();

string DataArrival();

}

public class clsAttempt1 : iTCPInterface

{

public void Connected()

{}

public void Close()

{}

public string DataArrival()

{return "boo";}

public void Dispose()

{}

}

The above will compile without any issues. However, I don't want anything
being able to call public void close when I create an instance of
clsAttempt1. Now I would have thought that by saying "Private void Close()"
I would achieve this. Alas, here is my problem. When I do this I get a
compile error:

'clsAttempt1' does not implement interface member 'iTCPInterface.Close()'.
'clsAttempt1.Close()' is either static, not public, or has the wrong return
type.

Where have I gone wrong? I don't want Close accessible outside of the class
e.g. myObject.Close.

Thanks in advance.
Nov 16 '05 #1
3 2488
Where have I gone wrong? I don't want Close accessible outside of the class
e.g. myObject.Close.


Then you shouldn't implement an interface with such a method.

You can implement the method privately using explicit implementation

void ITCPInterface.Close() {... }

but it will still be callable from the outside through a ITCPInterface
reference.

Mattias

--
Mattias Sjögren [MVP] mattias @ mvps.org
http://www.msjogren.net/dotnet/ | http://www.dotnetinterop.com
Please reply only to the newsgroup.
Nov 16 '05 #2
Hi Richard,

Here's the trick:

public class clsAttempt1 : iTCPInterface
{
void iTCPInterface.Connected()
{}
void iTCPInterface.Close()
{}
}

This way, the Connected and Close methods will be accessible only through
the interface but not the implementing class itself.

--
Sincerely,
Dmitriy Lapshin [C# / .NET MVP]
Bring the power of unit testing to the VS .NET IDE today!
http://www.x-unity.net/teststudio.aspx

"Richard" <no**@nope.com> wrote in message
news:AI********************@pipex.net...
Hi,

I'm new to C# and are having difficultly implementing an interface class.
Basically I want to define an interface class, then inherit it. I want
some
of the methods to be public, and some not. Below is an example of what
I'm
trying to do;

using System;

public interface iTCPInterface : IDisposable

{

void Connected();

void Close();

string DataArrival();

}

public class clsAttempt1 : iTCPInterface

{

public void Connected()

{}

public void Close()

{}

public string DataArrival()

{return "boo";}

public void Dispose()

{}

}

The above will compile without any issues. However, I don't want anything
being able to call public void close when I create an instance of
clsAttempt1. Now I would have thought that by saying "Private void
Close()"
I would achieve this. Alas, here is my problem. When I do this I get a
compile error:

'clsAttempt1' does not implement interface member 'iTCPInterface.Close()'.
'clsAttempt1.Close()' is either static, not public, or has the wrong
return
type.

Where have I gone wrong? I don't want Close accessible outside of the
class
e.g. myObject.Close.

Thanks in advance.


Nov 16 '05 #3
Use an abstract class instead.

"Richard" <no**@nope.com> wrote in message
news:AI********************@pipex.net...
Hi,

I'm new to C# and are having difficultly implementing an interface class.
Basically I want to define an interface class, then inherit it. I want
some
of the methods to be public, and some not. Below is an example of what
I'm
trying to do;

using System;

public interface iTCPInterface : IDisposable

{

void Connected();

void Close();

string DataArrival();

}

public class clsAttempt1 : iTCPInterface

{

public void Connected()

{}

public void Close()

{}

public string DataArrival()

{return "boo";}

public void Dispose()

{}

}

The above will compile without any issues. However, I don't want anything
being able to call public void close when I create an instance of
clsAttempt1. Now I would have thought that by saying "Private void
Close()"
I would achieve this. Alas, here is my problem. When I do this I get a
compile error:

'clsAttempt1' does not implement interface member 'iTCPInterface.Close()'.
'clsAttempt1.Close()' is either static, not public, or has the wrong
return
type.

Where have I gone wrong? I don't want Close accessible outside of the
class
e.g. myObject.Close.

Thanks in advance.

Nov 16 '05 #4

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

4
by: Roy Pereira | last post by:
I have an application that is composed of a set of "Content" dlls and a viewer application. The viewer calls a standard set of functions that are present in all the dlls. I maintain this by...
9
by: Anon Email | last post by:
Hi people, I'm learning about header files in C++. The following is code from Bartosz Milewski: // Code const int maxStack = 16; class IStack
3
by: Christof Warlich | last post by:
Hi, I'm looking for a more concise way than below to extend an existing interface. Just imagine that class Interface would have 1000 other members and class DerivedInterface only needs to add...
4
by: Doug | last post by:
I am working on an existing .NET (C Sharp) component that had a com interface that was used by a VB component. When it was originally written, it had the SSEAssemblyCom class below - minus the two...
6
by: Alex Sedow | last post by:
Example 1 interface I { string ToString(); } public class C : I { public void f() {
20
by: Ole Hanson | last post by:
I am accessing my database through an interface, to allow future substitution of the physical datastore - hence I would like to declare in my Interface that my DAL-objects implementing the...
4
by: Ray Dukes | last post by:
What I am looking to do is map the implementation of interface properties and functions to an inherited method of the base class. Please see below. ...
5
by: Colin McGuire | last post by:
Hi all, when I write the class below Private Class employee End Class and then add the line "Implements IVF" which is an interface I have written, the IDE modifies my code to display
15
by: Xah Lee | last post by:
On Java's Interface Xah Lee, 20050223 In Java the language, there's this a keyword “interfaceâ€. In a functional language, a function can be specified by its name and parameter specs....
8
by: rn5a | last post by:
Suppose I have the following class code: Imports System Imports System.Data Imports System.Data.SqlClient Public Class DBSettings Private sqlCmd As SqlCommand Private sqlConn As...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
1
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...
0
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...
0
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The...
0
muto222
php
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.