Results 1 to 2 of 2

Thread: How to return reference??

  1. #1

    Thread Starter
    Frenzied Member
    Join Date
    May 2002
    Posts
    1,602

    How to return reference??

    I have a really tricky problem, here is the deal:

    I have a method that need to perform certain things based on the type of the object I pass as parameter. I pass an interface where I have a method called "getmytype" where I return "image", "text" etc etc

    NOW in this function I need to get the instance of the object that the interface is implemented with. Can this be done?

    Is there another way to solve it?

    The problems start with that I have an arraylist of 5 different kinds of objects.

    Then I have a loop that will interate through all these and get information from the objects. And depending on what kind of object it is, different properties have to be accessed. That is why I need to distinguish them so I can check the type, because when I access them from the arraylist they are of type "object". Then I perform a directcast to the interface and pass that to the function.


    Perhaps I have solved this in a bad way? By all means suggest improvements how I can design this in a better way. Im happy to explain it in further detail if needed...
    kind regards
    Henrik

  2. #2

    Thread Starter
    Frenzied Member
    Join Date
    May 2002
    Posts
    1,602
    Ouch no replies, I think I need to rephrase a bit.

    I HAVE solved the problem, yes but is it the right way to do it? I tried to find a proper pattern that would give me a hint, but nada.


    I will outline the base of the problem.

    * I have different objects in an array. These objects have nothing in common (too bad for me).

    * I need to write one method that can one of these objects and execute a method depending on what kind of object it is (type)

    *I need this to be easy to use and highly adaptable to new kind sof objects...

    * I solved this by creating a silly interface called "name"

    * and when I call my method Im performing a directcast from the array object to IDummy(my interface)

    * and in the method I call the only method in the interface "GetName" which return the name of the object.

    * IN the method I then have a select - case that is performing different things based on the name of the object.


    This is a bad solution in terms of OO. I thought about the grasp pattern "expert" which says clearly that the object with enough information should perform the task. But in my way I want another class to handle these objects because in this case it is better for the overall structure...


    * The class im writing is a printing class, and all these objects in the array are line objects. And on the printing eventarg I have to call a different method of printing wether or not it is text, bitmap or whatever... do you understand? Im basically weapping the winforms printing in a number of support classes making priting so much easier...

    I need help from all experienced MCAD's and MCSDs's here!!

    kind regards
    Henrik

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width