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

  • Thread starter Thread starter NicK chlam via DotNetMonster.com
  • Start date Start date
N

NicK chlam via DotNetMonster.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?
 
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
 
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 said:
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?



[microsoft.public.dotnet.languages.csharp]
 
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);
}
 
Daniel O'Connell said:
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.
 
NicK chlam via DotNetMonster.com said:
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).
 
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
 
That was my first response.

Dale

Jon Skeet said:
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).
 
Back
Top