Click here to Skip to main content
15,887,351 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi I have a class A with some public variables, these variables are to be accessed by other classes (Forms), so those classes create instances of A. What should be the relationship between class A and other classes?
I am working in C#.Net
Posted
Comments
Zubair SE 23-Oct-13 6:09am    
Class A is a form?
suleman115 23-Oct-13 6:27am    
No its a simple class

First off, variables should not be public - they should normally be encapsulated in Properties instead.

There is no relationship that exists between a form and any class instances it creates - or at least no formal relationship - any more than there is a relationship between your form and a string it displays in a text box. The form may create the instance, but it isn't "related" to the string in any way, other than it uses it to set the value of a TextBox property. Your class is the same as the string, unless it is derived from Control in some way, in which case it can be displayed, and then the form can be the Parent control for the instance of your class control.
 
Share this answer
 
As Per Object Oriented programming concepts, never expose public variables. instead declare them as private and use public methods or properties to access from other classes.

And as per solution there is no relation required between your A and other classes. It is just creating an object of A in Form class.
 
Share this answer
 
A Form is a class instance, that is an object. When it creates an instance of class A then it becomes the 'creator' or the 'owner' of that object. That is a 'has' relationship with the object.
 
Share this answer
 
Comments
suleman115 23-Oct-13 7:10am    
Ok thank you
BillWoodruff 23-Oct-13 7:13am    
I take exception to your description here: when an instance creates an instance of another Object, it does not automatically have any relationship to the created instance.

When a creating Object assigns the reference to a created Object to an internal field, at that point I think it's valid to say the creating Object "has-a" created Object. But, if you really wanted to use the strictest OO terminology, you might say that what the Object "has" is really a field of Type created-object (a variable which is a "handle," that is a pointer to a reference to the created Object). But, for beginning programmers these days, who needs to know the distinction between Composition and Aggregation :) ?
CPallini 23-Oct-13 7:30am    
You are completely right, I used relatively poor phrasing, there.
About 'holding' another object, there are, of course three cases:
'Just creation': the creator immediately loses created object reference.
'Scoped creation': the creator locally assigns the object reference, hence it 'has' it for a while.
'creation as member': this is, as you correctly noted the 'handle' case, the creator holds a reference to the created object for its own lifetime.
"I have a class A with some public variables, these variables are to be accessed by other classes (Forms), so those classes create instances of A."

If the sole purpose of Class A is to be a "provider" of information (data), methods, or functions, to other Classes, then your design here is wrong: each instance of Class A created by other Classes will have its own unique values for each field within A. Depending on how you defined the Class (sealed ?), or how you defined methods, and functions, other Classes might be able to inherit from 'A and change internal state, or, override A's methods or functions.

If you intend that Class A is re-usable, and expect that other Classes will create instances of it, each with their own values stored in A's internal fields, then your design is okay.

You need to clarify what the purpose of Class A is, here.

You can create a static Class in which all fields, methods, and functions, are unchangeable, and are common to all users of the Class.

You can have a non-static Class in which some fields, methods, or functions, are static (unique to the Class itself), and other fields are not static (will be allocated with each instance of the Class created).

You can have a (non-static) Class of which only one instance can be created: that kind of Class is called a Singleton. CodeProject has lots of resources on Singletons: here's one: [^].

Here's Jon Skeet (C# in Depth) on Singleton Classes in C#: [^].
 
Share this answer
 
v3
Comments
suleman115 23-Oct-13 23:47pm    
Class A contains connection string for connecting to SQL Server database, and also some regular expressions. Both are not changed by any accessing class, only their values are being used.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900