By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
446,187 Members | 1,070 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 446,187 IT Pros & Developers. It's quick & easy.

Object Variables vs Pointers to Object Variables

P: n/a
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
Share this Question
Share on Google+
1 Reply


P: n/a
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 discussion thread is closed

Replies have been disabled for this discussion.