Help regarding classes.

T

trialproduct2004

Hi all

I am having application which has three classes.

in one class say class1, i am having arraylist where i am adding and
removing string.
there is one method in class1 which is creating object of class2 and
calling method of class2.

class2 has one member of type class1.

In class2 i am creating object of class3.

Class3 has member of type class1 which is getting initialized by
class2.
From class3 i want to modify arraylist which i have declared in class1.

So currently i am calling method of class1 by using object of type
class1 which i am passing from class2 to class3.

is it a correct way of doing this.

If some one know better way of modifying array list of outer call by
calling method of some other class, please tell me.

thanks in advance.
 
P

Pohihihi

keeping a ref for the previous form object seems ok but think in the
condition when you are making any changes to your class1, thus have to make
those changes in class3 and 2 as well. Ex can be the method name you are
calling.

If you are changing arraylist with some small data that is not depending on
class 2 or 3 then I will rather use events for this perpose. even if it is
depending on those classes passing small size of data will not reduce
performace. but if you doing changing with heavy data then probably i will
also do what you are doing. this way is much faster than msg passing.
 
G

Guest

In case of larg amount of data transferring, i wud rather do it like the
following

1.create a 'data manager' class just for managing data and expose methods
for manipulate them
2. class1, class2, class3 register event handler to the central 'data
manager'. Also creating and registering events or just delegates if those
classes need to communicate with each other

The reason is that making all other classes depending on just 1 class while
irrelavent to others

just a thought,

regards
 
K

Kevin Spencer

Okay, you have 3 classes. Class 2 has a member of type Class 3. Your
requirement is that the Class 3 member of Class 2 is be able to modify a
member of Class 1, which is not a member of Class 2 or Class 3. So far, am I
correct, other than the fact that you have implemented a way to do this?

It is necessary to strip away everything that is irrelevant in order to see
the solution clearly. It is irrelevant what class created Class 2, as far as
your requirements are concerned. Your requirement is simply that a Class 3
member of a Class 2 instance be able to modify a specific instance of Class
1. Perhaps one of your requirements is that a Class 1 instance create a
Class 2 instance, and that a Class 3 member of the Class 2 instance be able
to modify a member of the Class 1 instance that created it. But you have not
clearly stated this.

Can you restate your requirements exactly? In other words, don't tell us
what you *have* - tell us what functionality your app *requires*
specifically. The design of classes and applications is predicated upon the
exact requirements. Be sure to reference instances as instances where
instances are being referenced, and classes (types) as classes where class
types are being referenced. This will make your requirements easier to
understand. Leave out irrelevant details, such as the type (ArrayList) of
the member of the Class 1 instance that is being modified. As long as the
member is accessible, the type of the member is irrelevant, and muddies the
water.

--
HTH,

Kevin Spencer
Microsoft MVP
..Net Developer
Complex things are made up of
Lots of simple things.
 
T

trialproduct2004

Hi

Thanks for your reply.

I will try to explain my requirement in more details:-

See i have one class say class1 whose member variable I want modify
through class3 's method.

and class1 and class3 are connected to each other using class2.
like class1->class2->class3

class1 is calling method of class2 where i have created instance of
class3 and called method of class3.

so i am passing object of class1 to class2 and from class2 to class3 to
modify member of class1.

Is it a correct way of doing this.

Or is there any other alternative of doing this.

Any help will be appreciated.

thanks
 
J

Jon Skeet [C# MVP]

I will try to explain my requirement in more details:-

See i have one class say class1 whose member variable I want modify
through class3 's method.

and class1 and class3 are connected to each other using class2.
like class1->class2->class3

class1 is calling method of class2 where i have created instance of
class3 and called method of class3.

so i am passing object of class1 to class2 and from class2 to class3 to
modify member of class1.

Is it a correct way of doing this.

Yes (although you shouldn't expose the variable itself in the first
place - you should be using a property or method to manipulate the
instance of class1). Quite how you get the reference to the instance of
class1 as far as the instance of class3 depends on the situation -
sometimes you'll pass it in the constructor (or set it with a property
or method call), so it can keep it for future use. Sometimes you'll
want to pass it in as a parameter to the method which needs it.

Often this kind of circular dependency indicates a design problem
though - it's worth considering whether or not you could change the
design to avoid the problem in the first place. (Often you can't, but
sometimes you can.)

Jon
 
T

trialproduct2004

Hi

thanks for your reply.
can u give me example of using events for modifying data of class1
through class3.

If you can give me sample of this, then it would be benefical for me to
understand event mechanism.

any help will be appreciated.

thanks in advance.
 
K

Kevin Spencer

Okay, I'm presupposing that the structure of these classes is what it is,
and that is part of the scenario.

If so, and you want to modify a member of class 1 in class 3, why are you
passing a reference to the class and not the member?

--
HTH,

Kevin Spencer
Microsoft MVP
..Net Developer
Complex things are made up of
Lots of simple things.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top