Page 1 of 1

Breadcrumb Navigation in .NET

#1 RexGrammer  Icon User is offline

  • Coding Dynamo
  • member icon

Reputation: 182
  • View blog
  • Posts: 783
  • Joined: 27-October 11

Posted 07 June 2012 - 11:37 AM

Breadcrumbs


Recently I've had the need for a breadcrumb-like feature in .NET. I've tried to implement it using a string to represent the folder path, but it turned ugly really quick and became producing error which would require writing even more code for it to work correctly work (becoming REALLY MESSY). So I've thought about it and decided I could make a simple class to help me with that.

Oh, by the way if you don't know what breadcrumbs navigation is, here's a little article on Wikipedia: Breadcrumb (navigation). :)

So the next thing (after I've decide I'm going to make a class for breadcrumb navigation) on the agenda is the planning of the class:

Planning:

Some core features I would like it to have:

1. The ability to parse already-made file paths
2. The ability to add folders to the hierarchy
3. The ability to remove folders from the hierarchy
4. The ability to count the number of folders
5. And the ability to output the current folder path as a string

The next part is to plan how would those features be implemented:

1. You can make a constructor that accepts a already-made folder path as a parameter
2. A method that adds the folder
3. A method that removes the folder
4. A method that counts the number of folders
5. A method that represents the current folder path as a string

How could we realize those methods? Like this:

1. Make a private list of strings that holds the folder names. The top-most folder will be the 0th member of the list, and the bottom-most the last
2. In order to parse a path, split the string passed to the constructor on the '/' characters and add all of the elements of the array that you get by splitting to the list
3. Make a method that just adds the folder name to the list, you supply the name as a parameter to the method
4. Remove the last element of the list
5. A read-only property that gets the number of elements in the list
6. Define a string builder, for each string in the list add the "/" + the actual list element

Now comes the 'hard' part (it's not really hard):

Realization:

We need to define a private list that will hold the folder names:

private List<string> _folders;



Easy as pie! ;)

Constructors:

Next we need to define two constructors: one without parameters and one with the parameter that is the path that you want to add upon (represented in a string).

The parameterless one:
In the constructor code block just initialize the list we defined:
_folders = new List<string>();



The one with the folder path parameter:
Just split the string at the '/' characters and add all elements of the array to the list:
string[] folders = path.Split('/');
_folders.AddRange(folders);



The count property:

To implement the Count property just make a read-only property (just add the getter) and return the count of items in the list:
get { return _folders.Count; }



The add/remove folder methods:

To add a folder to the breadcrumbs just add the folder name to the list. You provide folder name as a parameter to the method and then call upon the Add method of the list:

public void AddFolder(string folder)
        {
            _folders.Add(folder);
        }



To remove the folder, just remove the last item in the list:
_folders.RemoveAt(_folders.Count - 1);



The string representation of the path:

To implement a method that will get the string representation of the current path:

1. Define a string builder
StringBuilder path = new StringBuilder();



2. Loop through the items in the list:
foreach (string _folder in _folders)
            {
            }



3. Foreach of the folders appent the "/" + the folder name to the string builder:
path.Append("/");
path.Append(folder);



4. Return the string representation of the stringbuilder:
return path.ToString();



NOTE: We could implement a feature that adds or removes the folder at a specific position, but since this is a breadcrumb navigation we only need to alter the last position in the hierarchy.

See the full code:
Spoiler


You're free to build up on this and add features as you like :D.

Is This A Good Question/Topic? 1
  • +

Page 1 of 1