What am I not understanding?

  • Thread starter Thread starter Claire
  • Start date Start date
C

Claire

I've explained the problem in the following code snippet. I want to share
single functions for use by several data types.
In this function I call different functions to fill out the original object
"o" that is passed in.
FileGroup is a derivative of an ArrayList object.
Can I fix this problem or will I have to create a mass of overloaded
functions for the whole path from where o originates right through to the
database?

public static bool ReadTourTemplate(ref object o)
{
if (o is Tour)
{
Foo;
}
else if (o is Logic.Collectors.DI225D.Files.FileGroup)
{

// compiler complains if I attempt to do the following
// return readFileGroup(ref ( Logic.Collectors.DI225D.Files.FileGroup)o)
// so I cast o to "create" t and pass that.
Logic.Collectors.DI225D.Files.FileGroup t =
(Logic.Collectors.DI225D.Files.FileGroup)o;

return readFileGroup(ref t );
// at this stage t is a FileGroup containing a list of files and is what
I expect
// On the other hand, object o is no longer the "same" object as t and
the list of files is empty.

}
return false;
}

private static bool readFileGroup(Logic.Collectors.DI225D.Files.FileGroup
Group)
{
foobar;
}
 
Claire said:
I've explained the problem in the following code snippet. I want to share
single functions for use by several data types.
In this function I call different functions to fill out the original object
"o" that is passed in.
FileGroup is a derivative of an ArrayList object.
Can I fix this problem or will I have to create a mass of overloaded
functions for the whole path from where o originates right through to the
database?

Firstly, it's not at all clear that you really need pass by reference.
Secondly, it would almost certainly be better to make your methods
return the appropriate object if you *do* need to create a new object.
I'm assuming the bool is a "it worked or not" flag, in which case you
should probably be using exceptions for that.

If you *really* want to stick with the same design, just do

bool ret = readFileGroup (ref t);
o = t;
return ret;
 
Hi Claire,

Firstly, you don't need to pass the objects by reference for the code below,
unless you are changing what the object references/points to (i.e. unless
you need to use o = new object() ).

What is the relationship between Tour and FileGroup? Usually when there is
an if statement checking for the type of object passed in it means
polymorphism isn't being used where it should. In this case Tour and
FileGroup could have a common base-class or interface with a Read method, so
that a call to o.Read() will pick up the correct method.

Besides that, I don't see the problem. Using both a 'ref' and 'non-ref'
version the code works. I'm not sure what your FileGroup looks like so I
created the simplest version possible for this. The output is One Two Three
as expected:

public static bool ReadTourTemplate(object o)
{
if(o is FileGroup)
{
FileGroup t = (FileGroup)o;
return readFileGroup(t);
}
return false;
}


private static bool readFileGroup(FileGroup Group)
{
Group.files.Add("One");
Group.files.Add("Two");
Group.files.Add("Three");

return true;
}

private void button1_Click(object sender, System.EventArgs e)
{
object fg = new FileGroup();
ReadTourTemplate(fg);
foreach(object o in ((FileGroup)fg).files)
System.Console.WriteLine(o);
}


public class FileGroup
{
public ArrayList files;

public FileGroup()
{
this.files = new ArrayList();
}
}

--Liam.
 
Hi Claire,

It would help if you give us the exact error messages.
Also, your code is inconsistent. You refer to ReadFileGroup accepting a
ref FileGroup and a FileGroup parameter. It is unclair which is correct.
Your current code passes a ref FileGroup into a method accepting FileGroup.

If you can show us your original code I'm sure it will be easier to help
you.

An alternative method might be to either have a common parent so you can
pass the parent reference instead of object, or an interface describing
all necessary methods and properties and pass the interface as the
parameter to ReadTourTemplate.
 
Thanks Liam,
The Tour and FileGroup are totally dissimilar objects, they'd each have a
read and write method but that's the only thing in common. They're each in
different projects, FileGroup is used by a different solution too, so it was
cleaner to leave them separate.
Yes, Id need refs because the final database query contains an "out object
o" parameter , the objects are created in the database project and passed
back.
Where things went wrong was where I believed "t" and "o" would remain
references to the same object. Object pointed to by "t" was set by the
external database project but of course this didn't follow through to object
pointed to by "o" as "t" and "o" hold references but are not the same. Silly
of me to think that, old pre .net rules still apply. (sorry, rubbish at
explaining)
 
I havn't read your issue in great detail as I can see its already
resolved, but I did skim read this part and have a small comment.

In cases like this where you want polymorphic behaviour but don't want
to inherit from a common ancestor, it is still possible to do by using
interfaces, you could create an interface that declares a read and a
write method and have each class support the interface, this would
provide an elegant solution.

Regards Tim.
 
Back
Top