Connecting Tech Pros Worldwide Help | Site Map

Passing an Object by Ref

Mike Guerrieri
Guest
 
Posts: n/a
#1: Jul 22 '05
I have a few objects that I've created, Contact, Address, AddressCollection
(inherits from CollectionBase), dtaContact, and dtaAddress. One of the
properties of the Contact object (Contact.Addresses) is a Type of
AddressCollection. When I want to add a new contact to my database I create a
new Address object for each address and a new AddressCollection object. I
then populate the Address objects and add them to the AddressCollection
(InnerList.Add(address)). Next I populate the Contact object properties
including the AddressCollection. Now I want to pass the Contact object by Ref
to the dtaContact.Insert() method so that I can use the output parameter of
my SP to populate the ContactID property and have that value reflected in my
contact object. This functionality works just fine. However within my
dtaContact.Insert() method I want to loop through the AddressCollection and
pass each Address object by Ref to the dtaAddress.Insert method. When I do
this I get the following error when I try to Rebuild...

error CS1605: Cannot pass 'contactAddress' as a ref or out argument because
it is read-only

Can anyone provide some insight as to why this may be happening?

Thanks,

Mike G.
Scott M.
Guest
 
Posts: n/a
#2: Jul 22 '05

re: Passing an Object by Ref


It appears that you have a property called: contactAddress that is marked
as ReadOnly.

"Mike Guerrieri" <Mike Guerrieri@discussions.microsoft.com> wrote in message
news:46C52177-8998-45DA-A3F9-70FE22C137C6@microsoft.com...[color=blue]
>I have a few objects that I've created, Contact, Address, AddressCollection
> (inherits from CollectionBase), dtaContact, and dtaAddress. One of the
> properties of the Contact object (Contact.Addresses) is a Type of
> AddressCollection. When I want to add a new contact to my database I
> create a
> new Address object for each address and a new AddressCollection object. I
> then populate the Address objects and add them to the AddressCollection
> (InnerList.Add(address)). Next I populate the Contact object properties
> including the AddressCollection. Now I want to pass the Contact object by
> Ref
> to the dtaContact.Insert() method so that I can use the output parameter
> of
> my SP to populate the ContactID property and have that value reflected in
> my
> contact object. This functionality works just fine. However within my
> dtaContact.Insert() method I want to loop through the AddressCollection
> and
> pass each Address object by Ref to the dtaAddress.Insert method. When I do
> this I get the following error when I try to Rebuild...
>
> error CS1605: Cannot pass 'contactAddress' as a ref or out argument
> because
> it is read-only
>
> Can anyone provide some insight as to why this may be happening?
>
> Thanks,
>
> Mike G.[/color]


Mike Guerrieri
Guest
 
Posts: n/a
#3: Jul 22 '05

re: Passing an Object by Ref


Thanks for the fast reply Scott. The contactAddress is not marked ReadOnly,
here is the section where it is created...

dtaContactAddress contactAddressData = new dtaContactAddress();
foreach (ContactAddress contactAddress in contact.AddressCollection)
{
contactAddress.ContactId = contactID;
contactAddressData.Insert(ref contactAddress);
}

Could it be that objects in a collection are implicitly ReadOnly?

- Mike G.


"Scott M." wrote:
[color=blue]
> It appears that you have a property called: contactAddress that is marked
> as ReadOnly.
>
> "Mike Guerrieri" <Mike Guerrieri@discussions.microsoft.com> wrote in message
> news:46C52177-8998-45DA-A3F9-70FE22C137C6@microsoft.com...[color=green]
> >I have a few objects that I've created, Contact, Address, AddressCollection
> > (inherits from CollectionBase), dtaContact, and dtaAddress. One of the
> > properties of the Contact object (Contact.Addresses) is a Type of
> > AddressCollection. When I want to add a new contact to my database I
> > create a
> > new Address object for each address and a new AddressCollection object. I
> > then populate the Address objects and add them to the AddressCollection
> > (InnerList.Add(address)). Next I populate the Contact object properties
> > including the AddressCollection. Now I want to pass the Contact object by
> > Ref
> > to the dtaContact.Insert() method so that I can use the output parameter
> > of
> > my SP to populate the ContactID property and have that value reflected in
> > my
> > contact object. This functionality works just fine. However within my
> > dtaContact.Insert() method I want to loop through the AddressCollection
> > and
> > pass each Address object by Ref to the dtaAddress.Insert method. When I do
> > this I get the following error when I try to Rebuild...
> >
> > error CS1605: Cannot pass 'contactAddress' as a ref or out argument
> > because
> > it is read-only
> >
> > Can anyone provide some insight as to why this may be happening?
> >
> > Thanks,
> >
> > Mike G.[/color]
>
>
>[/color]
Jon Skeet [C# MVP]
Guest
 
Posts: n/a
#4: Jul 22 '05

re: Passing an Object by Ref


Mike Guerrieri <MikeGuerrieri@discussions.microsoft.com> wrote:[color=blue]
> Thanks for the fast reply Scott. The contactAddress is not marked ReadOnly,
> here is the section where it is created...
>
> dtaContactAddress contactAddressData = new dtaContactAddress();
> foreach (ContactAddress contactAddress in contact.AddressCollection)
> {
> contactAddress.ContactId = contactID;
> contactAddressData.Insert(ref contactAddress);
> }
>
> Could it be that objects in a collection are implicitly ReadOnly?[/color]

Objects themselves aren't readonly. However, the *variable*
contactAddress is readonly - the variable used in a foreach is always
implicitly readonly.

It doesn't look to me like you need to pass contactAddress by reference
though - that would just allow Insert to change the value of
contactAddress, which itself is a reference.

I *suspect* you're a bit confused about reference types vs value types
and parameter passing.
See http://www.pobox.com/~skeet/csharp/parameters.html

--
Jon Skeet - <skeet@pobox.com>
http://www.pobox.com/~skeet
If replying to the group, please do not mail me too
Mike Guerrieri
Guest
 
Posts: n/a
#5: Jul 22 '05

re: Passing an Object by Ref


Thank you Jon, I didn't realize that the variable used in a foreach is always
implicitly readonly.

I don't believe that I'm confused by reference and value types. For each
Address object in my collection, when the data from an Address object is
inserted into the database an AddressID will be returned in an output
parameter. I want to populate the AddressID property of the Address object
with that ID.

Thank you very much for your help.

~ Mike G.


"Jon Skeet [C# MVP]" wrote:
[color=blue]
> Mike Guerrieri <MikeGuerrieri@discussions.microsoft.com> wrote:[color=green]
> > Thanks for the fast reply Scott. The contactAddress is not marked ReadOnly,
> > here is the section where it is created...
> >
> > dtaContactAddress contactAddressData = new dtaContactAddress();
> > foreach (ContactAddress contactAddress in contact.AddressCollection)
> > {
> > contactAddress.ContactId = contactID;
> > contactAddressData.Insert(ref contactAddress);
> > }
> >
> > Could it be that objects in a collection are implicitly ReadOnly?[/color]
>
> Objects themselves aren't readonly. However, the *variable*
> contactAddress is readonly - the variable used in a foreach is always
> implicitly readonly.
>
> It doesn't look to me like you need to pass contactAddress by reference
> though - that would just allow Insert to change the value of
> contactAddress, which itself is a reference.
>
> I *suspect* you're a bit confused about reference types vs value types
> and parameter passing.
> See http://www.pobox.com/~skeet/csharp/parameters.html
>
> --
> Jon Skeet - <skeet@pobox.com>
> http://www.pobox.com/~skeet
> If replying to the group, please do not mail me too
>[/color]
Patrice
Guest
 
Posts: n/a
#6: Jul 22 '05

re: Passing an Object by Ref


This is not a problem. Even objects passed byval are updatable (you just
allows to update or not *this* pointer).

Patrice

--

"Mike Guerrieri" <MikeGuerrieri@discussions.microsoft.com> a écrit dans le
message de news:D0F5FA17-242F-4AA5-8B65-B2E65A0224ED@microsoft.com...[color=blue]
> Thank you Jon, I didn't realize that the variable used in a foreach is[/color]
always[color=blue]
> implicitly readonly.
>
> I don't believe that I'm confused by reference and value types. For each
> Address object in my collection, when the data from an Address object is
> inserted into the database an AddressID will be returned in an output
> parameter. I want to populate the AddressID property of the Address object
> with that ID.
>
> Thank you very much for your help.
>
> ~ Mike G.
>
>
> "Jon Skeet [C# MVP]" wrote:
>[color=green]
> > Mike Guerrieri <MikeGuerrieri@discussions.microsoft.com> wrote:[color=darkred]
> > > Thanks for the fast reply Scott. The contactAddress is not marked[/color][/color][/color]
ReadOnly,[color=blue][color=green][color=darkred]
> > > here is the section where it is created...
> > >
> > > dtaContactAddress contactAddressData = new dtaContactAddress();
> > > foreach (ContactAddress contactAddress in contact.AddressCollection)
> > > {
> > > contactAddress.ContactId = contactID;
> > > contactAddressData.Insert(ref contactAddress);
> > > }
> > >
> > > Could it be that objects in a collection are implicitly ReadOnly?[/color]
> >
> > Objects themselves aren't readonly. However, the *variable*
> > contactAddress is readonly - the variable used in a foreach is always
> > implicitly readonly.
> >
> > It doesn't look to me like you need to pass contactAddress by reference
> > though - that would just allow Insert to change the value of
> > contactAddress, which itself is a reference.
> >
> > I *suspect* you're a bit confused about reference types vs value types
> > and parameter passing.
> > See http://www.pobox.com/~skeet/csharp/parameters.html
> >
> > --
> > Jon Skeet - <skeet@pobox.com>
> > http://www.pobox.com/~skeet
> > If replying to the group, please do not mail me too
> >[/color][/color]


Jon Skeet [C# MVP]
Guest
 
Posts: n/a
#7: Jul 22 '05

re: Passing an Object by Ref


Mike Guerrieri <MikeGuerrieri@discussions.microsoft.com> wrote:[color=blue]
> Thank you Jon, I didn't realize that the variable used in a foreach is always
> implicitly readonly.
>
> I don't believe that I'm confused by reference and value types. For each
> Address object in my collection, when the data from an Address object is
> inserted into the database an AddressID will be returned in an output
> parameter. I want to populate the AddressID property of the Address object
> with that ID.[/color]

And you don't need to pass anything by reference in order to do that.
Pass the reference to an Address object (passing that reference by
value) and update it in your method.

Put it this way: passing a variable by reference when you then make no
use of the potentially changed value of the variable should always
raise alarm bells - there's no (semantic) point in passing the variable
by reference in that case.

See the article I referred to in my last post.

--
Jon Skeet - <skeet@pobox.com>
http://www.pobox.com/~skeet
If replying to the group, please do not mail me too
Closed Thread


Similar .NET Framework bytes