473,471 Members | 4,095 Online
Bytes | Software Development & Data Engineering Community
Create Post

Home Posts Topics Members FAQ

Wrapper class

This may be a trivial question, but it's stumping me.

public class A
{
}

public class B : A
{
B(A a)
{
// I want this class to reference 'a' and not copy it.
}
}

The idea is that you provide an instance of the base class 'A' and wrap
class 'B' around the same instance to provide extra functionality. I want to
specialise class 'A' into class 'B'. How do I do this?

Dave
May 25 '06 #1
14 13498
the wrapper does this:

From: Dave Moran - view profile
Date: Thurs, May 25 2006 6:09 pm
Email: Dave Moran <dmo...@nospam.nospam>
Groups: microsoft.public.dotnet.languages.csharp
Not yet ratedRating:
show options
Reply | Reply to Author | Forward | Print | Individual Message | Show
original | Report Abuse | Find messages by this author
This may be a trivial question, but it's stumping me.

public class A
{
public A(stirng ConnectionString) {...}
}
public class B
{
private A internalA;
public B(stirng ConnectionString)
{
internalA = new A(ConnectionString);
}
}

otherwise you do not wrap A. you subclass it

hope this helps
Galin Iliev[MCSD.NET]
www.galcho.com

May 25 '06 #2
In this case B isn't an A; it just behaves like one,

One solution here would be an interface IA (i.e. "I" and your original class
name) which defines the key properties etc; B then implements IA (B : IA),
and forwards / specialises the methods to the instance of A that it holds.

Marc
May 25 '06 #3
Thanks for the speedy reply. I think I'm trying to subclass A rather than
wrap it. I want to reference the original, not create a new instance of A.

What needs to happen in the constructor of B if want it to simply reference
A (i.e. subclass)? I also need to pass in an instance of A into the
constructor, not the data for A.

Dave

public class A
{
public A(string ConnectionString) {...}
}

public class B : A
{
public B(A a)
{
internalA = new A(ConnectionString);
}
}

"Galcho[MCSD.NET]" wrote:
the wrapper does this:

From: Dave Moran - view profile
Date: Thurs, May 25 2006 6:09 pm
Email: Dave Moran <dmo...@nospam.nospam>
Groups: microsoft.public.dotnet.languages.csharp
Not yet ratedRating:
show options
Reply | Reply to Author | Forward | Print | Individual Message | Show
original | Report Abuse | Find messages by this author
This may be a trivial question, but it's stumping me.

public class A
{
public A(stirng ConnectionString) {...}
}
public class B
{
private A internalA;
public B(stirng ConnectionString)
{
internalA = new A(ConnectionString);
}
}

otherwise you do not wrap A. you subclass it

hope this helps
Galin Iliev[MCSD.NET]
www.galcho.com

May 25 '06 #4
Dave,

You can't do this. The reason is that A doesn't have the information
that is needed to make it into B. If that was the case, then you would be
able to do a direct cast.

It just doesn't make sense. It's like saying all apples are red,
therefore, all red things are apples. Apples are a specific subclass of red
things, the reverse is not true.

Applying that here, you are saying "all instances of B are instances of
A, therefore, all instances of A are instances of B", which obviously, can
not be.

You have no choice but to copy the values. You can not get that
instance of A to be treated like B.

Hope this helps.
--
- Nicholas Paldino [.NET/C# MVP]
- mv*@spam.guard.caspershouse.com

"Dave Moran" <dm****@nospam.nospam> wrote in message
news:58**********************************@microsof t.com...
Thanks for the speedy reply. I think I'm trying to subclass A rather than
wrap it. I want to reference the original, not create a new instance of A.

What needs to happen in the constructor of B if want it to simply
reference
A (i.e. subclass)? I also need to pass in an instance of A into the
constructor, not the data for A.

Dave

public class A
{
public A(string ConnectionString) {...}
}

public class B : A
{
public B(A a)
{
internalA = new A(ConnectionString);
}
}

"Galcho[MCSD.NET]" wrote:
the wrapper does this:

From: Dave Moran - view profile
Date: Thurs, May 25 2006 6:09 pm
Email: Dave Moran <dmo...@nospam.nospam>
Groups: microsoft.public.dotnet.languages.csharp
Not yet ratedRating:
show options
Reply | Reply to Author | Forward | Print | Individual Message | Show
original | Report Abuse | Find messages by this author
This may be a trivial question, but it's stumping me.

public class A
{
public A(stirng ConnectionString) {...}
}
public class B
{
private A internalA;
public B(stirng ConnectionString)
{
internalA = new A(ConnectionString);
}
}

otherwise you do not wrap A. you subclass it

hope this helps
Galin Iliev[MCSD.NET]
www.galcho.com

May 25 '06 #5
There's no such thing as trivial!

I didn't think I was assuming all instances of A were instances of B. I was
only assuming that all instances of B were instances of A (i.e. derived from
A).

What I was trying to do was create a set of helper functions that operate on
an instance of 'A' that I know is a particular type of object. I don't want
to copy any data. I just want to operate on the data using specialised
functions. What approach would you suggest?

Dave

May 25 '06 #6
Well, given that spec, there is no need for B to even pretend to be an A or
an IA; it's just an object whose ctor accepts an A, and exposes some
methods...?

If you really mean to be able to use an instance of B as an instance of A,
then you can't do this with an existing A; inheritance means "is an", not
"has an"; "has an" is encapsulation, as shown below. When B : A, then the
"A" part of that *is intrisically fixed in the B* - you can't change it.
Encapsulation with interfaces is a route out of this, as this is then "looks
like" - so B : IA and A : IA - anything that accepts an IA can have either
an A or a B, and if you /know/ you have a B then you can use the extra
functions. This was in my previous post.

Encapsulation example (with neither inheritance nor interfaces):

public class A {}

public class B {
private readonly A _a;
public B (A a) {
_a = a;
}
public void DoSomething() {
a.DoSomethingElse();
a.DoSomethingOther();
}
}

May 25 '06 #7
Thanks for the reply. Your suggestion doesn't sound quite right for what I
need. The derived class was designed to provide additional functionality to
that which exists in the base class. Implementing an interface just means
that I have to foward the methods to the base class, a lot of work for no
gain?

Perhaps I'm the problem the wrong way round. I need to operate on certain
instances of class A that I know have specialised characteristics. I don't
want to copy the data from A, I just want to operate on it.

Dave
May 25 '06 #8
Hi

Encapsulation does look to be the right thing to do here. I'm assuming that
the assignment simply increments the 'reference count' on the instance of A
and allows me to reference the data directly. Removing the readonly keyword
would also allow me to change that data, if necessary?

Thanks for everybody's suggestions.

Dave
May 25 '06 #9
this seems like encapsulation would be the best bet .. but in more general
situations you should probably have a look at the proxy/decorator patterns
(both instance and non-instance) which also support polymorhism.

example:

proxy ...

public class Foo {
public virtual void Bar() {
//some code
}
}

public class FooProxy : Foo{
private readonly Foo m_Foo;
public override void Bar() {
//add some code here
m_Foo.Bar();
//or add some code here
}
private FooProxy(Foo _Foo) {
m_Foo = Foo;
}
}

the instance proxy being ..

public class FooProxy : Foo{
public override void Bar() {
//add some code here
base.Bar();
//or add some code here
}
}

These patterns are generally used when you are changing the behaviors of
methods (as opposed to defining helper methods as you added)

Cheers,

Greg Young
MVP - C#
"Marc Gravell" <ma**********@gmail.com> wrote in message
news:%2****************@TK2MSFTNGP03.phx.gbl...
In this case B isn't an A; it just behaves like one,

One solution here would be an interface IA (i.e. "I" and your original
class name) which defines the key properties etc; B then implements IA (B
: IA), and forwards / specialises the methods to the instance of A that it
holds.

Marc

May 25 '06 #10
other way for defining helper methods /additional ones / is to use
partial classes
http://www.devx.com/dotnet/Article/22603

example

public class A
{
public void Method1(){...}
}

///and you extend it
public partial A
{
// add additional mehod
public void Method2(){...}

}

I hope this helps
Galin Iliev[MCSD.NET]
www.galcho.com

May 25 '06 #11
The readonly keyword indicates that you can't change which A the
variable _a points to after it's set in the constructor. It doesn't
make any claims about whether you can change the state of the A
referenced by _a; you can still do that.

Just for your information, .NET doesn't use reference counting, it uses
mark-and-sweep garbage collection. The effect is the same, though:
referencing an existing object prevents it from being collected.

May 25 '06 #12
Basically, it can't be done. Note, I'm not saying the C# won't let you.
It CANNOT be done.(sort of a law of phyisics thing)

What you would like to do is:
A a = new A();
B b = new B(a);

Which would mean that both a & b, which different types, nevertheless
occupy the same space. Consider what would happen if we did this:
a = null;

Now, the A object which was held by a can now be garage collected, but
b *is* that object. Is b collected as well?

May 25 '06 #13
Agree with everything Bruce says.

Just so add: I made _a "readonly" as this feels natural (to me, at least)
for a class that is meant to wrap an instance of something. If I need to be
able to change the "a" in question, then yes: we could ditch the "readonly"
and add some accessors, but then I would also start asking questions about
thread safety - do we have to sync all access to _a, for instance in case
another thread swaps it mid-way through an operation on b...

Whenever writing a wrapper or facade, my instinct is to make the underlying
object readonly until I *know* I have a (genuine) need to do otherwise...
for instance, another alternative here would simply be to create a new
instance of B around the new A... which is more appropriate depends on the
usage.

Marc
May 26 '06 #14
"Dave Moran" wrote...
Thanks for the speedy reply. I think I'm trying to
subclass A rather than wrap it. I want to reference
the original, not create a new instance of A.
Well, it's not possible to subclass instances, just classes...
What needs to happen in the constructor of B if want
it to simply reference A (i.e. subclass)? I also need
to pass in an instance of A into the
constructor, not the data for A.

Dave

public class A
{
public A(string ConnectionString) {...}
}

public class B : A
{
public B(A a)
{
internalA = new A(ConnectionString);
}
}


What you seem to be looking for is the "Decorator" pattern, extending an
object rather than a class. There are several articles on how to achieve
this, e.g.:

http://www.codeproject.com/csharp/csdespat_2.asp

The "best" way to achieve this, is to use the interfaces from the class you
want to decorate, and simply forward all calls to the instance, with or
without other enhancements.

interface IA
{
void InstanceMethod();
}

public class A
{
public A(string ConnectionString) {...}

public void InstanceMethod() {...}
}

public class B : IA
{
private A internalA = null;

public B(A a)
{
internalA = a;
// Do not use new, simply use the reference...
}

public void InstanceMethod()
{
a.InstanceMethod();
}
public void NewMethod()
{
// Do something else with a;
}
}
--------------------------------------------

You *could* make the B class a subclass of A, and try to ignore the
inherited fields, etc, but that will not function very well, unless all
methods and properties in A are declared "virtual" so you can *override*
them.

// Bjorn A
May 29 '06 #15

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

Similar topics

4
by: Christian Christmann | last post by:
Hi, I need an STL list and was thinking of putting the list in wrapper class. The reason for my decision is that you can much better perform consistency checks. For instance, I need a...
1
by: sureshjram | last post by:
Hi all , What is a Wrapper Class in c#?? I am new to this OOPS plz explain it thanks in advance regards Suresh
2
by: John Mullin | last post by:
We are having a problem which appears similar to a previous posting: http://groups.google.com/groups?hl=en&lr=&frame=right&th=d97f552e10f8c94c&seekm=OZw33z9EDHA.2312%40TK2MSFTNGP10.phx.gbl#link1 ...
1
by: Samuel R. Neff | last post by:
We're using XmlSerializer to serialize our data objects for persistence in a WinForms app. This is our primary data store, we're not web connected and are not using a database. We've had to...
2
by: source_acct | last post by:
Can someone explain the function of a wrapper class and maybe an example? Thanks.
0
by: IndulgentCoder | last post by:
Hi all, I'm new to scripts forum. I've a .NET wrapper class written in c# that makes some calls to native dlls using platform invocation. The native dlls inturn perform read operation on MSOffice...
2
by: Christian Chrismann | last post by:
Hi, an application uses a wrapper class for an STL list. Basically, this template class provides the same functions (delete, append, find...) as an ordinary STL list. Additionally, there are...
12
by: joshuapking | last post by:
I'm having a very difficult time coming across an appropriate solution for this seemingly simple problem. I've written a managed wrapper class for some legacy C++ routines. One routine generates...
2
by: MyCrystalGift | last post by:
Hi, I have an old C++ GUI Application CPPAPP.exe that calls a C DLL library RULE.DLL through a C++ class wrapper LoadRule.CPP. Now I need to call the C DLL RULE.DLL from C# GUI application...
5
by: GCRhoads | last post by:
I have some templated functions and I want to write a call wrapper class for it. I also want to pass the function object from this class to some functions that will then call the function...
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
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...
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...
1
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...
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: 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 ...
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.