Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Reference Types Question

Status
Not open for further replies.

Milby7

Programmer
Dec 5, 2003
67
GB
Hi!

Does anyone know if objects are returned byval, or byref in c#?

i.e.
Code:
foo refToNewObj = FactoryClass.CreateAndAddToTable();
or
Code:
foo copyOfNewObj = FactoryClass.CreateAndAddToTable();

I'm trying to devise a way of keeping a collection of objects that are references to object instances elsewhere in an application.

e.g. If you create a foo object, then as part of the creation process the instance would be added to a 'loaded objects table'.

However, these objects are created using factory methods which return instances (as above).

If methods return instance copies, then i guess i could store a fooData object in the table & have any foo object refer to these, but that's just another layer of complexity that would be best to avoid.

Thanks!
 
99% sure int, string, long and the like are value objects, while classes are reference objects.

Jason Meckley
Programmer
Specialty Bakers, Inc.
 
Code:
Foo concrete1 = FooFactory.CreateFoo();
Foo concrete2 = concrete1;

concrete1.Property = 1;
concrete2.Property = 2;

// concrete1.Property == 2;

List<Foo> list = new List<Foo>();
list.Add(concrete1);

list[0].Property = 3;

// concrete1.Property == 3;
// concrete2.Property == 3;

mr s. <;)

 
Thanks for your posts.

It's not so much which types are value etc. Going to back to my original post where i mentioned the 'loaded objects table', I'm not sure why, but it seems like i've got a brand new copy of the objects in the table, so when i update the objects elsewhere their member variables get out of sync!? I thought it'd be a simple case of adding them to a central Dictionary<T> i'd end up with a single instance of each object. e.g.

Code:
[i]// Create new instance & add it to table...[/i]
foo obj1 = FactoryClass.CreateObject();

obj1.Name = "Bob";

[i]// Get reference to existing object (from table)...[/i]
foo obj2 = FactoryClass.GetObject(obj1.Id);

[i]// although i'd expect both obj1.Name & obj2.Name to be identical, they're not.[/i]
if (obj1.Name == obj2.Name)
  Console.WriteLine("Everything's in sync!");
else
  Console.WriteLine("The 2 instances aren't the same!?");

Anyone got any ideas?
 
Perhaps your factory is creating new instances, compare obj1 and obj2 using Object.ReferenceEquals.
 
the problem exists either with CreateObject() or GetObject(string id)
if CreateObject() doesn't set the Id property then null/empty is passed to GetObject. If GetObject isn't getting foo, or setting Name then the values are not the same.
these work
Code:
[Test]
public void Strings_are_the_same()
{
   string expected = "foo";
   string actual = "foo";

   Assert.AreEqual(expected, actual);
}

[Test]
public void Ids_are_the_same()
{
   Foo expected = new Foo("foo");
   Foo actual = new Foo("foo");

   Assert.AreNotEqual(expected, acutal);
   Assert.AreEqual(expected.Id, actual.Id);
}

private class Foo
{
   private string id;

   public Foo(string id)
   {
      this.id = id;
   }

   public string Id
   {
      get { return id; }
   }
}
so the problem is either creating or getting the values



Jason Meckley
Programmer
Specialty Bakers, Inc.
 
You're right. I've just knocked up a simple proof of concept which works fine. The full fledged version which doesn't work is built upon the csla framework which makes the process that much more complicated. Apologies for taking your time.

Thanks!
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top