473,411 Members | 1,975 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,411 software developers and data experts.

What am I not understanding?

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;
}
Nov 16 '05 #1
5 1210
Claire <as*******@ntlworld.com> wrote:
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;

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet
If replying to the group, please do not mail me too
Nov 16 '05 #2
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;
}

Nov 16 '05 #3
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.

--
Happy Coding!
Morten Wennevik [C# MVP]
Nov 16 '05 #4
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)
Nov 16 '05 #5
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.
Claire wrote:
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.

Nov 16 '05 #6

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

137
by: Philippe C. Martin | last post by:
I apologize in advance for launching this post but I might get enlightment somehow (PS: I am _very_ agnostic ;-). - 1) I do not consider my intelligence/education above average - 2) I am very...
125
by: Sarah Tanembaum | last post by:
Beside its an opensource and supported by community, what's the fundamental differences between PostgreSQL and those high-price commercial database (and some are bloated such as Oracle) from...
5
by: Randell D. | last post by:
Folks, Can someone point me towards where I can find out what XML is? I'm pretty clued up with PHP, MySQL, Apache and basic javascript and I've heard alot of the past year or more on XML but...
86
by: Michael Kalina | last post by:
Because when I asked for comments on my site-design (Remember? My site, your opinion!) some of you told me never to change anything on font-sizes! What do you guys think of that:...
18
by: Seigfried | last post by:
I have to write a paper about object oriented programming and I'm doing some reading to make sure I understand it. In a book I'm reading, however, polymorphism is defined as: "the ability of two...
669
by: Xah Lee | last post by:
in March, i posted a essay “What is Expressiveness in a Computer Language”, archived at: http://xahlee.org/perl-python/what_is_expresiveness.html I was informed then that there is a academic...
11
by: antonyliu2002 | last post by:
I know that this has been asked and answered thousands of times. As a matter of fact, I know that I need to say If Not Page.IsPostBack Then 'Do something End If for things that needs to be...
25
by: raylopez99 | last post by:
First in an occasional series. I'm somewhat experienced in C++, and am using .NET Visual Studio 2005 as the IDE. I'm learning C#. What I don't like about C#, compared to C++ (all flavors): ...
12
by: Darrel | last post by:
I'm still having a hell of a time figuring out this whole SQL Express set up. I finally discovered why I couldn't run the aspnet_regsql...my local sql server wasn't running. I turned that on,...
4
MMcCarthy
by: MMcCarthy | last post by:
http://bytes.com/images/howtos/projectscope_blocks.jpgAs a freelance IT consultant for over 10 years, Ive come to appreciate well defined project scopes. A project scope is a common understanding...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development projectplanning, coding, testing,...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...
0
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.