473,405 Members | 2,379 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,405 software developers and data experts.

Object Variables vs Pointers to Object Variables

I suspect I may be fundamentally misunderstanding how object variable
references behave - and I'd sure appreciate some clarification - given my
particular scenario.

Summary: In an ASP.NET (1.1) Web application I store some information in an
ArrayList in the Session. After it is initially placed in the Session, the
applicaiton may need to update some of that information.

The type of data stored in each element of the ArrayList is of a struct I
have defined - like this:

public struct ProductProperties {
public int productID;
public int quantity;
}

// Here's how I go about placing it in the Session (in the code-behind
module of an ascx);

AppName.ClassName.ProductProperties productProperties;
....
productProperties = dataClass.GetProductPropertyValues(productID);
// Note: GetProductPropertyValues() returns an object of type
AppName.ClassName.ProductProperties
// In GetProductPropertyValues() the returned object is instantiated
like this:
// ProductProperties retStruct = new ProductProperties();
// then the members of retStruct are populated, and then the structure
is returned to the caller like this:
// return retStruct;

if (Session["Cart"] == null) {
cart = new System.Collections.ArrayList();
productProperties.quantityInCart = 1; // productProperties has been
instantiated elsewhere
cart.Add(productProperties);
Session.Add("Cart", cart);
}
// Then at some later point the user want to update the quantity. Here's the
code that is supposed to update the
// quantity in the session-level arraylist:

System.Collections.ArrayList cart;
cart = (System.Collections.ArrayList) Session["Cart"]; // Yes, some code is
missing here, but this is what's relevant.
AppName.ClassName.ProductProperties productProperties =
(AppName.ClassName.ProductProperties) cart[loopCounter];

// Notice in the above line that there is no "new" keyword used to
instantiate productProperties.
// I would think then that productProperties as instantiated in this
line is just a pointer to the actual
// object stored in cart.
// And this is where I think I may be wrong. Here's why:
// when I check the values (e.g., productProperties.productID) it has
the expected value.
// So, life is good - that is UNTIL I go to update the value of that
object variable... like this:

productProperties.quantityInCart = newQuantity;

// Doing this can be verified to actually update the value as expected - BUT
ONLY for the local variable,
// 'productProperties'. It is as if that variable is its own object and NOT
simply a pointer to the "real object"
// which is stored in the ArrayList in the Session. I verified this by
creating another variable/pointer
// to the exact same cart element - and it still holds the original value
for quantityInCArt - and NOT
// the value of newQuantity.

// I suppose this is a fundamental of how objects are instantiated in .NET.
I thought a new object was
// created only when I use the 'new' keyword - and when 'new' is omitted I'm
creating a pointer to the
// real or actual object. But given the behaviour I'm observing I must be
wrong about that.

I'd sure appreciate some clarification on this.

Thanks!

Nov 17 '05 #1
1 1651
Jeremy S. wrote:
I suspect I may be fundamentally misunderstanding how object variable
references behave - and I'd sure appreciate some clarification -
given my particular scenario.

Summary: In an ASP.NET (1.1) Web application I store some information
in an ArrayList in the Session. After it is initially placed in the
Session, the applicaiton may need to update some of that information.

The type of data stored in each element of the ArrayList is of a
struct I have defined - like this:

public struct ProductProperties {
public int productID;
public int quantity;
}


That's your error. Structs are value types. So if you do:
ProductProperties someInstace = (ProductProperties)myArrayList[i];

you'll get a NEW instance, a copy, of the original. The same is true
when you grab an int from an arraylist. Increasing it won't increase
the value in the arraylist.

Change it to object. Structs aren't usable in arraylists.

FB

--
------------------------------------------------------------------------
Get LLBLGen Pro, productive O/R mapping for .NET: http://www.llblgen.com
My .NET blog: http://weblogs.asp.net/fbouma
Microsoft MVP (C#)
------------------------------------------------------------------------
Nov 17 '05 #2

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

Similar topics

4
by: Ralph?? | last post by:
I'm trying to use object pointers to access/modify various properties of various layers. The problem i'm having is that pointers created within my functions dont have world scope even though the...
11
by: C++fan | last post by:
Suppose that I define the following class: class example_class{ public: example_class(); void funtion_1(); void function_2(); protected:
3
by: Alex | last post by:
I'm having a problem porting an ASP solution to ASPX. In the ASP solution I'm accessing a DCOM server, create sub DCOM objects and call functions from VB script on the ASP pages. The DCOM object...
6
by: Jake Barnes | last post by:
I was just reading this article on Ajaxian: http://ajaxian.com/archives/show-love-to-the-object-literal This is a newbie question, but what is the object literal? I thought it was like an...
35
by: Frederick Gotham | last post by:
(Before I begin, please don't suggest to me to use "std::vector" rather than actual arrays.) I understand that an object can have resources (e.g. dynamically allocated memory), and so we have to...
7
by: Arpan | last post by:
The .NET Framework 2.0 documentation states that An Object variable always holds a pointer to the data, never the data itself. Now w.r.t. the following ASP.NET code snippet, can someone please...
35
by: Jamey Shuemaker | last post by:
I've seen multiple threads (several in the last 6 months or so) on this topic, but I wanted to clarify my practices. I understand the need for cleaning object variables by setting them to Nothing...
3
by: vijay.gandhi | last post by:
Hi, I am trying to convert some unmanaged code (C++) to managed code (using C++/CLI). 1) One of the functions used returns a void* which I need to cast into a handle of a managed object. Can...
32
by: Joe | last post by:
I am just starting to use Object Oriented PHP coding, and I am seeing quite often the following (this example taken from a wiki): $wakka =& new Wakka($wakkaConfig); What exactly is the =&, and...
275
by: Astley Le Jasper | last post by:
Sorry for the numpty question ... How do you find the reference name of an object? So if i have this bob = modulename.objectname() how do i find that the name is 'bob'
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
0
BarryA
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...
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
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,...
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...
0
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...
0
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,...
0
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...

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.