473,387 Members | 1,463 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.

Create a new object in a method and pass it back by ref

Hi Folks

If I create a new instance of an object within a method how can I pass
it back by reference?

For example, the following code, in the context of an ASP.NET page,
returns "Original Label". Why not "New Label"? I realise I could give
CreateSomething a return type of Label and return it that way but in
my case I have more than one parameter that could need reinstancing.

Thanks, Al.
private void Page_Load(object sender, System.EventArgs e)
{
Label l;
l = new Label();
l.Text="Original label";
CreateSomething(l);
Response.Write(l.Text);
}

private void CreateSomething(Label l)
{
l = new Label();
l.Text="New Label";
}
Nov 15 '05 #1
3 1393
Allan Lembo <Al********@mailinator.com> wrote:
If I create a new instance of an object within a method how can I pass
it back by reference?
You need to pass the parameter by reference to start with.
For example, the following code, in the context of an ASP.NET page,
returns "Original Label". Why not "New Label"?
Because the value of l in Page_Load (which is a reference) has been
passed by value. Changing the value of l in CreateSomething doesn't
change the value of l in Page_Load. Note that I'm being precise here -
if you change something within the object that l refers to, that change
will be visible through l, because the value of l *itself* has not
changed.
I realise I could give
CreateSomething a return type of Label and return it that way but in
my case I have more than one parameter that could need reinstancing.


See http://www.pobox.com/csharp/parameters.html for more information
about this.

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet
If replying to the group, please do not mail me too
Nov 15 '05 #2
Hi Allan,

You are passing the parameter as value that's why it's not changed back in
the code, you could either pass it as ref or use instead the return value
for doing this:
private void Page_Load(object sender, System.EventArgs e)
{
Label l;
l = new Label();
l.Text="Original label";
l = CreateSomething();
Response.Write(l.Text);
}

private Label CreateSomething()
{
l = new Label();
l.Text="New Label";
return l;
}
What are you trying to do anyway?

Remember that the old l will only cease of exist if there are not any other
variable referencing it. ( as in the example above) .

Cheers,

--
Ignacio Machin,
ignacio.machin AT dot.state.fl.us
Florida Department Of Transportation

"Allan Lembo" <Al********@mailinator.com> wrote in message
news:d4**************************@posting.google.c om...
Hi Folks

If I create a new instance of an object within a method how can I pass
it back by reference?

For example, the following code, in the context of an ASP.NET page,
returns "Original Label". Why not "New Label"? I realise I could give
CreateSomething a return type of Label and return it that way but in
my case I have more than one parameter that could need reinstancing.

Thanks, Al.
private void Page_Load(object sender, System.EventArgs e)
{
Label l;
l = new Label();
l.Text="Original label";
CreateSomething(l);
Response.Write(l.Text);
}

private void CreateSomething(Label l)
{
l = new Label();
l.Text="New Label";
}

Nov 15 '05 #3
Jon Skeet, Ignacio Machin

Thank you for your replies gentlemen. Now I see. I thought that
because it was an object it was automatically passed as a reference,
whereas actually what's happening is an object reference is being
passed by value. Which is different!

Thanks again, Al.
Nov 15 '05 #4

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

Similar topics

2
by: brazilnut52 | last post by:
I am going to outline the steps I go through to produce the problem. Hopefully this will help you understand the problem better I have created a simple COM DLL in .NET by using the COM class...
5
by: nukiboy | last post by:
========= test.js =================== function Test() { this.temp = "hehehehe" ; this.method1 = method1 ;
2
by: sonu | last post by:
I am tring to create activexobject like var Fo =new ActiveXObject("Scripting.FileSystemObject"); but i am finding error that automation server cant create object any help
1
by: Rufus DeDufus | last post by:
This is more a matter of programming taste ... I have a Database DLL that is calling into Access through OLEDB, and exposes functions like AddPerson(STRING szFirstName, STRING szLastName, INT uAge)...
4
by: BBFrost | last post by:
I want to build a generic application that runs one of a set of class methods depending on data extracted from a database table. For example if I extract value X from the database table I also...
1
by: RA | last post by:
Hi Is there a way to serialize an object method to a soap message? I need to pass a soap message through a queue and have a .net object. I wonder if I can serialize the proxy class method so...
1
by: Paul Tomlinson | last post by:
Question about a System.Threading.Timer object and the "state" object you pass to it... Timer stateTimer = new Timer( = new TimerCallback( OnTimer ), o, 1000, 1000); I have an array of timer...
0
by: Rodney | last post by:
Guys, I have got a vbscript which creates an instance of an object and I get the error ActiveX component can't create object This works if I have this in a standalone .html file but not...
3
by: Hei | last post by:
Hi All, i using .showdialog to show a child form for user input some data, and i wand to pass back these data to the parent form. how can i achieve this? thx. Hei.
3
by: Michael | last post by:
Hi. I have COM+ component installed on my XP. I have problem to create instance of it inside vbscript. How can I do it? The error I get is : ActiveX component can't create object. Thanks
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: 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?
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
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,...

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.