473,799 Members | 2,954 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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(objec t sender, System.EventArg s e)
{
Label l;
l = new Label();
l.Text="Origina l label";
CreateSomething (l);
Response.Write( l.Text);
}

private void CreateSomething (Label l)
{
l = new Label();
l.Text="New Label";
}
Nov 15 '05 #1
3 1406
Allan Lembo <Al********@mai linator.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.co m>
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(objec t sender, System.EventArg s e)
{
Label l;
l = new Label();
l.Text="Origina l 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********@mai linator.com> wrote in message
news:d4******** *************** ***@posting.goo gle.com...
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(objec t sender, System.EventArg s e)
{
Label l;
l = new Label();
l.Text="Origina l 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
13416
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 template and by setting output to a type library (DLL). All the object does is return a string value. At this point I have not checked the option to register for COM interop in Visual Studio So I go into Excel (where I want to use the object). Go to VB...
5
1708
by: nukiboy | last post by:
========= test.js =================== function Test() { this.temp = "hehehehe" ; this.method1 = method1 ;
2
7115
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
1800
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) etc. Now I want to interrogate it in this manner: GetPersonsBetweenAges(INT uLowerAge, INT uUpperAge, ..), and get back a recordset of Persons.
4
1693
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 extract a string variable that contains the corresponding method name. string sMethodName = null; Class1 oClass1 = new Class1( ); If Class1 has a method named Method1 and the variable sMethodName =
1
2312
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 that the soap message will be created correctly with the web method to invoke. Thanks
1
3301
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 objects which all fire into my OnTimer( object state ) function very nicely. I pass in an object "o" on creation of this timer which I subsequently get passed to me in my OnTimer function. Now in the OnTimer function I want to modify the object...
0
1012
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 when I load the script from my asp.net app
3
1429
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
8545
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
9686
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
9540
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10026
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
9068
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
7564
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
5463
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
5585
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
4139
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
3757
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.