Windows Vista Forums
Vista Forums Home Join Vista Forums Windows 7 Forum Vista Tutorials Tags
Welcome to Windows Vista Forums. Our forum is dedicated to helping you find solutions with any problems, errors or issues you are experiencing with Windows Vista. The Vista forum also covers news and updates and has an extensive Windows Vista tutorial section that covers a wide range of tips and tricks.

Go Back   Vista Forums > Misc Newsgroups > .NET General

Vista - c# question

Reply
 
Old 04-06-2008   #1 (permalink)
Ten


 
 

c# question

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


My System SpecsSystem Spec
Old 04-06-2008   #2 (permalink)
Chris Crowther


 
 

Re: c# question

Ten wrote:
Quote:

> 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.
Quote:

> Tem
--
Chris

My System SpecsSystem Spec
Old 04-06-2008   #3 (permalink)
Marc Vangrieken


 
 

Re: c# question

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" <tem1...@xxxxxx> wrote:
Quote:

> 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
My System SpecsSystem Spec
Old 04-06-2008   #4 (permalink)
Marc Vangrieken


 
 

Re: c# question

On 6 apr, 21:48, Chris Crowther <hik...@xxxxxx> wrote:
Quote:

> Ten wrote:
Quote:

> > 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.
>
Quote:

> > Tem
>
> --
> Chris
>
> hikari.vcf
> 1KDownloaden

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. The commonality between
the proposed solution, "Façade" and "Proxy" is the usage of
"delegation".

Of course, as a mental model "Façade" is fine...

Kind regards,
Marc Vangrieken
http://vangrieken.wordpress.com
My System SpecsSystem Spec
Old 04-06-2008   #5 (permalink)
Tem


 
 

Re: c# question

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" <marc.vangrieken@xxxxxx> wrote in message
news:96f39104-1794-40b3-b7c0-06baaacee0ce@xxxxxx
Quote:

> 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" <tem1...@xxxxxx> wrote:
Quote:

>> 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
>
My System SpecsSystem Spec
Old 04-06-2008   #6 (permalink)
Jack Jackson


 
 

Re: c# question

On Sun, 6 Apr 2008 12:06:32 -0700, "Ten" <tem1232@xxxxxx> wrote:
Quote:

>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.
My System SpecsSystem Spec
Old 04-06-2008   #7 (permalink)
Peter Bromberg [C# MVP]


 
 

RE: c# question

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:
Quote:

> 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
>
>
My System SpecsSystem Spec
Old 04-07-2008   #8 (permalink)
Marc Vangrieken


 
 

Re: c# question

On 6 apr, 22:28, "Tem" <tem1...@xxxxxx> wrote:
Quote:

> 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" <marc.vangrie...@xxxxxx> wrote in message
>
> news:96f39104-1794-40b3-b7c0-06baaacee0ce@xxxxxx
>
Quote:

> > Hi Tem,
>
Quote:

> > 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".
>
Quote:

> > 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?
>
Quote:

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

> > On 6 apr, 21:06, "Ten" <tem1...@xxxxxx> wrote:
Quote:

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

> >> 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.
>
Quote:
Quote:

> >> Class A
> >> {
> >> B b = new B();
>
Quote:
Quote:

> >> }
>
Quote:
Quote:

> >> 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/Aggrega...29#Aggregation.
The examples are in C++, but with a little imagination it can be C#.

Kind regards,
Marc Vangrieken
http://vangrieken.wordpress.com
My System SpecsSystem Spec
Old 04-07-2008   #9 (permalink)
Marc Vangrieken


 
 

Re: c# question

>I think it's perfectly reasonable to describe briefly
Quote:

> 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

My System SpecsSystem Spec
Reply

Thread Tools


Similar Threads
Thread Forum
IE8 Question Vista General
escape character question - hopefully an easy question PowerShell
vista genral question and ultimate question Vista General
Dual boot system question and family deal discount question Vista General
RC1 Cd Key Question Vista installation & setup


Vista Forums is an independent web site and has not been authorized,
sponsored, or otherwise approved by Microsoft Corporation.
"Windows Vista", the Start Orb, and related materials are trademarks of Microsoft Corp.
© Designer Media Ltd

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46