0

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

Onsightfree
  • 307
  • 3
  • 11
  • 1
    I've read your question like 3-4 times and still can't figure out what exactly is the question. Please, post some code. – LightStriker Oct 22 '12 at 09:36
  • Try to create a simple and self-contained example of *code* helping explain what you've done and what you're asking, and post it here. =) – J. Steen Oct 22 '12 at 09:36
  • The question will be easier to answer if you show your type definitions. – Jodrell Oct 22 '12 at 09:36

4 Answers4

1

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.

user1610015
  • 6,561
  • 2
  • 15
  • 18
  • This B Class should contain most of the information I read from the usb. Then based on userinput Im about to manipulate the data, and few steps later I will overwrite the files on the usb. The form handles the user input, but the heart of the software should be manipulating some files with minimalizing read and write. – Onsightfree Oct 22 '12 at 09:48
  • Then I think you should make all the reading in the constructor of the USB reader class, and store the info in the B object, which should be a property of the USB reader class. – user1610015 Oct 22 '12 at 09:55
  • Thanks for your reply, I would very likely upvote your answer if I could, but missing the 15 repu. I think it also helped me in understanding, just as NileshChauhan's answer – Onsightfree Oct 22 '12 at 10:04
1
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;

NileshChauhan
  • 5,469
  • 2
  • 29
  • 43
  • I am staring at your answer right now, and I think its the one I was looking for. If I understand correctly, I would be able to reach all getters setters and public functions of 'data' from my Form class? – Onsightfree Oct 22 '12 at 09:59
  • 1
    I have tried it out in the meantime, and it works wonderfully. Regarding this link, If the 2nd case happens here, then I fully understand whats going on. http://rapidapplicationdevelopment.blogspot.de/2007/01/parameter-passing-in-c.html - So I accepted your solution as the answer, thanks a lot NileshChauhan – Onsightfree Oct 22 '12 at 10:40
  • It is indeed. reader.ReadUSBData() create a an object in memory, whose address is returned and eventually captured in data,. Which is available for your to manipulate further using data as the handle. – NileshChauhan Oct 22 '12 at 12:18
  • It would be worth for you to Property and Methods. Pointer: http://stackoverflow.com/questions/601621/properties-vs-methods/601783#601783 – NileshChauhan Oct 22 '12 at 12:21
0

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
    {
    }
}
rexcfnghk
  • 14,435
  • 1
  • 30
  • 57
0

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

Fanda
  • 3,760
  • 5
  • 37
  • 56