By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
443,492 Members | 1,289 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 443,492 IT Pros & Developers. It's quick & easy.

Can a class return a value?

P: n/a
I'm working with c# 3.0 and am wondering if I can make a class return a
value like a function. For example, I would like to do something like this:

Point pt;
pt=SomeClass someclass=new SomeClass(double param1, double param2, double
param3);

I have some complex calculations I need to do to get a point's X and Y
values, and I wanted to break this off to a separate class. I also wanted
to make it as simple as possible to get the return value such as doing it
all in one line as above. It cant be a static class because the params
passed in will be stored as class level variables while a number of methods
process them.

Any good recommendations for this?

Thanks.

--
mo*******@noemail.noemail
Jan 30 '07 #1
Share this Question
Share on Google+
2 Replies


P: n/a
Hi moondaddy,

This could be implemented using implicit operator
(http://msdn2.microsoft.com/en-us/lib...(VS.80).aspx):

class SomeClass
{
private Point m_ptResult;

public Point PtResult
{
get { return m_ptResult; }

}

public SomeClass(double p1, double p2, double p3)
{
m_ptResult = new Point(p1 + p2, p3);
}

public static implicit operator Point(SomeClass sc)
{
return sc.PtResult;
}
}

Then you could use code like this:

Point p = new SomeClass(1, 2, 3);
This feature already exists in C# 2.0.
Hope this helps.
Regards,
Walter Wang (wa****@online.microsoft.com, remove 'online.')
Microsoft Online Community Support

==================================================
When responding to posts, please "Reply to Group" via your newsreader so
that others may learn and benefit from your issue.
==================================================

This posting is provided "AS IS" with no warranties, and confers no rights.

Jan 30 '07 #2

P: n/a
Hi Walter,

In that case there is no point in using instance variables because no
reference to the instance is maintained and the OP might as well use a static
class.

Using your class it can be done in two lines though (I am not sure I agree
with the OP's suggestion as it does not exactly improve readability of the
code):

SomeClass c;
Point pt = (c = new SomeClass(1, 2, 3));

:-) Jakob

--
http://www.dotninjas.dk

"Walter Wang [MSFT]" wrote:
Hi moondaddy,

This could be implemented using implicit operator
(http://msdn2.microsoft.com/en-us/lib...(VS.80).aspx):

class SomeClass
{
private Point m_ptResult;

public Point PtResult
{
get { return m_ptResult; }

}

public SomeClass(double p1, double p2, double p3)
{
m_ptResult = new Point(p1 + p2, p3);
}

public static implicit operator Point(SomeClass sc)
{
return sc.PtResult;
}
}

Then you could use code like this:

Point p = new SomeClass(1, 2, 3);
This feature already exists in C# 2.0.
Hope this helps.
Regards,
Walter Wang (wa****@online.microsoft.com, remove 'online.')
Microsoft Online Community Support

==================================================
When responding to posts, please "Reply to Group" via your newsreader so
that others may learn and benefit from your issue.
==================================================

This posting is provided "AS IS" with no warranties, and confers no rights.

Jan 30 '07 #3

This discussion thread is closed

Replies have been disabled for this discussion.