473,320 Members | 1,863 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,320 software developers and data experts.

ByVal TreeView Modifies Original?

I am working on a small project that uses the treeview control in .net
2003. I have a tree view that I am sending to a sub in order to
iterate through the nodes.

Public Sub test (ByVal inTreeView as Tree View)

But, the thing I want to do with the inTreeView requires me to expand
all the nodes before I iterate. The problem is that when the sub is
complete, the original tree view on my form ends up with all of the
nodes expanded I had hoped that sending it to my sub "ByVal" would
make a copy that I could mess around with without effecting the
original. I also tried creating a new treeview and assigning the
inTreeView to it, then doing my stuff:

dim TestTree as new TreeView
TestTree = inTreeView

but no matter what the source treeview always gets expanded when I do
an expandall TestTree.ExpandAll()

Can anyone tell me how to get a duplicate of a treeview in a way that
I can expandall and not effect the source object?

Thanks
cas

Mar 18 '07 #1
2 1524
cas wrote:
<backposted/>

I suppose you know that you have value and reference types in VB (or,
actually, in .Net). A reference type, which the TreeView happens to
be, must allocate its data in a special memory area called "heap", and
live there until it's reclaimed by a memory management code called
garbage collector.

Well, reference types are represented by *pointers*, that is, a value
that *references* their address in the heap.

When you declare a variable of a reference type, such as a treeview,
you're declaring a placeholder for that "pointer", just that. When
that variable is assigned a value, it "points to" the address of the
actual data that lives in the heap.

The thing with reference types is that you can pass their pointers
around, and each variable will be pointing to (or referencing) the
same location in memory, the same object.

Then, when you pass a variable of a reference type ByVal, you create a
copy *of the pointer* and assigns to a temporary variable. What this
does is to protect your original *variable* (not the original object,
which lives far away, in the heap) from being modified. This prevents,
for instance, the called code from assigning, say, Nothing to your
variable, making it completely unusable in its original context. Or
from assignin' it the pointer to a completely different object, which
might create a complete confusion in your code (if it wasn't what you
expected).

The thing to be aware of is, as you could see, even though a variable
of a reference type passed ByVal is protected, the object it points to
is not. Anything in the object can be modified by the called code,
because just a copy of the reference was passed, not a copy of the
object...

Notice that when you create a variable with "New", as you did with
your TestTree in your attempt to create a *damn new tree view*, you
*are* creating a new object in the heap. And the address of that new
object is being assigned to your variable. But then, when you assign
the value of your original treeview to TestTree, guess what
happened... the pointer it had to the new treeview was replaced by the
pointer to the old one, just nullifying your efforts (and leaving an
unreferenced object in the heap, ready to be collected by the garbage
collector in its next scan).

What you'd want is some sort of clone method, which I don't suppose
the treeview exposes...

Sorry for the bad news.

Regards,

Branco.
I am working on a small project that uses the treeview control in .net
2003. I have a tree view that I am sending to a sub in order to
iterate through the nodes.

Public Sub test (ByVal inTreeView as Tree View)

But, the thing I want to do with the inTreeView requires me to expand
all the nodes before I iterate. The problem is that when the sub is
complete, the original tree view on my form ends up with all of the
nodes expanded I had hoped that sending it to my sub "ByVal" would
make a copy that I could mess around with without effecting the
original. I also tried creating a new treeview and assigning the
inTreeView to it, then doing my stuff:

dim TestTree as new TreeView
TestTree = inTreeView

but no matter what the source treeview always gets expanded when I do
an expandall TestTree.ExpandAll()

Can anyone tell me how to get a duplicate of a treeview in a way that
I can expandall and not effect the source object?

Thanks
cas

Mar 18 '07 #2
On Mar 18, 7:20 pm, "Branco Medeiros" <branco.medei...@gmail.com>
wrote:
cas wrote:

<backposted/>

I suppose you know that you have value and reference types in VB (or,
actually, in .Net). A reference type, which the TreeView happens to
be, must allocate its data in a special memory area called "heap", and
live there until it's reclaimed by a memory management code called
garbage collector.

Well, reference types are represented by *pointers*, that is, a value
that *references* their address in the heap.

When you declare a variable of a reference type, such as a treeview,
you're declaring a placeholder for that "pointer", just that. When
that variable is assigned a value, it "points to" the address of the
actual data that lives in the heap.

The thing with reference types is that you can pass their pointers
around, and each variable will be pointing to (or referencing) the
same location in memory, the same object.

Then, when you pass a variable of a reference type ByVal, you create a
copy *of the pointer* and assigns to a temporary variable. What this
does is to protect your original *variable* (not the original object,
which lives far away, in the heap) from being modified. This prevents,
for instance, the called code from assigning, say, Nothing to your
variable, making it completely unusable in its original context. Or
from assignin' it the pointer to a completely different object, which
might create a complete confusion in your code (if it wasn't what you
expected).

The thing to be aware of is, as you could see, even though a variable
of a reference type passed ByVal is protected, the object it points to
is not. Anything in the object can be modified by the called code,
because just a copy of the reference was passed, not a copy of the
object...

Notice that when you create a variable with "New", as you did with
your TestTree in your attempt to create a *damn new tree view*, you
*are* creating a new object in the heap. And the address of that new
object is being assigned to your variable. But then, when you assign
the value of your original treeview to TestTree, guess what
happened... the pointer it had to the new treeview was replaced by the
pointer to the old one, just nullifying your efforts (and leaving an
unreferenced object in the heap, ready to be collected by the garbage
collector in its next scan).

What you'd want is some sort of clone method, which I don't suppose
the treeview exposes...

Sorry for the bad news.

Regards,

Branco.
I am working on a small project that uses the treeview control in .net
2003. I have a tree view that I am sending to a sub in order to
iterate through the nodes.
Public Sub test (ByVal inTreeView as Tree View)
But, the thing I want to do with the inTreeView requires me to expand
all the nodes before I iterate. The problem is that when the sub is
complete, the original tree view on my form ends up with all of the
nodes expanded I had hoped that sending it to my sub "ByVal" would
make a copy that I could mess around with without effecting the
original. I also tried creating a new treeview and assigning the
inTreeView to it, then doing my stuff:
dim TestTree as new TreeView
TestTree = inTreeView
but no matter what the source treeview always gets expanded when I do
an expandall TestTree.ExpandAll()
Can anyone tell me how to get a duplicate of a treeview in a way that
I can expandall and not effect the source object?
Thanks
cas
Ok, thank you for your reply :)

Mar 19 '07 #3

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

Similar topics

42
by: lauren quantrell | last post by:
So many postings on not to use the treeview control, but nothing recently. Is it safe to swim there yet with Access 2000-Access 2003?
3
by: Steve Richter | last post by:
in windows explorer, the nodes immed under the "my computer" root node appear with a minimum of indenting ( the +/- square is directly underneath the root node ). In the .NET TreeView control the...
0
by: | last post by:
This is ugly, but.... If you switch the TreeView's font to a fixed-space (i.e., NOT proportional) font, such as Courier New, your original work-around of adding spaces as appropriate will work....
11
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...
1
by: musosdev | last post by:
Hi guys I was using the Microsoft.Web.UI.WebControls extra namespace in my application to provide me with a TreeView control. I was using specifically ..GetNodeFromIndex(), SelectedNodeIndex and...
2
by: dixiecanterbury | last post by:
I have a tree view on a form and I need to pass that treeview to another form keeping the state of the treeview (expanded nodes, etc). After the treeview has been manipulated (added nodes,...
1
by: Victor Rodriguez | last post by:
Is there a way that I can have a client side event like oncontextmenu="showfunction();" on each node? thanks, Victor
0
by: drop | last post by:
Hi, I am using a Treeview and I'd like to know what is the best way to refresh the children of a specific node in the treeview. I already do that using full postback, but this sends back the...
2
by: casManG | last post by:
I am working on a small project that uses the treeview control in vb.net 2003. I have a tree view that I am sending to a sub in order to iterate through the nodes. Public Sub test (ByVal...
0
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...
0
isladogs
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...
1
isladogs
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...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
0
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...
0
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
1
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
0
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 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.