473,385 Members | 1,486 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,385 software developers and data experts.

Problem with out parameters

Hi,

I want to make a class that acts like an "object factory", that
creates objects all inherited from a root class called MyBaseObject.
To create objects I want to use a method in which I pass in the first
parameter the object type I want to create, and in the second
parameter (an output parameter) the reference to the object created.

So, the method is declared like this:
public int GetObject(MyObjectTypes type, out MyBaseObject obj)

Suppose I have a class MyObject1 inherited from MyBaseObject.

In the client code that uses the GetObject method I want to do
something like this:

.... other lines of code ...
MyObject1 newObj;
Factory.GetObject(MyObjcectType.Object1, out newObj);
.... other lines of code ...

Using the code above, I get the following error at compilation time:
'argument 2: cannot convert from out MyObject1 to out MyBaseObject'.

I use this code to avoid the problem:

.... other lines of code ...
MyObject1 newObj;
MyBaseObject temp;

Factory.GetObject(MyObjcectType.Object1, out temp);
newObj = (MyObject1) temp;
.... other lines of code ...

Is there a way to force the c# compiler to accept the first version of
my code?

Thanks in advance for any suggestion

Bye
Gianluca
Nov 16 '05 #1
4 1159
>Is there a way to force the c# compiler to accept the first version of
my code?


No

Mattias

--
Mattias Sjögren [MVP] mattias @ mvps.org
http://www.msjogren.net/dotnet/ | http://www.dotnetinterop.com
Please reply only to the newsgroup.
Nov 16 '05 #2
Gianluca <mo**********@hotmail.com> wrote:
I want to make a class that acts like an "object factory", that
creates objects all inherited from a root class called MyBaseObject.
To create objects I want to use a method in which I pass in the first
parameter the object type I want to create, and in the second
parameter (an output parameter) the reference to the object created.

So, the method is declared like this:
public int GetObject(MyObjectTypes type, out MyBaseObject obj)


What's it returning, and is it actually important? (You're ignoring it
in the calling code example you've given.) It seems quite odd to me
that a method called GetObject doesn't return an object of any
description in its normal returned value.

I suggest you change the signature to

public MyBaseObject GetObject (MyObjectTypes type)

That way you can just do:

MyObject1 newObj = (MyObject1) Factory.GetObject(MyObjectType.Object1);

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet
If replying to the group, please do not mail me too
Nov 16 '05 #3
> Is there a way to force the c# compiler to accept the first version
of my code?

As Mattias said, "No," and here is why.

Your method declares that it is returning a MyBaseObject in the out
variable, which means that it could return an actual MyBaseObject or
any object of a type derived from MyBaseObject, such as a MyObject1.

However, the calling method has no way of knowing what will be
returned. If it were to allow you to pass a MyObject1 as the out
argument, then the method could conceivably return an object of type
MyBaseObject, or MyObject2, or some other thing, and you would end up
breaking type safety. You would have a MyObject1 variable referring to
(pointing to) an object of a different type. Very bad!

As such, the compiler requires you to perform a specific act--a
cast--to assure it that you know the thing being returned is really a
MyObject1 and not anything else.

I suppose that the language designers could come up with some
nomenclature for a cast-and-out-argument all rolled into one, but it
seems unlikely that you'd often need something like that.

You can, of course, get around this by doing this:

public class Factory
{
public static void GetObject(MyObjectType type, out MyBaseObject
result)
{
...
}

public static void GetObject(MyObjectType type, out MyObject1 result)
{
MyBaseObject temp;
GetObject(type, out temp);
result = (MyObject1)temp;
}
}

....but then the "object type" argument seems superfluous, doesn't it,
so you could simply recode it like this:

public static void GetObject(out MyObject1 result)
{
MyBaseObject temp;
GetObject(MyObjectType.Object1, out temp);
result = (MyObject1)temp;
}

Note that this works only if you use "out" parameters... it won't work
with function results, because all of the methods would have the same
signature.

It also someone bastardizes the Factory pattern, because it means that
for every new derived type you implement, you have to create a new
nethod signature in the Factory. However, it does give you a more
convenient notation for calling the factory method.

Nov 16 '05 #4
mo**********@hotmail.com (Gianluca) wrote in message news:<56**************************@posting.google. com>...
Hi,

[...]

Thanks to all for your help.
I know that what I'm trying to do is possibile in VB.NET, so I thought
that I could do it in that way also in C#.

Anyway, I will take into consideration your suggestions.

Bye
Nov 16 '05 #5

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

Similar topics

7
by: Christopher Brandsdal | last post by:
Hi! I have a problem running my code on 2000 server and iis5.0. The code runs perfectly on my localhost (xp iis5.1) but when i run it on 2000 server iis5.0 I get this error: ...
3
by: Bilbo | last post by:
I have a a headscratcher here: I have a form that when submitted should do 2 things when a user enters data and then clicks the Add button. Here goes: 1. Call a stored procedure called...
1
by: Thanks | last post by:
I have a routine that is called on Page_Init. It retrieves folder records from a database which I display as Link Buttons in a table cell. I set the table cell's bgcolor to a default color (say...
1
by: leslie_tighe | last post by:
Hello, I have webservice created with Axis 1.2.1 and that I am trying to consuming in .NET (VB) using the Microsoft provided tools. While I am able to consume methods on the service that return...
4
by: leslie_tighe | last post by:
Hello, I have a webservice running on a J2EE server created with Axis 1.2.. I have a client that I am building in .net that needs to consume this webserivce and am having a bit of trouble. I have...
0
by: ryan | last post by:
I've been tasked with consuming a Perl web service that was written by a person in a different department of my company. The problem is it's the guy's first attempt at web services and he doesn't...
0
by: CJM | last post by:
Repeated for the benefit of m.p.i.asp.general, which I forgot to include in the original posting... "CJM" <cjmnews04@REMOVEMEyahoo.co.ukwrote in message news:4lo3f8F2shqtU1@individual.net...
12
by: Light | last post by:
Hi all, I posted this question in the sqlserver.newusers group but I am not getting any response there so I am going to try it on the fine folks here:). I inherited some legacy ASP codes in my...
14
by: rashmidutt | last post by:
hello sir i am making project on vb.net language..and project is on hospital management..its major project..and too many fields are present in its data base..i was connecting data base in forms but...
3
by: kpeeroo | last post by:
Private Function AddCompanyOvertime() As Integer Dim companyID As Integer = GetCompanyID() Console.WriteLine(companyID) Dim paramCompanyID As New SqlParameter("@CompanyID",...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 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 former...
0
by: ryjfgjl | last post by:
In our work, we often need to import Excel data into databases (such as MySQL, SQL Server, Oracle) for data analysis and processing. Usually, we use database tools like Navicat or the Excel import...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
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...

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.