c# question

c# question

am 06.04.2008 21:06:32 von Tem

i have 5 non-static classes
Class A, B, C, D, and E

Class A creates an instance of class B. what would be a way for classes C D
E to pass data into that instance of class B.

Class A
{
B b = new B();
}

Tem

Re: c# question

am 06.04.2008 21:48:29 von Chris Crowther

This is a multi-part message in MIME format.
--------------020107060402000100010003
Content-Type: text/plain; charset=ISO-8859-1; format=flowed
Content-Transfer-Encoding: 8bit

Ten wrote:

> Class A creates an instance of class B. what would be a way for classes
> C D E to pass data into that instance of class B.

You could either make B available via a property on A and call methods
on B directly - if it's OK to have direct access to the object in your
model - or you could add methods to A which manipulate B on behalf of C,
D and E (which is pretty much a façade pattern).

I'd personally go for the façade.

> Tem

--
Chris

--------------020107060402000100010003
Content-Type: text/x-vcard; charset=utf-8;
name="hikari.vcf"
Content-Transfer-Encoding: 7bit
Content-Disposition: attachment;
filename="hikari.vcf"

begin:vcard
fn:Chris Crowther
n:Crowther;Chris
org:J&M Crowther Ltd.
adr:;;23 Longship Way;Maldon;Essex;CM9 6UG;UK
email;internet:chris@jm-crowther.co.uk
title:Developer
tel;work:+44 (0)845 890 0997
tel;home:+44 (0)1621 857 034
tel;cell:+44 (0)7803 168740
x-mozilla-html:FALSE
version:2.1
end:vcard


--------------020107060402000100010003--

Re: c# question

am 06.04.2008 21:49:04 von Marc Vangrieken

Hi Tem,

Since "b" is private inside "A" there's no way for outsiders to send
messages (method calls, property setters, ...) to "b". You can, of
course, use delegation from "A" to "B"; "a" receives a message and
sends a "similar" message to it's private instance of "b".

Other things are possible in certain scenario's; for instance when you
only need to pass certain data to "b" upon construction or
intialization. Is that the case?

Kind regards,
Marc Vangrieken
http://vangrieken.wordpress.com

On 6 apr, 21:06, "Ten" wrote:
> i have 5 non-static classes
> Class A, B, C, D, and E
>
> Class A creates an instance of class B. what would be a way for classes C D
> E to pass data into that instance of class B.
>
> Class A
> {
> B b = new B();
>
> }
>
> Tem

Re: c# question

am 06.04.2008 22:17:19 von Marc Vangrieken

On 6 apr, 21:48, Chris Crowther wrote:
> Ten wrote:
> > Class A creates an instance of class B. what would be a way for classes
> > C D E to pass data into that instance of class B.
>
> You could either make B available via a property on A and call methods
> on B directly - if it's OK to have direct access to the object in your
> model - or you could add methods to A which manipulate B on behalf of C,
> D and E (which is pretty much a fa=E7ade pattern).
>
> I'd personally go for the fa=E7ade.
>
> > Tem
>
> --
> Chris
>
> hikari.vcf
> 1KDownloaden


Personally, I wouldn't call it a "Fa=E7ade" nor "Proxy" (having this
little information). "Fa=E7ade", "Proxy" and "Adapter" are all
applications of "Wrapper" with a different intent. None of these
intents are hinted in the problem statement. The commonality between
the proposed solution, "Fa=E7ade" and "Proxy" is the usage of
"delegation".

Of course, as a mental model "Fa=E7ade" is fine...

Kind regards,
Marc Vangrieken
http://vangrieken.wordpress.com

Re: c# question

am 06.04.2008 22:28:48 von Tem

i need to pass data to b not just on initialization.

do you know where i can find an exmaple of the first method you mentioned?
delegation

Thanks
Tem

"Marc Vangrieken" wrote in message
news:96f39104-1794-40b3-b7c0-06baaacee0ce@g1g2000pra.googleg roups.com...
> Hi Tem,
>
> Since "b" is private inside "A" there's no way for outsiders to send
> messages (method calls, property setters, ...) to "b". You can, of
> course, use delegation from "A" to "B"; "a" receives a message and
> sends a "similar" message to it's private instance of "b".
>
> Other things are possible in certain scenario's; for instance when you
> only need to pass certain data to "b" upon construction or
> intialization. Is that the case?
>
> Kind regards,
> Marc Vangrieken
> http://vangrieken.wordpress.com
>
> On 6 apr, 21:06, "Ten" wrote:
>> i have 5 non-static classes
>> Class A, B, C, D, and E
>>
>> Class A creates an instance of class B. what would be a way for classes C
>> D
>> E to pass data into that instance of class B.
>>
>> Class A
>> {
>> B b = new B();
>>
>> }
>>
>> Tem
>

Re: c# question

am 06.04.2008 23:27:58 von Jack Jackson

On Sun, 6 Apr 2008 12:06:32 -0700, "Ten" wrote:

>i have 5 non-static classes
>Class A, B, C, D, and E
>
>Class A creates an instance of class B. what would be a way for classes C D
>E to pass data into that instance of class B.
>
>Class A
>{
>B b = new B();
>}
>
>Tem

Two of the ways to do this are:

1. Make a public readonly property of A that returns b. Then the
callers would say:

a.B.SomePropertyOfB = 5

Users of A can access any methods/properties of B.

2. Create properties/methods of A that then call the appropriate
properties and methods of B. In this case the properties/methods of A
may or may not be exactly the same as those of B. You would probably
only use this technique if you wanted to expose only a small subset of
B's interface to the users of A. You are hiding the existence of a B,
and in the future would be free to use a class other than B without
the callers having to change their code as long as it offered the same
functionality that you expose.

Re: c# question

am 07.04.2008 00:58:18 von Peter Duniho

On Sun, 06 Apr 2008 13:17:19 -0700, Marc Vangrieken
wrote:

> Personally, I wouldn't call it a "Façade" nor "Proxy" (having this
> little information). "Façade", "Proxy" and "Adapter" are all
> applications of "Wrapper" with a different intent. None of these
> intents are hinted in the problem statement.

Frankly, not much of anything at all is hinted at in the problem
statement. I think it's perfectly reasonable to describe briefly
solutions appropriate to different problem statements, given that the OP
has provided so little to work with. You have no reason to believe that
he is NOT looking for any of those designs.

Pete

RE: c# question

am 07.04.2008 01:55:01 von pbromberg

The gist of the thread - simplest approach - might be:

Class A
{
public B b;

A( B b)
{
this.b = b;
}
}

You pass an instance of B into the ctor for A.
Now "B" is a public field of A. The code is ugly, but I hope you get the idea.
-- Peter
Site: http://www.eggheadcafe.com
UnBlog: http://petesbloggerama.blogspot.com
Short Urls & more: http://ittyurl.net


"Ten" wrote:

> i have 5 non-static classes
> Class A, B, C, D, and E
>
> Class A creates an instance of class B. what would be a way for classes C D
> E to pass data into that instance of class B.
>
> Class A
> {
> B b = new B();
> }
>
> Tem
>
>

Re: c# question

am 07.04.2008 12:29:58 von Marc Vangrieken

On 6 apr, 22:28, "Tem" wrote:
> i need to pass data to b not just on initialization.
>
> do you know where i can find an exmaple of the first method you mentioned?
> delegation
>
> Thanks
> Tem
>
> "Marc Vangrieken" wrote in message
>
> news:96f39104-1794-40b3-b7c0-06baaacee0ce@g1g2000pra.googleg roups.com...
>
> > Hi Tem,
>
> > Since "b" is private inside "A" there's no way for outsiders to send
> > messages (method calls, property setters, ...) to "b". You can, of
> > course, use delegation from "A" to "B"; "a" receives a message and
> > sends a "similar" message to it's private instance of "b".
>
> > Other things are possible in certain scenario's; for instance when you
> > only need to pass certain data to "b" upon construction or
> > intialization. Is that the case?
>
> > Kind regards,
> > Marc Vangrieken
> >http://vangrieken.wordpress.com
>
> > On 6 apr, 21:06, "Ten" wrote:
> >> i have 5 non-static classes
> >> Class A, B, C, D, and E
>
> >> Class A creates an instance of class B. what would be a way for classes C
> >> D
> >> E to pass data into that instance of class B.
>
> >> Class A
> >> {
> >> B b = new B();
>
> >> }
>
> >> Tem

You can find an example of delegation here:
http://en.wikipedia.org/wiki/Delegation_(programming). Also usefull:
Explanation of composition and aggregation:
http://en.wikipedia.org/wiki/Aggregation_%28object-oriented_ programming%29#Aggregation.
The examples are in C++, but with a little imagination it can be C#.

Kind regards,
Marc Vangrieken
http://vangrieken.wordpress.com

Re: c# question

am 07.04.2008 12:37:34 von Marc Vangrieken

>I think it's perfectly reasonable to describe briefly
> solutions appropriate to different problem statements, given that the OP
> has provided so little to work with.

I agree, but only when the solutions offered are placed into a correct
context. But, anyway, that's not what this post is about...

Kind regards,
Marc Vangrieken
http://vangrieken.wordpress.com