tags:

views:

96

answers:

3

I have two classes that are instantiaded and loaded at runtime. I would like to check the resources of one without having to refer the instances as it can get messy if there are a lot of checks and classes.

For example, if I have the two following classes and I want to call one from another.


Class Item 
{
    private int id;
    private int loc;

    void Item()
    {
        // the Class actually has some properties with get set, but thats not the point here.
        id = 1;
        loc = 2;
    }

    public bool check()
    {
        // Check if the several fields are ok for DB submission

        // how Can I refer to the other class from here? Do I have to pass the instance as a parameter?
        return Locals.Exists(loc); // does not work because its not static!
    }

}

Class Locals
{
    Hashtable l = new Hastable();

    void Locals()
    {
        // This will actually be loaded from a DB at runtime.
        l.add(1, "Local 1");
        l.add(2, "Local 2");
    }

    public bool Exists(int i)
    {
        return l.ContainsKey(i);
    }

}

//Form Code:
main()
{
    Item newItem = new Item();
    Locals allLocals = new Locals();

    newItem.check();
}

Is there a way to do this without having to call

    newItem.check(allLocals);

From what I saw, even with delegates, the caller classe need the instance of the other class.

In short, Is there another way to promote cross Instances communication?

Was I confusing enough?

A: 

It's not entirely clear what you're trying to accomplish here, but one option to fix the problem of passing lots of instances around is to use an IoC framework (Ninject is one example, but there are several available to choose from.) Because instances are injected dynamically as your objects are constructed, this can help to reduce inconvenient glue code. There are other patterns available to allow the classes to find eachother (a Singleton instance or some form of Repository that allows lookup of an instance using some identifying data.) It's also possible to use a Message passing design, which strongly decouples the classes, at the cost of some additional overhead.

It really depends on what you're trying to do. Every design will have its pros and cons.

Dan Bryant
A: 

How can I refer to the other class from here? Do I have to pass the instance as a parameter?

Yes, the only way to communicate with an instance is to get a reference to it (or an intermediary). Since Locals isn't a static class, pass an instance to an Item constructor or method parameter.

Jeff Sternal
A: 

not sure i understand what you mean, but i usually add for similiar cases a static list with all of the objects

Class Locals
{
public static List<Locals> MyLocals = new List<Locals>(); // first thing i add
Hashtable l = new Hastable();

void Locals()
{
    // This will actually be loaded from a DB at runtime.
    l.add(1, "Local 1");
    l.add(2, "Local 2");

    MyLocals.Add(this); // second thing i add
}

and then you can get the objects from a static context.

be sure to edit the dispose function as well, otherwise the GC will not work.

Kaneti
Interesting aproach. It does not exactly addresses the question but seems to be a possible approach.Can you elaborate on the Dispose functions?
upsfeup
if you don't dispose of the objects in the list created - they will never be collected by the garbage collector (since they have a reference leading to them).if you are using memory consuming objects and don't dispose of them - you'll have an out of memory exception...usually I inherit the "IDisposable" interface and implement a simple "MyLocals.Remove(this);" as the function body. when i'm done with the object i call this function and GC is free to collect it.it does require that i do the memory management myself on these objects, so sometimes i need to have a more complexed logic...
Kaneti