Is it possible to create class constructors that return any value? Like return something; in functions.
Here is an example
//Kent -
using System;
-
using System.Collections.Generic;
-
using System.Linq;
-
using System.Web;
-
using System.Data;
-
-
namespace WebApplication1
-
{
-
public class Class1
-
{
-
public Class1() //This does not work. Is it possible to make a constructor to return anything?
-
{
-
DataTable table = new DataTable();
-
table.Columns.Add("Dosage", typeof(int));
-
table.Columns.Add("Drug", typeof(string));
-
table.Columns.Add("Patient", typeof(string));
-
table.Columns.Add("Date", typeof(DateTime));
-
-
table.Rows.Add(25, "Indocin", "David", DateTime.Now);
-
table.Rows.Add(50, "Enebrel", "Sam", DateTime.Now);
-
table.Rows.Add(10, "Hydralazine", "Christoff", DateTime.Now);
-
table.Rows.Add(21, "Combivent", "Janet", DateTime.Now);
-
table.Rows.Add(100, "Dilantin", "Melanie", DateTime.Now);
-
//return table; It doesn´t work
-
}
-
}
-
}
-
- public class Class1
-
{
-
public Class1() //This does not work. Is it possible to make a constructor to return anything?
-
{
-
}
-
-
public bool DoInitialize()
-
{
-
bool success = false;
-
DataTable table = new DataTable();
-
table.Columns.Add("Dosage", typeof(int));
-
table.Columns.Add("Drug", typeof(string));
-
table.Columns.Add("Patient", typeof(string));
-
table.Columns.Add("Date", typeof(DateTime));
-
-
table.Rows.Add(25, "Indocin", "David", DateTime.Now);
-
table.Rows.Add(50, "Enebrel", "Sam", DateTime.Now);
-
table.Rows.Add(10, "Hydralazine", "Christoff", DateTime.Now);
-
table.Rows.Add(21, "Combivent", "Janet", DateTime.Now);
-
table.Rows.Add(100, "Dilantin", "Melanie", DateTime.Now);
-
success = true;
-
//return table; It doesn´t work
-
Return success;
-
}
-
}
8 10146
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
Why dont you create a method for that call to return a value. all you need to do is -
-
public class Class1{
-
public Class1(){} //constructor
-
public DataTable getData() //method
-
{
-
//your code here
-
-
}
-
}
-
-
Research a bit as Dheeraj mentioned.
- public class Class1
-
{
-
public Class1() //This does not work. Is it possible to make a constructor to return anything?
-
{
-
}
-
-
public bool DoInitialize()
-
{
-
bool success = false;
-
DataTable table = new DataTable();
-
table.Columns.Add("Dosage", typeof(int));
-
table.Columns.Add("Drug", typeof(string));
-
table.Columns.Add("Patient", typeof(string));
-
table.Columns.Add("Date", typeof(DateTime));
-
-
table.Rows.Add(25, "Indocin", "David", DateTime.Now);
-
table.Rows.Add(50, "Enebrel", "Sam", DateTime.Now);
-
table.Rows.Add(10, "Hydralazine", "Christoff", DateTime.Now);
-
table.Rows.Add(21, "Combivent", "Janet", DateTime.Now);
-
table.Rows.Add(100, "Dilantin", "Melanie", DateTime.Now);
-
success = true;
-
//return table; It doesn´t work
-
Return success;
-
}
-
}
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.
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
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.
Ok, thank you all for your help, i think I get it know. All help has been useful. //Kent
Even if the class is sealed you could still use the decorator design pattern to change its functionality if you really wanted to.
Sign in to post your reply or Sign up for a free account.
Similar topics
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...
|
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...
|
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...
|
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++....
|
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...
|
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...
|
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,...
|
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...
|
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"...
|
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...
|
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,...
|
by: Charles Arthur |
last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
|
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
|
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...
|
by: nemocccc |
last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
|
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: 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,...
|
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,...
|
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...
| |