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

Can a class return a value?

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
2 1203
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
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

1
by: Michael Albanese | last post by:
I am building an ASP.Net web application that records employee incident data over several screens. I have built custom classes to hold this information as the user enters data. In order to persist...
3
by: jamie_m_ | last post by:
I have a custom collection ... clFile that INHERITS from NameObjectCollectionBase the problem is, when I try to create an xmlserializer instance i get an error You must implement a default...
10
by: Not Available | last post by:
On the host server: namespace JCart.Common public class JCartConfiguration : IConfigurationSectionHandler private static String dbConnectionString; public static String ConnectionString { get...
0
by: Daniel Sélen Secches | last post by:
I found a good class to do a simple FTP. Very good.... I'm posting it with the message, i hope it helps someone ============================================================== Imports...
12
by: Michael Maes | last post by:
Hello, I have a BaseClass and many Classes which all inherit (directly) from the BaseClass. One of the functions in the BaseClass is to (de)serialize the (inherited) Class to/from disk. ...
5
by: Dick | last post by:
Hello, I'm trying to serialize a class with a Hashtable within: ' Class code: Imports System.Collections Class clsOptions Public countID As Integer Public persons As New Hashtable End Class
3
by: Bmack500 | last post by:
I have a sub, and a class. The sub is like this: Sub dothis() Dim aInfoListX As New infoWrapperClass aInfoListX.params = ReadConfig() Dim sqlCMD As New SqlCommand aInfoListX.sqlCmd = sqlCMD ...
1
by: davebaranas | last post by:
I am able to serialize this but I get a null exception when I try to deserialize it back Even if I don't make any child classes it throws "Object reference not set to an instance of an object."...
20
by: tshad | last post by:
Using VS 2003, I am trying to take a class that I created to create new variable types to handle nulls and track changes to standard variable types. This is for use with database variables. This...
6
by: alan | last post by:
I'm creating a sort-of "wrapper" class which (partly) acts like a variable. Something like: template<class t> class cell{ t curval; public: /*public for debugging only - will be private in...
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: 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
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
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
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...

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.