473,503 Members | 1,739 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Instancing a class where base object already exists

What I am hoping to do is make a constructor for a derived class that
can accept an instance of the base class. Maybe this just is not possible?

I am working with the new SSLStream in the 2005 beta. They came up with
what seems to be an awkward object model to me. The SSLStream has nearly
the same member signature as a NetworkStream. Both are derived from
Stream. But in both cases you need a TCPClient instance to get them
going. This leaves you constantly checking to see if the socket has been
negotiated for SSL, and if it has you must do I/O on the SSLStream and
if not on the "Client" property of the TCPClient.

Other implementors have chosen to give you a single socket type object
that can be used for either clear text or SSL encrypted communications.
This is MUCH handier. For .NET I sure wish they would have made
TCPClient upgradable, and as such the Client property would give you an
upgraded NetworkStream. I am creating software that has to be able to
upgrade and downgrade the socket on the fly.

My thought was to derive a class from TCPClient with a property called
"NetStream" that would either return the client property of TCPClient if
SSL has not yet been negotiated, or the SSLStream object if it has, but
in both cases cast as a Stream. The complication is that when listening
for connections the framework instances the TCPClient for you in your
call to Accept, so you already have the instance for the base for this
new derived class. What would be cool is if there were a way to make an
overloaded constructor in my derived class that would accept the
instance of the base class.

I know there are some new object capabilities in 2005, but haven't found
a way to do this.

Can you teach an old dog a new trick?
Nov 21 '05 #1
4 1338
In article <eF**************@TK2MSFTNGP10.phx.gbl>, Lee Gillie wrote:
What I am hoping to do is make a constructor for a derived class that
can accept an instance of the base class. Maybe this just is not possible?

I am working with the new SSLStream in the 2005 beta. They came up with
what seems to be an awkward object model to me. The SSLStream has nearly
the same member signature as a NetworkStream. Both are derived from
Stream. But in both cases you need a TCPClient instance to get them
going. This leaves you constantly checking to see if the socket has been
negotiated for SSL, and if it has you must do I/O on the SSLStream and
if not on the "Client" property of the TCPClient.

Other implementors have chosen to give you a single socket type object
that can be used for either clear text or SSL encrypted communications.
This is MUCH handier. For .NET I sure wish they would have made
TCPClient upgradable, and as such the Client property would give you an
upgraded NetworkStream. I am creating software that has to be able to
upgrade and downgrade the socket on the fly.

My thought was to derive a class from TCPClient with a property called
"NetStream" that would either return the client property of TCPClient if
SSL has not yet been negotiated, or the SSLStream object if it has, but
in both cases cast as a Stream. The complication is that when listening
for connections the framework instances the TCPClient for you in your
call to Accept, so you already have the instance for the base for this
new derived class. What would be cool is if there were a way to make an
overloaded constructor in my derived class that would accept the
instance of the base class.

I know there are some new object capabilities in 2005, but haven't found
a way to do this.

Can you teach an old dog a new trick?


Public Class MyDerivedClass
Inherits TCPClient

Private instance As TCPClient

' insert other constructors here

Public Sub New (ByVal tcpInstance As TCPClient)
Me.instance = tcpInstance

' more cool stuff...
End Sub

End Class
Does this help?
--
Tom Shelton [MVP]
Nov 21 '05 #2
Tom Shelton wrote:
In article <eF**************@TK2MSFTNGP10.phx.gbl>, Lee Gillie wrote:
What I am hoping to do is make a constructor for a derived class that
can accept an instance of the base class. Maybe this just is not possible?

I am working with the new SSLStream in the 2005 beta. They came up with
what seems to be an awkward object model to me. The SSLStream has nearly
the same member signature as a NetworkStream. Both are derived from
Stream. But in both cases you need a TCPClient instance to get them
going. This leaves you constantly checking to see if the socket has been
negotiated for SSL, and if it has you must do I/O on the SSLStream and
if not on the "Client" property of the TCPClient.

Other implementors have chosen to give you a single socket type object
that can be used for either clear text or SSL encrypted communications.
This is MUCH handier. For .NET I sure wish they would have made
TCPClient upgradable, and as such the Client property would give you an
upgraded NetworkStream. I am creating software that has to be able to
upgrade and downgrade the socket on the fly.

My thought was to derive a class from TCPClient with a property called
"NetStream" that would either return the client property of TCPClient if
SSL has not yet been negotiated, or the SSLStream object if it has, but
in both cases cast as a Stream. The complication is that when listening
for connections the framework instances the TCPClient for you in your
call to Accept, so you already have the instance for the base for this
new derived class. What would be cool is if there were a way to make an
overloaded constructor in my derived class that would accept the
instance of the base class.

I know there are some new object capabilities in 2005, but haven't found
a way to do this.

Can you teach an old dog a new trick?

Public Class MyDerivedClass
Inherits TCPClient

Private instance As TCPClient

' insert other constructors here

Public Sub New (ByVal tcpInstance As TCPClient)
Me.instance = tcpInstance

' more cool stuff...
End Sub

End Class
Does this help?


Ummmm, well... the way I understand it, with your suggestion there are
now TWO instances of TCPClient. One in the base of any instance of
MyDerivedClass. Plus also the private member called "instance".

I guess I hear you saying to overload every possible method of TCPClient
so that I may redirect the calls to instance.methodxxx? I can see that
working.

But if I could really set the instance of the base of MyDerivedClass,
then there would only be one instance of TCPClient. Even if there were
momentarily two instances of TCPClient... the much bigger issue for me,
would be that it would not be necessary to override all public members
of TCPClient.

If I could set the instance of the base of MyDerivedClass, then when
TCPClient changes, gets new members, they are automatically revealed via
the MyDerivedClass interface. I create a new maintenance issue for
myself otherwise.

I don't see what kind of canons of OOP would be violated by allow this.

- Lee
Nov 21 '05 #3

Why inherit from TCPClient if we are going to encapsulate as well?

By inheriting, MyDerivedClass offers all the TCPClient interface to the
user but when called they will act on the 'base TCPClient' instead of
the encapsulated instance.

Unless we are planning to shadow all of the TCPClient interface in the
MyDerivedClass and create new versions acting upon instance

e..g

public shadows Function GetStream() as
System.Net.Sockets.NetworkStream
return instance.GetStream()
end function

then I don't see how 'instance' will ever get used.

What am I missing?
I am rather limited in my comments as I don't have '05 and can't try
out the SSLStream but a possible solution (though not inheriting from
TcpClient - so it cannot be used polymorphically with client, which may
be an issue) is...

imports System.Net
imports System.Net.Sockets

' Implements secure and non-secure stream
Class MyChannel

private _client as TcpClient
private _stream as NetworkStream

' ctors can be implemented in multiple ways - pass in a tcpclient or
pass in the items needed to instantiate the tcpclient. For ease of
coding, just pass in tcpclient

public sub new ( client as TcpClient)
_client = client
_stream = client.GetStream()
end sub

' secure the stream. No idea what needs to be passed in - really
should get '05 at some point.
Public sub SecureStream( <whatever params are needed to set up the
secure stream> )

_stream = new SSLStream(_stream, <whatever params>)

end sub

' returns the standard ClientStream is not secure or else the
SSLStream
Public readonly property Stream() as NetworkStream
Get
_return _stream
End Get
End Property

End class
hth,
Alan.

Nov 21 '05 #4
al*******@users.com wrote:
Why inherit from TCPClient if we are going to encapsulate as well?

By inheriting, MyDerivedClass offers all the TCPClient interface to the
user but when called they will act on the 'base TCPClient' instead of
the encapsulated instance.

Unless we are planning to shadow all of the TCPClient interface in the
MyDerivedClass and create new versions acting upon instance

e..g

public shadows Function GetStream() as
System.Net.Sockets.NetworkStream
return instance.GetStream()
end function

then I don't see how 'instance' will ever get used.

What am I missing?
I am rather limited in my comments as I don't have '05 and can't try
out the SSLStream but a possible solution (though not inheriting from
TcpClient - so it cannot be used polymorphically with client, which may
be an issue) is...

imports System.Net
imports System.Net.Sockets

' Implements secure and non-secure stream
Class MyChannel

private _client as TcpClient
private _stream as NetworkStream

' ctors can be implemented in multiple ways - pass in a tcpclient or
pass in the items needed to instantiate the tcpclient. For ease of
coding, just pass in tcpclient

public sub new ( client as TcpClient)
_client = client
_stream = client.GetStream()
end sub

' secure the stream. No idea what needs to be passed in - really
should get '05 at some point.
Public sub SecureStream( <whatever params are needed to set up the
secure stream> )

_stream = new SSLStream(_stream, <whatever params>)

end sub

' returns the standard ClientStream is not secure or else the
SSLStream
Public readonly property Stream() as NetworkStream
Get
_return _stream
End Get
End Property

End class
hth,
Alan.


This works. Or nearly. The only common base of NetworkStream and
SSLStream is Stream, a base of both NetworkStream and SSLStream. So
_stream would be a "Stream". Where CommandChannel is an instance of your
MyChannel then one would do I/O with it in a singular way, regardless of
whether the stream is SSL or not...

CommandChannel.Stream.BeginRead( ...

Had we been able to discover a way to set the base class instance, with
inheritance, this would instead be something like:

CommandChannel.BeginRead( ...

But at least your way we could avoid this scenario:

If mSecured Then
SSLChannel.BeginRead(...
Else
TCPClient.GetStream.BeginRead(...
End If

Of course the main point is to be able to program the I/O handling blind
to the fact that SSL is engaged or not. What you propose, instead of
inheritance, accomplishes that.

In any case, can you see how truly handy it would be if there were an
OOP primitive to construct a derived class with the base class instance
already existing, and to be able to specify it to the constructor? I
think to answer my original question then, we would have to say there IS
NO WAY OF DOING IT. But there are other effective approaches to making
the I/O model blind to being secure.

Thanks for all the input.
Best regards - Lee
Nov 21 '05 #5

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

Similar topics

10
1635
by: lkrubner | last post by:
I killed last night and a good chunk of today trying to figure out this one particular attempt to get a class and initialize it. My code is using a class method called getObject to include() a file...
3
1726
by: Brad Navarro | last post by:
OK, I may be asking for the impossible, but I'll give it a shot: For a case like this: using System.Data.SqlClient; abstract class Base { private SqlConnection DBConnect; Base(string...
1
8291
by: Mark McDonald | last post by:
This question kind of follows on from Mike Spass’ posting 10/11/2004; I don’t understand why you can’t declare an implicit operator to convert a base class to a derived class. The text...
5
3140
by: Andy | last post by:
Hi all, I have a site with the following architecture: Common.Web.dll - Contains a CommonPageBase class which inherits System.Web.UI.Page myadd.dll - Contains PageBase which inherits...
2
2065
by: David Buchan | last post by:
I have written a program to write some text to a file. Currently, if the file already exists, the program simply overwrites it. What I'd like to do, is have the program ask me if I wish to...
0
3692
by: ruju00 | last post by:
I am getting an error in Login() method of the following class FtpConnection public class FtpConnection { public class FtpException : Exception { public FtpException(string message) :...
5
2604
by: Dennis Jones | last post by:
Hello, I have a couple of classes that look something like this: class RecordBase { }; class RecordDerived : public RecordBase {
26
5333
by: nyathancha | last post by:
Hi, How Do I create an instance of a derived class from an instance of a base class, essentially wrapping up an existing base class with some additional functionality. The reason I need this is...
20
4002
by: tshad | last post by:
Using VS 2003, I am trying to take a class that I created to create new variable types to handle nulls and track changes to standard variable types. This is for use with database variables. This...
0
7203
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
7334
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
5579
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
5014
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
4675
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
3156
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
0
1514
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated ...
1
737
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
0
383
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence...

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.