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

Urgent any way to create a new object and return it in a function

is thier any way to create a new object in a function and return that
object being able to use it in the main class?

like to have a static method that inputs first and last names, creates a
object and returns it?

--
Message posted via http://www.dotnetmonster.com
Nov 17 '05 #1
8 1273
Either create your method to return the object, like:

public MyObject SomeFunction(string firstname, string lastname)
{
return new MyObject(firstname, lastname);
}

Or, if you are returning a different type, for whatever reason, since
objects are reference types, just pass the object type in:

public void SomeFunction, string firstname, stringlastname, MyObject obj)
{
obj = new MyObject(firstname, lastname);
}

HTH

Dale Preston
MCAD, MCDBA, MCSE
"NicK chlam via DotNetMonster.com" <fo***@nospam.DotNetMonster.com> wrote in
message news:e8******************************@DotNetMonste r.com...
is thier any way to create a new object in a function and return that
object being able to use it in the main class?

like to have a static method that inputs first and last names, creates a
object and returns it?

--
Message posted via http://www.dotnetmonster.com

Nov 17 '05 #2
The second will have to be:

public void SomeFunction, string firstname, stringlastname, ref MyObject obj)
{
obj = new MyObject(firstname, lastname);
}

so hte MyObject refernce can be changed

Regards

Richard Blewett - DevelopMentor
http://www.dotnetconsult.co.uk/weblog
http://www.dotnetconsult.co.uk

Either create your method to return the object, like:

public MyObject SomeFunction(string firstname, string lastname)
{
return new MyObject(firstname, lastname);
}

Or, if you are returning a different type, for whatever reason, since
objects are reference types, just pass the object type in:

public void SomeFunction, string firstname, stringlastname, MyObject obj)
{
obj = new MyObject(firstname, lastname);
}

HTH

Dale Preston
MCAD, MCDBA, MCSE
"NicK chlam via DotNetMonster.com" <fo***@nospam.DotNetMonster.com> wrote in
message news:e8******************************@DotNetMonste r.com...
is thier any way to create a new object in a function and return that
object being able to use it in the main class?

like to have a static method that inputs first and last names, creates a
object and returns it?

--
Message posted via http://www.dotnetmonster.com


[microsoft.public.dotnet.languages.csharp]
Nov 17 '05 #3
> Or, if you are returning a different type, for whatever reason, since
objects are reference types, just pass the object type in:

public void SomeFunction, string firstname, stringlastname, MyObject obj)
{
obj = new MyObject(firstname, lastname);
}

That actually will not work. Objects are reference types, but they still
pass by value, that is the reference to the object is pushed onto the stack
and the value of the reference is passed, whereas parameters passed by
reference actually pass a reference to the variable in question which allows
you to assign to that variable. Jon Skeet has a good article on this, but I
can't quite seem to get his site going to find it. Hopefully he'll post a
link as he did a much better job explaining it than I have right now.

The correct code would be
public void SomeFunction(string firstname, string lastname, out MyObject
obj)
{
obj = new MyObject(firstname, lastname);
}
HTH

Dale Preston
MCAD, MCDBA, MCSE
"NicK chlam via DotNetMonster.com" <fo***@nospam.DotNetMonster.com> wrote
in
message news:e8******************************@DotNetMonste r.com...
is thier any way to create a new object in a function and return that
object being able to use it in the main class?

like to have a static method that inputs first and last names, creates a
object and returns it?

--
Message posted via http://www.dotnetmonster.com


Nov 17 '05 #4
Google on class factories. Pretty cool stuff.

Regards,
Jeff

*** Sent via Developersdex http://www.developersdex.com ***
Nov 17 '05 #5
Daniel O'Connell [C# MVP] <onyxkirx@--NOSPAM--comcast.net> wrote:
Or, if you are returning a different type, for whatever reason, since
objects are reference types, just pass the object type in:

public void SomeFunction, string firstname, stringlastname, MyObject obj)
{
obj = new MyObject(firstname, lastname);
}


That actually will not work. Objects are reference types, but they still
pass by value, that is the reference to the object is pushed onto the stack
and the value of the reference is passed, whereas parameters passed by
reference actually pass a reference to the variable in question which allows
you to assign to that variable. Jon Skeet has a good article on this, but I
can't quite seem to get his site going to find it. Hopefully he'll post a
link as he did a much better job explaining it than I have right now.


http://www.pobox.com/~skeet/csharp/parameters.html

It looks like my web server ISP may have a problem at the moment.
However, if you search for "particularly with regard to reference
types" (including the quotes) on Google, you'll find the page and can
view its cached copy.

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet
If replying to the group, please do not mail me too
Nov 17 '05 #6
NicK chlam via DotNetMonster.com <fo***@nospam.DotNetMonster.com>
wrote:
is thier any way to create a new object in a function and return that
object being able to use it in the main class?

like to have a static method that inputs first and last names, creates a
object and returns it?


I'm not sure why the current responses that I've seen haven't suggested
just returning the value:

static Name (string first, string last)
{
return new Name (first, last);
}

(assuming a "Name" class with appropriate parameters, of course).

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet
If replying to the group, please do not mail me too
Nov 17 '05 #7
simply define a static method wich calls a constructor and returns the
result:

public static MyClass GetObject(.........)
{
//maybe some preparing code
.....

return new MyClass(........);
}

that's all

"NicK chlam via DotNetMonster.com" <fo***@nospam.DotNetMonster.com> schrieb
im Newsbeitrag news:e8******************************@DotNetMonste r.com...
is thier any way to create a new object in a function and return that
object being able to use it in the main class?

like to have a static method that inputs first and last names, creates a
object and returns it?

--
Message posted via http://www.dotnetmonster.com

Nov 17 '05 #8
That was my first response.

Dale

"Jon Skeet [C# MVP]" <sk***@pobox.com> wrote in message
news:MP************************@msnews.microsoft.c om...
NicK chlam via DotNetMonster.com <fo***@nospam.DotNetMonster.com>
wrote:
is thier any way to create a new object in a function and return that
object being able to use it in the main class?

like to have a static method that inputs first and last names, creates a
object and returns it?


I'm not sure why the current responses that I've seen haven't suggested
just returning the value:

static Name (string first, string last)
{
return new Name (first, last);
}

(assuming a "Name" class with appropriate parameters, of course).

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet
If replying to the group, please do not mail me too

Nov 17 '05 #9

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

Similar topics

9
by: Moe Sizlak | last post by:
Hi There, I am trying to write the selected value of a listcontrol when a button is clicked and I keep getting the error "object not set to a reference of an object". The libox itself is in a...
8
by: Tim::.. | last post by:
Can someone please tell me why I keep getting the following error for some of my web application users but not others??? Even though the application runs from a central webserver??? Thanks for...
2
by: kalikoi | last post by:
Hi All when i execute the following code i'm getting the error **************************************************************************** Content-type: text/html Software error: Malformed...
20
by: Niyazi | last post by:
Hi all, I have a integer number from 1 to 37000. And I want to create a report in excel that shows in 4 alphanumeric length. Example: I can write the cutomerID from 1 to 9999 as: 1 ---->...
3
by: ricolee99 | last post by:
Hi everyone, I have a problem that i have been trying to solve for awhile. I'm given a code where the purpose is to create a general dataset mapper. Given any dataset, i have a class,...
1
by: dasilva109 | last post by:
Hi guys I am new to C++ and need urgent help with this part of my code for a uni coursework I have to submit by Thursday //ClientData.h #ifndef CLIENTDATA_H #define CLIENTDATA_H #include...
3
by: sunbeam | last post by:
Short Description of the Project: we developed a e-learning system for our students. each student has a unique username/password to view the modules he/she should view and nothing more. since we...
6
by: jenipriya | last post by:
Hi all... its very urgent.. please........i m a beginner in oracle.... Anyone please help me wit dese codes i hv tried... and correct the errors... The table structures i hav Employee (EmpID,...
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: 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: 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
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
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.