473,657 Members | 2,546 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 1347
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.method xxx? 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.Sock ets.NetworkStre am
return instance.GetStr eam()
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.Sock ets

' 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.GetStrea m()
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(_stre am, <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.Sock ets.NetworkStre am
return instance.GetStr eam()
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.Sock ets

' 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.GetStrea m()
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(_stre am, <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.BeginRea d( ...

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.Begi nRead(...
Else
TCPClient.GetSt ream.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
1653
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 and then initialize the class in that file (one class per file, each file has the same name as the class). This code has been working fine for 8 months, and now is dying on this one line. Oddly, it is dying on the last return of this method. ...
3
1736
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 SqlConnectStr) { DBConnect = new SqlConnection(SqlConnectStr);
1
8328
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 books say “neither the source nor the target types of a conversion can be a base type of the other, since a conversion would then already exist”. But this is not really true, whilst automatic (implicit) conversions do occur from the derived...
5
3153
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 CommonPageBase - Contains myPage which inherits PageBase Each of these classes overrides OnInit and ties an event handler
2
2072
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 overwrite the existing file, ask for a new name if the answer is no, or overwrite if the answer is yes. I have the line: Dim objWriter As StreamWriter = New StreamWriter(OutputFileName)
0
3707
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) : base(message){} public FtpException(string message, Exception innerException) : base(message,innerException){}
5
2618
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
5354
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 because I am not always able to control/create all the different constructors the base class has. My problem can be described in code as follows ... /* This is the base class with a whole heap of constructors/functionality*/ public class Animal
20
4030
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 tells me if a variable has changed, give me the original and current value, and whether the current value and original value is/was null or not. This one works fine but is recreating the same methods over and over for each variable type. ...
0
8425
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, well explore What is ONU, What Is Router, ONU & Routers main usage, and What is the difference between ONU and Router. Lets take a closer look ! Part I. Meaning of...
0
8326
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
8845
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
8743
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
8522
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
1
6177
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 presenter, Adolph Dupr who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
5647
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 then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
4333
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
2
1736
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 can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.