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. 6 2166
It appears that you have a property called: contactAddress that is marked
as ReadOnly.
"Mike Guerrieri" <Mike Gu*******@discussions.microsoft.com> wrote in message
news:46**********************************@microsof t.com... 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.
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: It appears that you have a property called: contactAddress that is marked as ReadOnly.
"Mike Guerrieri" <Mike Gu*******@discussions.microsoft.com> wrote in message news:46**********************************@microsof t.com...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.
Mike Guerrieri <Mi***********@discussions.microsoft.com> wrote: 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?
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 - <sk***@pobox.com> http://www.pobox.com/~skeet
If replying to the group, please do not mail me too
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: Mike Guerrieri <Mi***********@discussions.microsoft.com> wrote: 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?
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 - <sk***@pobox.com> http://www.pobox.com/~skeet If replying to the group, please do not mail me too
This is not a problem. Even objects passed byval are updatable (you just
allows to update or not *this* pointer).
Patrice
--
"Mike Guerrieri" <Mi***********@discussions.microsoft.com> a écrit dans le
message de news:D0**********************************@microsof t.com... 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:
Mike Guerrieri <Mi***********@discussions.microsoft.com> wrote: 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?
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 - <sk***@pobox.com> http://www.pobox.com/~skeet If replying to the group, please do not mail me too
Mike Guerrieri <Mi***********@discussions.microsoft.com> wrote: 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.
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 - <sk***@pobox.com> http://www.pobox.com/~skeet
If replying to the group, please do not mail me too This thread has been closed and replies have been disabled. Please start a new discussion. Similar topics
by: domeceo |
last post by:
can anyone tell me why I cannot pass values in a setTimeout function
whenever I use this function it says "menu is undefined" after th
alert.
function imgOff(menu, num) {
if (document.images) {...
|
by: jr |
last post by:
Sorry for this very dumb question, but I've clearly got a long way to go!
Can someone please help me pass an array into a function. Here's a starting
point.
void TheMainFunc()
{
// Body of...
|
by: Dennis Myrén |
last post by:
I have these tiny classes, implementing an interface through which their
method
Render ( CosWriter writer ) ;
is called.
Given a specific context, there are potentially a lot of such objects,...
|
by: LP |
last post by:
Hello,
Here's the scenario:
Object A opens a Sql Db connection to execute number of SqlCommands. Then it
needs to pass this connection to a constructor of object B which in turn
executes more...
|
by: Scott Zabolotzky |
last post by:
I'm trying to pass a custom object back and forth between forms.
This custom object is pulled into the app using an external reference
to an assembly DLL that was given to me by a co-worker. A...
|
by: John Pass |
last post by:
Hi,
In the attached example, I do understand that the references are not changed
if an array is passed by Val. What I do not understand is the result of line
99 (If one can find this by line...
|
by: MSDNAndi |
last post by:
Hi,
I get the following warning:
"Possibly incorrect assignment to local 'oLockObject' which is the argument
to a using or lock statement. The Dispose call or unlocking will happen on
the...
|
by: Andrew Bullock |
last post by:
Hi,
I have two classes, A and B,
B takes an A as an argument in its constructor:
A a1 = new A();
B b = new B(a1);
|
by: TS |
last post by:
I was under the assumption that if you pass an object as a param to a method
and inside that method this object is changed, the object will stay changed
when returned from the method because the...
|
by: Deckarep |
last post by:
Hello fellow C# programmers,
This question is more about general practice and convention so here
goes:
I got into a discussion with a co-worker who insisted that as a general
practice all...
|
by: DolphinDB |
last post by:
Tired of spending countless mintues downsampling your data? Look no further!
In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
|
by: isladogs |
last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM).
In this month's session, we are pleased to welcome back...
|
by: Vimpel783 |
last post by:
Hello!
Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
|
by: ArrayDB |
last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
|
by: CloudSolutions |
last post by:
Introduction:
For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
|
by: Defcon1945 |
last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
|
by: af34tf |
last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you
|
by: Faith0G |
last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
|
by: isladogs |
last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 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 former...
| |