Converting a string into a method
-
Hi again. I have a peculiar problem that is somewhat reminiscent of vb6's Eval() function. Let's say I have a TreeView populated with an entire harddrive's directory structure, and I want to select the node that contains a folder I am looking for. If I use the .Find method, the search could take forever, and I may end up with multiple places in the TreeView where this name is located. If I already have the full path to the correct folder, then I need a quick way to get to the node without having to search the entire tree. Now, I actually have a quick way of doing this (at least I am confident it will work), but in the process of getting to that solution, I was experimenting with another idea that harks back to that vb6 Eval() function...Sort of. That other idea revolved around selectively expanding a tree branch and testing inside each child node to make sure that the next folder name in my full path could be found there. If so, it would expand that node and continue until all folders in the full path had been exhausted. Theoretically, I would then be at my desired node, which I could then select in the tree. But the problem is that in order to do that, you start at TreeView1.Nodes(0) and do a Find(). Once that node is selected, it gets expanded and you then do a Find() for the second folder, which gets found at Nodes(0).Nodes(x). Moving along in the same fashion, you get to Nodes(0).Nodes(x).Nodes(y), and so on till the task is completed. I think you can see the problem already...Without explicitly setting up Case blocks or If/Else blocks to handle each succession of methods,
Nodes(0) Nodes(0).Nodes(x) Nodes(0).Nodes(x).Nodes(y),...
there is no way for the code to handle this problem. And such a folder could lie at the tenth childnode, maybe even the twentieth, or more. The point being, it is a kludge to try to estimate when you should stop providing hard-coded cases and pray that some scenario doesn't exceed that limit. That is when I was thinking if there was a way to build a string programatically to whatever length I needed it to be, like,"TreeView1.Nodes(0).Nodes(x).Nodes(y).Expand"
and then convert that string into an actual code statement by having the string "evaluated" as such. Is this possible? As I stated above, I think I found another very elegant solution to my TreeView Find() problem which is simple and quick. But I am curious now, if vb.Net has the ability to turn a string into a code statement, or maybe even multiline text into actual v -
Hi again. I have a peculiar problem that is somewhat reminiscent of vb6's Eval() function. Let's say I have a TreeView populated with an entire harddrive's directory structure, and I want to select the node that contains a folder I am looking for. If I use the .Find method, the search could take forever, and I may end up with multiple places in the TreeView where this name is located. If I already have the full path to the correct folder, then I need a quick way to get to the node without having to search the entire tree. Now, I actually have a quick way of doing this (at least I am confident it will work), but in the process of getting to that solution, I was experimenting with another idea that harks back to that vb6 Eval() function...Sort of. That other idea revolved around selectively expanding a tree branch and testing inside each child node to make sure that the next folder name in my full path could be found there. If so, it would expand that node and continue until all folders in the full path had been exhausted. Theoretically, I would then be at my desired node, which I could then select in the tree. But the problem is that in order to do that, you start at TreeView1.Nodes(0) and do a Find(). Once that node is selected, it gets expanded and you then do a Find() for the second folder, which gets found at Nodes(0).Nodes(x). Moving along in the same fashion, you get to Nodes(0).Nodes(x).Nodes(y), and so on till the task is completed. I think you can see the problem already...Without explicitly setting up Case blocks or If/Else blocks to handle each succession of methods,
Nodes(0) Nodes(0).Nodes(x) Nodes(0).Nodes(x).Nodes(y),...
there is no way for the code to handle this problem. And such a folder could lie at the tenth childnode, maybe even the twentieth, or more. The point being, it is a kludge to try to estimate when you should stop providing hard-coded cases and pray that some scenario doesn't exceed that limit. That is when I was thinking if there was a way to build a string programatically to whatever length I needed it to be, like,"TreeView1.Nodes(0).Nodes(x).Nodes(y).Expand"
and then convert that string into an actual code statement by having the string "evaluated" as such. Is this possible? As I stated above, I think I found another very elegant solution to my TreeView Find() problem which is simple and quick. But I am curious now, if vb.Net has the ability to turn a string into a code statement, or maybe even multiline text into actual vI think you need to look at a recursive method. That is, a method that calls itself. Something like: Find( CurrentNode, SubPath ) where the CurrentNode and SubPath are adjusted on each call to itself. You should be able to find boatloads of examples on here, so I won't go into any more detail.
-
Hi again. I have a peculiar problem that is somewhat reminiscent of vb6's Eval() function. Let's say I have a TreeView populated with an entire harddrive's directory structure, and I want to select the node that contains a folder I am looking for. If I use the .Find method, the search could take forever, and I may end up with multiple places in the TreeView where this name is located. If I already have the full path to the correct folder, then I need a quick way to get to the node without having to search the entire tree. Now, I actually have a quick way of doing this (at least I am confident it will work), but in the process of getting to that solution, I was experimenting with another idea that harks back to that vb6 Eval() function...Sort of. That other idea revolved around selectively expanding a tree branch and testing inside each child node to make sure that the next folder name in my full path could be found there. If so, it would expand that node and continue until all folders in the full path had been exhausted. Theoretically, I would then be at my desired node, which I could then select in the tree. But the problem is that in order to do that, you start at TreeView1.Nodes(0) and do a Find(). Once that node is selected, it gets expanded and you then do a Find() for the second folder, which gets found at Nodes(0).Nodes(x). Moving along in the same fashion, you get to Nodes(0).Nodes(x).Nodes(y), and so on till the task is completed. I think you can see the problem already...Without explicitly setting up Case blocks or If/Else blocks to handle each succession of methods,
Nodes(0) Nodes(0).Nodes(x) Nodes(0).Nodes(x).Nodes(y),...
there is no way for the code to handle this problem. And such a folder could lie at the tenth childnode, maybe even the twentieth, or more. The point being, it is a kludge to try to estimate when you should stop providing hard-coded cases and pray that some scenario doesn't exceed that limit. That is when I was thinking if there was a way to build a string programatically to whatever length I needed it to be, like,"TreeView1.Nodes(0).Nodes(x).Nodes(y).Expand"
and then convert that string into an actual code statement by having the string "evaluated" as such. Is this possible? As I stated above, I think I found another very elegant solution to my TreeView Find() problem which is simple and quick. But I am curious now, if vb.Net has the ability to turn a string into a code statement, or maybe even multiline text into actual v -
I think you need to look at a recursive method. That is, a method that calls itself. Something like: Find( CurrentNode, SubPath ) where the CurrentNode and SubPath are adjusted on each call to itself. You should be able to find boatloads of examples on here, so I won't go into any more detail.
I was thinking about recursion, but I could not find a way to get the node depth to "recurse". Until just now maybe (lightbulb). But have to see if it is sound. But I am still curious about my original question...Has someone found a way to turn text into code?
-
dusty_dex wrote:
Is this^ any help?
Hi Dusty_dex. Are you referring to the previous post? If so, I'm checking into it. But I am still curious about my original question...Has someone found a way to turn text into code? In general, it would be so cool and useful for so many things.
-
I was thinking about recursion, but I could not find a way to get the node depth to "recurse". Until just now maybe (lightbulb). But have to see if it is sound. But I am still curious about my original question...Has someone found a way to turn text into code?
You don't need it. Yeah, it's called an IDE and compiler. You have to do about the same thing in your code. Create an instance of the compiler and feed it not a string, not an object graph that represents the code you're creating. I wish it was as easy as a couple lines of code, but no. Far from it. But, doing something like this really is not going to help you to solve your problem.
A guide to posting questions on CodeProject[^]
Dave Kreskowiak -
You don't need it. Yeah, it's called an IDE and compiler. You have to do about the same thing in your code. Create an instance of the compiler and feed it not a string, not an object graph that represents the code you're creating. I wish it was as easy as a couple lines of code, but no. Far from it. But, doing something like this really is not going to help you to solve your problem.
A guide to posting questions on CodeProject[^]
Dave KreskowiakBummer. :( I'm so close to finishing a program I've been working on for a year and the only thing hanging me up is this damn TreeView problem. I just find it so bizarre that MS could not provide some very simple functionality to a TreeView, that I would think would be self-evidently obvious properties and methods to include straight-away. I come up with nifty solutions only to have them dashed by a lack of really basic control methods. So frustrating.
-
I think you need to look at a recursive method. That is, a method that calls itself. Something like: Find( CurrentNode, SubPath ) where the CurrentNode and SubPath are adjusted on each call to itself. You should be able to find boatloads of examples on here, so I won't go into any more detail.
NeverJustHere wrote:
Something like: Find( CurrentNode, SubPath ) where the CurrentNode and SubPath are adjusted on each call to itself.
That is easy in principle, but it does not appear that there is any way to tell vb.Net to restrict a Find() to a starting childnode and a sub path. Without that, recursion is useless. Unless I keep building a new node structure that omits the prior part already searched. If that node structure represents an entire harddrive, that would be slower than watching mountains grow.
-
Hi again. I have a peculiar problem that is somewhat reminiscent of vb6's Eval() function. Let's say I have a TreeView populated with an entire harddrive's directory structure, and I want to select the node that contains a folder I am looking for. If I use the .Find method, the search could take forever, and I may end up with multiple places in the TreeView where this name is located. If I already have the full path to the correct folder, then I need a quick way to get to the node without having to search the entire tree. Now, I actually have a quick way of doing this (at least I am confident it will work), but in the process of getting to that solution, I was experimenting with another idea that harks back to that vb6 Eval() function...Sort of. That other idea revolved around selectively expanding a tree branch and testing inside each child node to make sure that the next folder name in my full path could be found there. If so, it would expand that node and continue until all folders in the full path had been exhausted. Theoretically, I would then be at my desired node, which I could then select in the tree. But the problem is that in order to do that, you start at TreeView1.Nodes(0) and do a Find(). Once that node is selected, it gets expanded and you then do a Find() for the second folder, which gets found at Nodes(0).Nodes(x). Moving along in the same fashion, you get to Nodes(0).Nodes(x).Nodes(y), and so on till the task is completed. I think you can see the problem already...Without explicitly setting up Case blocks or If/Else blocks to handle each succession of methods,
Nodes(0) Nodes(0).Nodes(x) Nodes(0).Nodes(x).Nodes(y),...
there is no way for the code to handle this problem. And such a folder could lie at the tenth childnode, maybe even the twentieth, or more. The point being, it is a kludge to try to estimate when you should stop providing hard-coded cases and pray that some scenario doesn't exceed that limit. That is when I was thinking if there was a way to build a string programatically to whatever length I needed it to be, like,"TreeView1.Nodes(0).Nodes(x).Nodes(y).Expand"
and then convert that string into an actual code statement by having the string "evaluated" as such. Is this possible? As I stated above, I think I found another very elegant solution to my TreeView Find() problem which is simple and quick. But I am curious now, if vb.Net has the ability to turn a string into a code statement, or maybe even multiline text into actual vtreddie wrote:
But I am curious now, if vb.Net has the ability to turn a string into a code statement, or maybe even multiline text into actual vb.Net code?
There's various ways to compile code (from your app) and execute it. I don't think it'd be very helpful in finding a TreeNode though.
treddie wrote:
If I already have the full path to the correct folder, then I need a quick way to get to the node without having to search the entire tree.
Start at the left of the folder-string. Find the item in the root-node. Open that node. Find the second part of the path in the current node's children. Repeat until the path is empty.
Bastard Programmer from Hell :suss: If you can't read my code, try converting it here[^]
-
dusty_dex wrote:
Is this^ any help?
Hi Dusty_dex. Are you referring to the previous post? If so, I'm checking into it. But I am still curious about my original question...Has someone found a way to turn text into code? In general, it would be so cool and useful for so many things.
treddie wrote:
But I am still curious about my original question...Has someone found a way to turn text into code?
66-666-##-11-22-88-8-##-999-666-88-##-222-2-66-##-4-33-88-##-7777-666-6-33-##-444-66-8-33-777-33-7777-8-444-66-4-##444-3-33-2-7777-11111 :-D
-
treddie wrote:
But I am still curious about my original question...Has someone found a way to turn text into code?
66-666-##-11-22-88-8-##-999-666-88-##-222-2-66-##-4-33-88-##-7777-666-6-33-##-444-66-8-33-777-33-7777-8-444-66-4-##444-3-33-2-7777-11111 :-D
-
treddie wrote:
But I am curious now, if vb.Net has the ability to turn a string into a code statement, or maybe even multiline text into actual vb.Net code?
There's various ways to compile code (from your app) and execute it. I don't think it'd be very helpful in finding a TreeNode though.
treddie wrote:
If I already have the full path to the correct folder, then I need a quick way to get to the node without having to search the entire tree.
Start at the left of the folder-string. Find the item in the root-node. Open that node. Find the second part of the path in the current node's children. Repeat until the path is empty.
Bastard Programmer from Hell :suss: If you can't read my code, try converting it here[^]
That is where the frustration is. There is no way to set any ARBITRARY node as a "current" node and start the search from there, in a Find() method. Although you can restrict the search to a node and none of its children, you can't programmatically start a search at any node without it being specific. There is no general statement for:
= MyNodes(2).Nodes.Find("Hello", True)
and= MyNodes(2).My1stChildNodes(3).Nodes.Find("Hello", True)
-
That is where the frustration is. There is no way to set any ARBITRARY node as a "current" node and start the search from there, in a Find() method. Although you can restrict the search to a node and none of its children, you can't programmatically start a search at any node without it being specific. There is no general statement for:
= MyNodes(2).Nodes.Find("Hello", True)
and= MyNodes(2).My1stChildNodes(3).Nodes.Find("Hello", True)
treddie wrote:
There is no way to set any ARBITRARY node as a "current" node and start the search from there, in a Find() method
Ano node contains a node-collection that can be searched. If you know what node to start from, that is.
treddie wrote:
There is no general statement for:
TreeView1.Nodes.Add("Node1", "Node1").Nodes.Add("Sub1", "Sub1").Nodes.Add("Hello", "Hello") TreeView1.Nodes.Add("Node2", "Node2").Nodes.Add("Sub1", "Sub1").Nodes.Add("Hello", "Hello") Dim foundNode As TreeNode = TreeView1.Nodes("Node2").Nodes("Sub1") ' .Nodes("Hello") If foundNode IsNot Nothing Then TreeView1.SelectedNode = foundNode TreeView1.Focus() End If
Bastard Programmer from Hell :suss: If you can't read my code, try converting it here[^]
-
Bummer. :( I'm so close to finishing a program I've been working on for a year and the only thing hanging me up is this damn TreeView problem. I just find it so bizarre that MS could not provide some very simple functionality to a TreeView, that I would think would be self-evidently obvious properties and methods to include straight-away. I come up with nifty solutions only to have them dashed by a lack of really basic control methods. So frustrating.
Just because it's a "simple" method to make your job easier, doesn't mean it's a good thing to put into the framework. The .NET Framework is already too heavy. That's why it has been split into Full and Client Profile versions. Why put more stuff into it that's really not neccessary?
A guide to posting questions on CodeProject[^]
Dave Kreskowiak -
treddie wrote:
There is no way to set any ARBITRARY node as a "current" node and start the search from there, in a Find() method
Ano node contains a node-collection that can be searched. If you know what node to start from, that is.
treddie wrote:
There is no general statement for:
TreeView1.Nodes.Add("Node1", "Node1").Nodes.Add("Sub1", "Sub1").Nodes.Add("Hello", "Hello") TreeView1.Nodes.Add("Node2", "Node2").Nodes.Add("Sub1", "Sub1").Nodes.Add("Hello", "Hello") Dim foundNode As TreeNode = TreeView1.Nodes("Node2").Nodes("Sub1") ' .Nodes("Hello") If foundNode IsNot Nothing Then TreeView1.SelectedNode = foundNode TreeView1.Focus() End If
Bastard Programmer from Hell :suss: If you can't read my code, try converting it here[^]
-
If you're curious about recursive functions/parsing/codifying information, perhaps you should read this book: Godel, Escher & Bach by Douglas E. Hofstadter. also, A New Kind of Science by Stephen Wolfram is pretty interesting too. :)
-
Also take look at Emil Post's stuff on 'productions', circa 1930s. The authors initial was wrong, it's Douglas R. Hofstadter. I need better glasses. :) The NKS book is more along the lines of cellular automata. You might want to get the legs on your coffee table reinforced first. ;P
-
Just because it's a "simple" method to make your job easier, doesn't mean it's a good thing to put into the framework. The .NET Framework is already too heavy. That's why it has been split into Full and Client Profile versions. Why put more stuff into it that's really not neccessary?
A guide to posting questions on CodeProject[^]
Dave Kreskowiak -
Also take look at Emil Post's stuff on 'productions', circa 1930s. The authors initial was wrong, it's Douglas R. Hofstadter. I need better glasses. :) The NKS book is more along the lines of cellular automata. You might want to get the legs on your coffee table reinforced first. ;P