-
Feb 5th, 2012, 03:42 AM
#1
Thread Starter
Addicted Member
Menu Structures
Hi There,
i have this structure:
this is populated from a database.
this is how it looks like
i need a little help on how to design a function that can achieve this structure in code.im just thinking of using a recursive function.but i dont know how to start..
-
Feb 7th, 2012, 05:07 AM
#2
Fanatic Member
Re: Menu Structures
what technology are you using? i.e. bulk standard C#. mvc etc.
In MVC, if you were to use lazy loading on the context from the top node going via Entity Framework for instance, then this will load this kind of structure into the objects for you. (could be slow though if you have loads of data).
Otherwise if your using linq to SQL then you can do something like:
http://stackoverflow.com/questions/1...-relationships
-
Feb 7th, 2012, 06:04 AM
#3
Thread Starter
Addicted Member
Re: Menu Structures
hi bill,
i am using windows application c#.my problems is how to put the idea on a function.
-
Feb 7th, 2012, 07:10 AM
#4
Fanatic Member
Re: Menu Structures
so you have something like
public struct mystruct {.......}
and presumably you have some kind of collection holding the structs.
lets say your collection is called mycollection.
ForEach (mystruct inrec in mycollection)
{
here you use a treecontrol and use the parentid with id to determine weather to create a node or childnode. The problem you have here though is that you have 2 root nodes.
-
Feb 7th, 2012, 07:24 AM
#5
Thread Starter
Addicted Member
Re: Menu Structures
Hi billy,
Code:
private void CreateMenu()
{
// returns menu structures above
var menu = menulogic.getMenu(role);
foreach (var m in menu.orderby(x=>x.menuindex))
{
//I dont know what next in here.
}
}
-
Feb 7th, 2012, 09:20 AM
#6
Re: Menu Structures
You should look into recursive functions.
I haven't worked with LINQ yet, but it should be fairly easy to first select the items where the parent is 0, and then recursively call the function again and then use the parent's item as index.
Something like:
Code:
private void CreateMenu(var menu, int parent = 0)
{
foreach(var m in menu.Where(x=>x.ParentID == parent))
{
// output m value
// ...
// Recurse
CreateMenu(menu, m.ID);
}
}
Delete it. They just clutter threads anyway.
-
Feb 7th, 2012, 08:25 PM
#7
Thread Starter
Addicted Member
Re: Menu Structures
Hi BigB,
i cannot use var datatype as parameter and also a default value.i am using vs2008 and .netframework3.5.
-
Feb 8th, 2012, 10:36 AM
#8
Fanatic Member
Re: Menu Structures
var menu = menulogic.getMenu(role);
You have stated that this returns a menu struct.
What does menulogic.getMenu(role) actually return?
is it a structure, a collection of structure items etc.
eg.
if getMenu method is declared as:
public myStruct getMenu(int role) {}
for instance, then do not return it into a var , but into a myStruct
myStruct menu = menulogic.getMenu(role);
Now you can pass menu since from BIgB
private void CreateMenu(var menu, int parent = 0)
now becomes
private void CreateMenu(myStruct menu, int parent = 0)
you might have to change your structure to a class.
Thus
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|