473,385 Members | 1,764 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.

Can constructor return values?

10
Is it possible to create class constructors that return any value? Like return something; in functions.

Here is an example
//Kent

Expand|Select|Wrap|Line Numbers
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Web;
  5. using System.Data;
  6.  
  7. namespace WebApplication1
  8. {
  9.     public class  Class1 
  10.     {
  11.         public  Class1() //This does not work. Is it possible to make a constructor to return anything?
  12.         {
  13.             DataTable table = new DataTable();
  14.             table.Columns.Add("Dosage", typeof(int));
  15.             table.Columns.Add("Drug", typeof(string));
  16.             table.Columns.Add("Patient", typeof(string));
  17.             table.Columns.Add("Date", typeof(DateTime));
  18.  
  19.             table.Rows.Add(25, "Indocin", "David", DateTime.Now);
  20.             table.Rows.Add(50, "Enebrel", "Sam", DateTime.Now);
  21.             table.Rows.Add(10, "Hydralazine", "Christoff", DateTime.Now);
  22.             table.Rows.Add(21, "Combivent", "Janet", DateTime.Now);
  23.             table.Rows.Add(100, "Dilantin", "Melanie", DateTime.Now);
  24.             //return table; It doesn´t work
  25.          }
  26.     }
  27. }
  28.  
Apr 23 '10 #1

✓ answered by tlhintoq

Expand|Select|Wrap|Line Numbers
  1. public class  Class1 
  2.     {
  3.         public  Class1() //This does not work. Is it possible to make a constructor to return anything?
  4. {
  5. }
  6.  
  7. public bool DoInitialize()
  8.         {
  9. bool success = false;
  10.             DataTable table = new DataTable();
  11.             table.Columns.Add("Dosage", typeof(int));
  12.             table.Columns.Add("Drug", typeof(string));
  13.             table.Columns.Add("Patient", typeof(string));
  14.             table.Columns.Add("Date", typeof(DateTime));
  15.  
  16.             table.Rows.Add(25, "Indocin", "David", DateTime.Now);
  17.             table.Rows.Add(50, "Enebrel", "Sam", DateTime.Now);
  18.             table.Rows.Add(10, "Hydralazine", "Christoff", DateTime.Now);
  19.             table.Rows.Add(21, "Combivent", "Janet", DateTime.Now);
  20.             table.Rows.Add(100, "Dilantin", "Melanie", DateTime.Now);
  21. success = true;
  22.             //return table; It doesn´t work
  23. Return success;
  24.          }
  25.     }

8 10146
Dheeraj Joshi
1,123 Expert 1GB
No constructor can not return anything. :)
BTW, why you want constructor to return anything?
Google a bit to know more about constructors.

Regards
Dheeraj Joshi
Apr 23 '10 #2
semomaniz
210 Expert 100+
Why dont you create a method for that call to return a value. all you need to do is

Expand|Select|Wrap|Line Numbers
  1.  
  2. public class  Class1{
  3.      public Class1(){}   //constructor
  4.      public DataTable  getData()   //method
  5.      {
  6.           //your code here
  7.  
  8.      }
  9. }
  10.  
  11.  
Research a bit as Dheeraj mentioned.
Apr 23 '10 #3
tlhintoq
3,525 Expert 2GB
Expand|Select|Wrap|Line Numbers
  1. public class  Class1 
  2.     {
  3.         public  Class1() //This does not work. Is it possible to make a constructor to return anything?
  4. {
  5. }
  6.  
  7. public bool DoInitialize()
  8.         {
  9. bool success = false;
  10.             DataTable table = new DataTable();
  11.             table.Columns.Add("Dosage", typeof(int));
  12.             table.Columns.Add("Drug", typeof(string));
  13.             table.Columns.Add("Patient", typeof(string));
  14.             table.Columns.Add("Date", typeof(DateTime));
  15.  
  16.             table.Rows.Add(25, "Indocin", "David", DateTime.Now);
  17.             table.Rows.Add(50, "Enebrel", "Sam", DateTime.Now);
  18.             table.Rows.Add(10, "Hydralazine", "Christoff", DateTime.Now);
  19.             table.Rows.Add(21, "Combivent", "Janet", DateTime.Now);
  20.             table.Rows.Add(100, "Dilantin", "Melanie", DateTime.Now);
  21. success = true;
  22.             //return table; It doesn´t work
  23. Return success;
  24.          }
  25.     }
Apr 23 '10 #4
hype261
207 100+
If you are tracking a failure during the objects initialization then you can throw an exception in the constructor. Just rememeber to catch the exception somewhere.
Apr 23 '10 #5
Kenta
10
Ok, thank you all for your answers, I learned something knew from it. At the moment I am not sitting behind a computer with Visual Studio installed so what I say now is just a thought. - I guess that it should be possible to inherit one class to another like Class2 : Class1. But is it possible to inherit from built in classes? Something like this Class1 : DataTable Is that possible?
//Kent
Apr 25 '10 #6
tlhintoq
3,525 Expert 2GB
Yes it is. Usually. Some .NET classes are sealed, meaning you can't inherit from them. That sucks when you run into one. But mostly you can.

I do this fairly often with the PictureBox control, to make a more customized one with a right-click | Save... feature... and that sort of thing.
Apr 25 '10 #7
Kenta
10
Ok, thank you all for your help, i think I get it know. All help has been useful. //Kent
Apr 26 '10 #8
hype261
207 100+
Even if the class is sealed you could still use the decorator design pattern to change its functionality if you really wanted to.
Apr 26 '10 #9

Sign in to post your reply or Sign up for a free account.

Similar topics

3
by: Test | last post by:
Sorry, some people may have asked this question before. It is really hard to find relevant articles about this topic on the web using key words. I know it is not recommended to use return...
28
by: Chiller | last post by:
I am in the process of writing a class that will represent metric distances by accepting a value (ie, 3) and a unit of measure (ie, m). I've written my constructor in the .h file as Distance...
7
by: Sandy | last post by:
Is there any way to write a "single constructor" that can be called using either 1 or 2 arguments. But without using default arguments. Actually i don't need this. Just asking as some other guy...
0
by: Queen | last post by:
Hi! First of all, perdon for my English. I don't express the thinks very good. Well, I'm working with eclipse 3.1.1, wxWidgets 2.4.2, MSYS 1.0.10 and Mingw 3.1.0. I'm doing a project in c++....
1
by: Patient Guy | last post by:
I am trying to write functions that manipulate objects representing dynamic tables in HTML documents in a more friendly way (to me). I do this by creating a table object (tableObject) through a...
3
by: sarathy | last post by:
Hi all, I have doubt regarding how objects are passed in C++. The primary problem of passing by value in C++, is that the destructor of the object passed will be called twice, thus creating...
6
by: Rolandpish | last post by:
Hi there. I'm doing an application in C# 2005. I was used in VB6 to put code in Forms load event in order to initialize some values of the controls (grid values, text boxes values, label captions,...
22
by: clicwar | last post by:
A simple program with operator overloading and copy constructor: #include <iostream> #include <string> using namespace std; class Vector { private: float x,y; public: Vector(float u, float...
53
by: Sanders Kaufman | last post by:
I'm having a BLAST writing this app as OOP as PHP allows. It's one thing to discuss the dance, but it's a thing of beauty to see the performance. I'm noticing that the constructor is a "reset"...
13
by: JD | last post by:
Hi, My associate has written a copy constructor for a class. Now I need to add an operator = to the class. Is there a way to do it without change her code (copy constructor) at all? Your help...
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: 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
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
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
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
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.