Reputation: 357
I'm (as new oop and c# hobbiist) would like to ask for some guidance on the following (simplified) problem:
I've made a small class (lets call it A), with 4 properties, and few methods. Also a larger class (B) with ~10props, and it should contain 12 objects of class A, and a fair amount of functions to play with the props. I have my main class M (which is a windows form), a Filereader class (F) to handle all I/O.
I made a windows event on wm_devicechange, that the usb should be read, making an object of F. Now as F reads the file, it should fill the properties of the object from class B, which was created in the class F. Now the problem is, I cannot access this object of B from my main class. The question is how to do this right?
Should I make a property of F which is type B? Is that common practice? Should I make the object of B in my main class? When making a poperty in F of type B, does it only store and pass the reference if get from M?
I would like to try to avoid useless copiing of the object and such, but this topic of oop is so overwhelming for me right now, even after a few books, I am not even sure this question makes a lot of sense. I have read this article 3 times, but I am still confused what is good practice of handling such a "problem"?
Thanks
Upvotes: 0
Views: 480
Reputation: 5569
public class Form
{
USBReader reader;
CollectedData data;
public Form()
{
reader = new USBReader();
}
public void ReadUSBData()
{
data = reader.ReadUSBData();
}
}
// Type F
public class USBReader
{
public CollectedData ReadUSBData()
{ // usb read logic.
}
}
//Type B
public class CollectedData {
List<A> list = new List<A>();
}
public class A { }
A simple implementation can be like this where your USB reader returns data.
Generally the actions like reading data are methods on your objects with a return type of your data-model( here B and A). While the properties are attributes of your object. e.g. USBReader can have a property with self explanatory name like int BufferSize
;
Upvotes: 1
Reputation: 15502
You won't be wasting space by object assignments, since all objectsclasses in C# are reference types. However in my opinion you should decide between inheritance and nested classes
For inheritance, you will do something like:
public class F : B
{
//class F definition here
}
For nested class, you will have:
public class F
{
public class B
{
}
}
Upvotes: 0
Reputation: 6678
All approaches you mentioned are plausible, but one of them will probably be more intuitive and elegant.
What exactly is the B class? If it's some kind of result from reading the file, you'll probably want to return it from some method of F that does the reading.
Upvotes: 1
Reputation: 3786
If you assign object, it is just reference, so the same instance. If you need to share object between two objects, pass the object as parameter in constructor or some method/property. Then all objects with this reference has access to the same instance (data). If you need different "data set" - instance, simply create other instance of object...
Upvotes: 0