473,587 Members | 2,473 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Beginner tag question

Hi all,

I've recently discovered the joys of tag :) I have a ListView which is
populated with strings from a List<>, and each ListView item is linked to
the relevant object in List<> using Tag.
The ListView is on a parent form, and I have a child form which adds items
to List<>. So I pass the List<> by ref*, the relevant objects are created in
the List<> and the ListView is updated in the parent form.
I'm now trying to edit items in the ListView. If an item is selected, then
ListView.Select edItems[0].Tag is used to get the object to edit. I need this
object passed to the child form, repopulating the relevant fields to allow
for editing. I can't seem to pass the object in
ListView.Select edItems[0].Tag, but I if I create a temp object, assign
ListView.Select edItems[0].Tag to this, pass this object to the child form,
and assign it back to ListView.Select edItems[0].Tag it seems to work e.g.:

if (lvMainList.Sel ectedItems[0].Tag is Journal)
{
Journal temp =
((Journal)lvMai nList.SelectedI tems[0].Tag);
JournalEntryFor m journalEntry
= new JournalEntryFor m(ref temp);

lvMainList.Sele ctedItems[0].Tag = temp;

journalEntry.Sh owDialog();

journalEntry.Di spose();
}

Is this an ok way to do this, or is there a more appropriate way? Also, what
happens to the old journal object when I reassign temp to the same Tag? Is
it garbage collected? Or does this waste memory?

Sorry for all the (perhaps obvious) questions! Any help would be greatly
appreciated :)

Chris

*as the list is ref type, is it passed by ref by anyway? I used the ref
keywords to be explicit - is this overkill?
Jan 3 '06 #1
6 1434
The problem is that you are passing by "ref". First, an explanation of
why you can't pass Tag by "ref", then an explanation of why you
shouldn't be using "ref".

Tag is a property of ListViewItem. That means that there (may be / is)
code in the get and set methods of the property: the property may not
map directly onto a field inside the class. In this case, it probably
does, but in the general case, a property may not even _have_ a field
behind it: it may be calculated from other fields, and setting it may
set multiple other fields. Given this, there's really no way to pass a
"pointer to" a property, because the property may not represent a
specific physical thing within the class.

You got around this by assigning the property's value to a temporary
variable, then passing that variable by "ref" (which works, because the
variable has a location on the stack, so you can "ref"er to that
location), the setting the property's value back to the value of your
temporary variable.

However, you don't need to do any of this. I think that you
misunderstand how things are passed around in .NET, and when you need
"ref". Jon Skeet has an excellent article here:

http://www.yoda.arachsys.com/csharp/parameters.html

The bottom line, in your case, is that you need to pass your List<> by
"ref" _only if you want to change it out for a whole new list_. You
need to pass the object that Tag refers to by "ref" _only if you want
to replace the whole object instance with a new instance_.

If you just want to manipulate the List<>, or change properties of the
object that Tag refers to, then you don't need "ref" at all. When you
pass a reference type (like a List<>) to a method, what is passed is a
reference to that object, so the method that you call will be working
with exactly the same object as the caller is. There is no copying
going on, so any changes that the method makes to the object (whether
it be a list or an individual object) will still be there when the
method returns.

You can read Jon's article for a more in-depth explanation.

As for your last two questions:

"What happens to the old journal object when I reassign temp to the
same Tag? Is it garbage collected? Or does this waste memory?"

Well, since the journal object that you originally got from the Tag
property is the same one you're assigning back to the Tag property,
there is no garbage collection involved at all.

Even if there were, I doubt very much that it would be enough to cause
any worry.

"As the list is ref type, is it passed by ref by anyway?"

Well, a reference to it is passed by value (see Jon's article). So it's
sort of passed by reference, in a way. (He says, ducking Jon's swing.)

"I used the ref keywords to be explicit - is this overkill?"

Yes, because adding "ref" means something else: it means that you want
to give the method you're calling the option to replace the object
instance with a whole new instance, in addition to just modifying
properties of the object. So, in your case, it's allowing the method to
replace the list of journals with a whole different list of journals.
If I'm correct in my understanding that you just want the method to be
able to change the contents of the list (but still keep the same list
object) then yes, it's overkill for what you want.

In a very few situations it can be useful, though.

Jan 3 '06 #2
Hi Bruce,
Thankyou for the detailed post and for the great article. I understand what
I'm doing now, and what I was doing wrong :)
Out of curiosity, what few situations could it be useful in?

Regards,

Chris

"Bruce Wood" <br*******@cana da.com> wrote in message
news:11******** **************@ g44g2000cwa.goo glegroups.com.. .
The problem is that you are passing by "ref". First, an explanation of
why you can't pass Tag by "ref", then an explanation of why you
shouldn't be using "ref".

Tag is a property of ListViewItem. That means that there (may be / is)
code in the get and set methods of the property: the property may not
map directly onto a field inside the class. In this case, it probably
does, but in the general case, a property may not even _have_ a field
behind it: it may be calculated from other fields, and setting it may
set multiple other fields. Given this, there's really no way to pass a
"pointer to" a property, because the property may not represent a
specific physical thing within the class.

You got around this by assigning the property's value to a temporary
variable, then passing that variable by "ref" (which works, because the
variable has a location on the stack, so you can "ref"er to that
location), the setting the property's value back to the value of your
temporary variable.

However, you don't need to do any of this. I think that you
misunderstand how things are passed around in .NET, and when you need
"ref". Jon Skeet has an excellent article here:

http://www.yoda.arachsys.com/csharp/parameters.html

The bottom line, in your case, is that you need to pass your List<> by
"ref" _only if you want to change it out for a whole new list_. You
need to pass the object that Tag refers to by "ref" _only if you want
to replace the whole object instance with a new instance_.

If you just want to manipulate the List<>, or change properties of the
object that Tag refers to, then you don't need "ref" at all. When you
pass a reference type (like a List<>) to a method, what is passed is a
reference to that object, so the method that you call will be working
with exactly the same object as the caller is. There is no copying
going on, so any changes that the method makes to the object (whether
it be a list or an individual object) will still be there when the
method returns.

You can read Jon's article for a more in-depth explanation.

As for your last two questions:

"What happens to the old journal object when I reassign temp to the
same Tag? Is it garbage collected? Or does this waste memory?"

Well, since the journal object that you originally got from the Tag
property is the same one you're assigning back to the Tag property,
there is no garbage collection involved at all.

Even if there were, I doubt very much that it would be enough to cause
any worry.

"As the list is ref type, is it passed by ref by anyway?"

Well, a reference to it is passed by value (see Jon's article). So it's
sort of passed by reference, in a way. (He says, ducking Jon's swing.)

"I used the ref keywords to be explicit - is this overkill?"

Yes, because adding "ref" means something else: it means that you want
to give the method you're calling the option to replace the object
instance with a whole new instance, in addition to just modifying
properties of the object. So, in your case, it's allowing the method to
replace the list of journals with a whole different list of journals.
If I'm correct in my understanding that you just want the method to be
able to change the contents of the list (but still keep the same list
object) then yes, it's overkill for what you want.

In a very few situations it can be useful, though.

Jan 3 '06 #3
Chris <mi*********@gm ail.com> wrote:
Thankyou for the detailed post and for the great article. I understand what
I'm doing now, and what I was doing wrong :)
Out of curiosity, what few situations could it be useful in?


In a few situations, it genuinely makes sense for a method to return
two values. In those situations, an out parameter is often used. For
instance, Double.TryParse attempts to parse a string and "return" a
value in the out parameter, but may fail and return false (setting the
out parameter to 0) if the parsing fails.

In some other situations, it makes sense to pass something in and
potentially change it. In my experience this is somewhat rarer, and I
can't immediately think of any good examples - where possible, I'd
suggest returning the new value rather than having a ref parameter. If
the method already returns something else, of course, that's when it
might make sense.

--
Jon Skeet - <sk***@pobox.co m>
http://www.pobox.com/~skeet Blog: http://www.msmvps.com/jon.skeet
If replying to the group, please do not mail me too
Jan 3 '06 #4
I just searched my entire .NET project base, and found three places in
which I use "ref". These fall into the following two categories:

1. A method is being called repeatedly (or recursively) and needs to
maintain some sort of "state" just for that method. For example, I have
a pair of mutually recursive methods that build a predicate (one that
builds OR relations and one that builds AND relations) and they need to
maintain a count of how many terms have already been added to the
predicate as they walk up and down the expression tree. True, I could
have created a whole separate class to do this and made the two "ref"
parameters into object state, but I was lazy and used the good ol'
procedural programming method. So, I guess this situation doesn't
really count because it was just sloth on my part.

2. A method needs to add some stuff to an array. This is very rare in
..NET as it's usually better to use ArrayList (or even better, in
VS2005, List<T>), but in one case, for particular reasons, I have a
method that adds more stuff to the end of an array. Since arrays can't
be expanded, I have to give the method the option to replace the array
with a bigger one.

Jan 3 '06 #5
I've got a question which relates to this.

I have a form which displays a lists items like a database record - it's
navigation looks like a bindingNavigato r control.
The idea is to have a quick browser of all items data, with the added
functionality of being able to view specific record types or all.
To allow a textbox to show "x (of y)" x being the current record and y being
the total number of records, do I need another list to store specific
journals in?
When I display all journals I could just use the list being passed in. But
if the user selects a specific type, I loop through the passed list and any
journals of that type are added to a tempList, and the contents of this list
displayed. Am I going about this the wrong way? It's the only way I could
think of to preserve an index as described above. By copying the entries of
one list to another, I'm making full copies of those items or am I making a
reference to the entries of the other list? This is slightly confusing me.
If I am making full copies, is there any way to pass items of one list to
another as references, so the new list items point to the relevant index of
the old list? If not, is there any easier way to do what I'm doing without
using lots of memory?

Chris

"Chris" <mi*********@gm ail.com> wrote in message
news:x6******** ***********@fe2 .news.blueyonde r.co.uk...
Hi Bruce,
Thankyou for the detailed post and for the great article. I understand
what I'm doing now, and what I was doing wrong :)
Out of curiosity, what few situations could it be useful in?

Regards,

Chris

"Bruce Wood" <br*******@cana da.com> wrote in message
news:11******** **************@ g44g2000cwa.goo glegroups.com.. .
The problem is that you are passing by "ref". First, an explanation of
why you can't pass Tag by "ref", then an explanation of why you
shouldn't be using "ref".

Tag is a property of ListViewItem. That means that there (may be / is)
code in the get and set methods of the property: the property may not
map directly onto a field inside the class. In this case, it probably
does, but in the general case, a property may not even _have_ a field
behind it: it may be calculated from other fields, and setting it may
set multiple other fields. Given this, there's really no way to pass a
"pointer to" a property, because the property may not represent a
specific physical thing within the class.

You got around this by assigning the property's value to a temporary
variable, then passing that variable by "ref" (which works, because the
variable has a location on the stack, so you can "ref"er to that
location), the setting the property's value back to the value of your
temporary variable.

However, you don't need to do any of this. I think that you
misunderstand how things are passed around in .NET, and when you need
"ref". Jon Skeet has an excellent article here:

http://www.yoda.arachsys.com/csharp/parameters.html

The bottom line, in your case, is that you need to pass your List<> by
"ref" _only if you want to change it out for a whole new list_. You
need to pass the object that Tag refers to by "ref" _only if you want
to replace the whole object instance with a new instance_.

If you just want to manipulate the List<>, or change properties of the
object that Tag refers to, then you don't need "ref" at all. When you
pass a reference type (like a List<>) to a method, what is passed is a
reference to that object, so the method that you call will be working
with exactly the same object as the caller is. There is no copying
going on, so any changes that the method makes to the object (whether
it be a list or an individual object) will still be there when the
method returns.

You can read Jon's article for a more in-depth explanation.

As for your last two questions:

"What happens to the old journal object when I reassign temp to the
same Tag? Is it garbage collected? Or does this waste memory?"

Well, since the journal object that you originally got from the Tag
property is the same one you're assigning back to the Tag property,
there is no garbage collection involved at all.

Even if there were, I doubt very much that it would be enough to cause
any worry.

"As the list is ref type, is it passed by ref by anyway?"

Well, a reference to it is passed by value (see Jon's article). So it's
sort of passed by reference, in a way. (He says, ducking Jon's swing.)

"I used the ref keywords to be explicit - is this overkill?"

Yes, because adding "ref" means something else: it means that you want
to give the method you're calling the option to replace the object
instance with a whole new instance, in addition to just modifying
properties of the object. So, in your case, it's allowing the method to
replace the list of journals with a whole different list of journals.
If I'm correct in my understanding that you just want the method to be
able to change the contents of the list (but still keep the same list
object) then yes, it's overkill for what you want.

In a very few situations it can be useful, though.


Jan 6 '06 #6
I think I answered this one for myself with a little playing around :).

Chris
"Chris" <mi*********@gm ail.com> wrote in message
news:ko******** ***********@fe2 .news.blueyonde r.co.uk...
I've got a question which relates to this.

I have a form which displays a lists items like a database record - it's
navigation looks like a bindingNavigato r control.
The idea is to have a quick browser of all items data, with the added
functionality of being able to view specific record types or all.
To allow a textbox to show "x (of y)" x being the current record and y
being the total number of records, do I need another list to store
specific journals in?
When I display all journals I could just use the list being passed in. But
if the user selects a specific type, I loop through the passed list and
any journals of that type are added to a tempList, and the contents of
this list displayed. Am I going about this the wrong way? It's the only
way I could think of to preserve an index as described above. By copying
the entries of one list to another, I'm making full copies of those items
or am I making a reference to the entries of the other list? This is
slightly confusing me. If I am making full copies, is there any way to
pass items of one list to another as references, so the new list items
point to the relevant index of the old list? If not, is there any easier
way to do what I'm doing without using lots of memory?

Chris

"Chris" <mi*********@gm ail.com> wrote in message
news:x6******** ***********@fe2 .news.blueyonde r.co.uk...
Hi Bruce,
Thankyou for the detailed post and for the great article. I understand
what I'm doing now, and what I was doing wrong :)
Out of curiosity, what few situations could it be useful in?

Regards,

Chris

"Bruce Wood" <br*******@cana da.com> wrote in message
news:11******** **************@ g44g2000cwa.goo glegroups.com.. .
The problem is that you are passing by "ref". First, an explanation of
why you can't pass Tag by "ref", then an explanation of why you
shouldn't be using "ref".

Tag is a property of ListViewItem. That means that there (may be / is)
code in the get and set methods of the property: the property may not
map directly onto a field inside the class. In this case, it probably
does, but in the general case, a property may not even _have_ a field
behind it: it may be calculated from other fields, and setting it may
set multiple other fields. Given this, there's really no way to pass a
"pointer to" a property, because the property may not represent a
specific physical thing within the class.

You got around this by assigning the property's value to a temporary
variable, then passing that variable by "ref" (which works, because the
variable has a location on the stack, so you can "ref"er to that
location), the setting the property's value back to the value of your
temporary variable.

However, you don't need to do any of this. I think that you
misunderstand how things are passed around in .NET, and when you need
"ref". Jon Skeet has an excellent article here:

http://www.yoda.arachsys.com/csharp/parameters.html

The bottom line, in your case, is that you need to pass your List<> by
"ref" _only if you want to change it out for a whole new list_. You
need to pass the object that Tag refers to by "ref" _only if you want
to replace the whole object instance with a new instance_.

If you just want to manipulate the List<>, or change properties of the
object that Tag refers to, then you don't need "ref" at all. When you
pass a reference type (like a List<>) to a method, what is passed is a
reference to that object, so the method that you call will be working
with exactly the same object as the caller is. There is no copying
going on, so any changes that the method makes to the object (whether
it be a list or an individual object) will still be there when the
method returns.

You can read Jon's article for a more in-depth explanation.

As for your last two questions:

"What happens to the old journal object when I reassign temp to the
same Tag? Is it garbage collected? Or does this waste memory?"

Well, since the journal object that you originally got from the Tag
property is the same one you're assigning back to the Tag property,
there is no garbage collection involved at all.

Even if there were, I doubt very much that it would be enough to cause
any worry.

"As the list is ref type, is it passed by ref by anyway?"

Well, a reference to it is passed by value (see Jon's article). So it's
sort of passed by reference, in a way. (He says, ducking Jon's swing.)

"I used the ref keywords to be explicit - is this overkill?"

Yes, because adding "ref" means something else: it means that you want
to give the method you're calling the option to replace the object
instance with a whole new instance, in addition to just modifying
properties of the object. So, in your case, it's allowing the method to
replace the list of journals with a whole different list of journals.
If I'm correct in my understanding that you just want the method to be
able to change the contents of the list (but still keep the same list
object) then yes, it's overkill for what you want.

In a very few situations it can be useful, though.



Jan 6 '06 #7

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

Similar topics

11
2547
by: Svens | last post by:
Hey everyone! I'm a math student working on a short script involving logs. I have a function on my scientific calculator, and was wondering if there was a similar funtion in python. For example: (log65536)/(log4)= 8 I've searched around a bit and haven't been able to find anything.
3
2974
by: jvax | last post by:
Hi all, I hope I'm posting in the right NG... I have a data text file I want to read from a c++ program. the data file goes like this: 90 # number of balls 33 42 13
1
2603
by: Mike Malter | last post by:
I am just starting to work with reflection and I want to create a log that saves relevant information if a method call fails so I can call that method again later using reflection. I am experimenting a bit with what I need to do this and have the following code snippet. But first if I pass the assembly name and type to...
14
2284
by: z_learning_tester | last post by:
But I can't seem to find the answer. The question is how do you reverse the words in a string? Or how do you reverse the numbers listed in a string? The example is usually something like: Turn this string "1,2,3,4,..." Into "...4,3,2,1" This one seems hard enough let alone trying to turn a string of space-seperated words around(is that...
12
1876
by: Blaze | last post by:
I am doing the first walk through on the Visual Studio .Net walkthrough book to learn a little about programming. I am having issues with the first tutorial not running correctly. It seems that the build fails with what the book tells me to do. Specifically, I am doing this: public authors1 GetAuthors() { authors1 authors = new...
5
2228
by: optimistx | last post by:
As a beginner in javascript I had a question. I was reading FAQ and posts here. I became very unhappy: Obviously this group is mainly for wise, pedantic, unkind etc people, who already know everything about javascript, and want to prove that to everyone in a very harsh way? Therefore, I skip the question, and we can go directly to the...
10
4441
by: Roman Zeilinger | last post by:
Hi I have a beginner question concerning fscanf. First I had a text file which just contained some hex numbers: 0C100012 0C100012 ....
4
1534
by: a | last post by:
Dear all vb.net developer I want to know the time I need to master vb.net? I'm beginner
3
2033
by: Ben Keshet | last post by:
I have a probably simple beginner's question - I have a script that I am currently able to print its output. instead, i want to write it into a file - I tried different versions of write() but might have gotten the syntax wrong. the variable I want to write is a line from a file I am reading: "... f = open('receptor.mol2', 'r') line =...
2
1840
by: roanhn | last post by:
Hello. I've to to write a master's thesis. Currently I deal with php, mysql, ajax. Fate decreed that I've to choose one of this subjects: 1.gdi+ library in .net technology 2.ado.net technology in VS 2008. I didn't have contact with Visual Studio, .NET. I only know some basics of c++. My question is what subject of your point of view...
0
8340
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that...
1
7967
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For...
0
8220
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 protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the...
1
5713
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 presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes...
0
5392
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert...
0
3840
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in...
0
3875
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
1452
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
0
1185
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating...

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.