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.
"Claire" <as*******@ntlworld.com> wrote in message
news:%2****************@tk2msftngp13.phx.gbl...
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.FileGr oup
Group)
{
foobar;
}