473,405 Members | 2,167 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,405 software developers and data experts.

Create Album for First Track, but Add to Album for Subsequent Tracks

Tyler Wiebe
Okay, to start off, I'd like to show what I have working.

Form1:

Expand|Select|Wrap|Line Numbers
  1. using System;
  2. using System.Collections.Generic;
  3. using System.IO;
  4. using System.Linq;
  5. using System.Windows.Forms;
  6.  
  7. namespace CustomListTest
  8. {
  9.     public partial class Form1 : Form
  10.     {
  11.         public static string MusicFolder = @"C:\Users\User\Desktop\My Music";
  12.  
  13.         public Form1()
  14.         {
  15.             InitializeComponent();
  16.         }
  17.  
  18.         private void button1_Click(object sender, System.EventArgs e)
  19.         {
  20.             if (Directory.Exists(MusicFolder))
  21.             {
  22.                 List<string> ImportList = new List<string>(Directory.GetFiles(MusicFolder, "*.mp3", SearchOption.AllDirectories).ToList<string>());
  23.                 foreach (string FilePath in ImportList)
  24.                 {
  25.                     _Library._Music.Add(new _Artist(_GetDetailsOf.File(FilePath, 13), _GetDetailsOf.File(FilePath, 14), _GetDetailsOf.File(FilePath, 15), _GetDetailsOf.File(FilePath, 26), _GetDetailsOf.File(FilePath, 21), FilePath));
  26.                 }
  27.  
  28.                 foreach (_Artist Artist in _Library._Music)
  29.                 {
  30.                     foreach (_Album Album in Artist._Albums)
  31.                     {
  32.                         foreach(_Track Track in Album._Tracks)
  33.                         {
  34.                             Console.WriteLine("Artist: " + Artist._Name + " -- Album: " + Album._Title + ": " + Album._Year + " -- Track#: " + Track._Number + " - Title: " + Track._Title + " -- FilePath: " + Track._FilePath);
  35.                         }
  36.                     }
  37.                 }
  38.             }
  39.         }
  40.     }
  41. }
  42.  
Custom Classes:

_GetDetailsOf.File()

Expand|Select|Wrap|Line Numbers
  1. namespace CustomListTest
  2. {
  3.     public class _GetDetailsOf
  4.     {
  5.         private static Shell32.Shell ShellObj = new Shell32.Shell();
  6.         private static Shell32.Folder FolderObj;
  7.         private static Shell32.FolderItem FolderItemObj;
  8.         private static string FileName;
  9.         private static string FileLocation;
  10.  
  11.         /// <summary>
  12.         /// Used to get details of a specified file
  13.         /// </summary>
  14.         /// <param name="FilePath"></param>
  15.         /// <param name="ColumnNumber">Artist: 13 | Album: 14 | Year: 15 | Track Number: 26 | Title: 21</param>
  16.         /// <returns></returns>
  17.         public static string File(string FilePath, int ColumnNumber)
  18.         {
  19.             FileName = FilePath.Substring(FilePath.LastIndexOf("\\") + 1);
  20.             FileLocation = FilePath.Substring(0, FilePath.LastIndexOf("\\"));
  21.             FolderObj = ShellObj.NameSpace(FileLocation);
  22.             FolderItemObj = FolderObj.ParseName(FileName);
  23.             return FolderObj.GetDetailsOf(FolderItemObj, ColumnNumber);
  24.         }
  25.     }
  26. }
  27.  
_Library:

Expand|Select|Wrap|Line Numbers
  1. using System.Collections.Generic;
  2.  
  3. namespace CustomListTest
  4. {
  5.     public class _Library
  6.     {
  7.         public static List<_Artist> _Music = new List<_Artist>();
  8.     }
  9.  
  10.     public class _Artist
  11.     {
  12.         public string _Name;
  13.  
  14.         public List<_Album> _Albums = new List<_Album>();
  15.  
  16.         public _Artist(string Artist, string Album, string Year, string TrackNumber, string Title, string FilePath)
  17.         {
  18.             _Name = Artist;
  19.             _Albums.Add(new _Album(Album, Year, TrackNumber, Title, FilePath));
  20.         }
  21.     }
  22.  
  23.     public class _Album
  24.     {
  25.         public string _Title;
  26.         public string _Year;
  27.  
  28.         public List<_Track> _Tracks = new List<_Track>();
  29.  
  30.         public _Album(string Album, string Year, string TrackNumber, string Title, string FilePath)
  31.         {
  32.             _Title = Album;
  33.             _Year = Year;
  34.             _Tracks.Add(new _Track(TrackNumber, Title, FilePath));
  35.         }
  36.     }
  37.  
  38.     public class _Track
  39.     {
  40.         public string _Number;
  41.         public string _Title;
  42.         public string _FilePath;
  43.  
  44.         public _Track(string TrackNumber, string Title, string FilePath)
  45.         {
  46.             _Number = TrackNumber;
  47.             _Title = Title;
  48.             _FilePath = FilePath;
  49.         }
  50.     }
  51. }
  52.  
Now the problem I'm having is in Form1, located at #region CurrentProblem:

Expand|Select|Wrap|Line Numbers
  1.         private void button1_Click(object sender, System.EventArgs e)
  2.         {
  3.             if (Directory.Exists(MusicFolder))
  4.             {
  5.                 List<string> ImportList = new List<string>(Directory.GetFiles(MusicFolder, "*.mp3", SearchOption.AllDirectories).ToList<string>());
  6.                 foreach (string FilePath in ImportList)
  7.                 {
  8.                     #region CurrentProblem
  9.  
  10.                     _Library._Music.Add(new _Artist(_GetDetailsOf.File(FilePath, 13), _GetDetailsOf.File(FilePath, 14), _GetDetailsOf.File(FilePath, 15), _GetDetailsOf.File(FilePath, 26), _GetDetailsOf.File(FilePath, 21), FilePath));
  11.  
  12.                     #endregion
  13.                 }
  14.  
  15.                 foreach (_Artist Artist in _Library._Music)
  16.                 {
  17.                     foreach (_Album Album in Artist._Albums)
  18.                     {
  19.                         foreach(_Track Track in Album._Tracks)
  20.                         {
  21.                             Console.WriteLine("Artist: " + Artist._Name + " -- Album: " + Album._Title + ": " + Album._Year + " -- Track#: " + Track._Number + " - Title: " + Track._Title + " -- FilePath: " + Track._FilePath);
  22.                         }
  23.                     }
  24.                 }
  25.             }
  26.         }
  27.  
I need to know how to do something like this:

Expand|Select|Wrap|Line Numbers
  1.                 foreach (string FilePath in ImportList)
  2.                 {
  3.                     if (_Library._Music.Contains(_Artist._Name(_GetDetailsOf.File(FilePath, 13))))
  4.                     {
  5.                         if (_Library._Music[_Library._Music.IndexOf(_Artist._Name(_GetDetailsOf.File(FilePath, 13)))]._Albums.Contains(_Album._Title(_GetDetailsOf.File(FilePath, 14)))
  6.                         {
  7.                             _Library._Music[_Library._Music.IndexOf(_Artist._Name(_GetDetailsOf.File(FilePath, 13)))]._Albums[]._Tracks.Add(new _Track(_GetDetailsOf.File(FilePath, 26), _GetDetailsOf.File(FilePath, 21), FilePath));
  8.                         }
  9.                         else
  10.                         {
  11.                             _Library._Music[_Library._Music.IndexOf(_Artist._Name(_GetDetailsOf.File(FilePath, 13)))]._Albums.Add(new _Album(_GetDetailsOf.File(FilePath, 14), _GetDetailsOf.File(FilePath, 15), _GetDetailsOf.File(FilePath, 26), _GetDetailsOf.File(FilePath, 21), FilePath));
  12.                         }
  13.                     }
  14.                     else
  15.                     {
  16.                         _Library._Music.Add(new _Artist(_GetDetailsOf.File(FilePath, 13), _GetDetailsOf.File(FilePath, 14), _GetDetailsOf.File(FilePath, 15), _GetDetailsOf.File(FilePath, 26), _GetDetailsOf.File(FilePath, 21), FilePath));
  17.                     }
  18.                 }
  19.  
I know that looks terrible, and I know it's incorrect, but that's what I need your help with.

I need a way so that if _GetDetailsOf.File(FilePath, 13) returns an artist that already is in _Library._Music, it'll then check if the album exists, and if the album exists, it'll add the file to album as a new _Track. Hopefully you understand this.

And just in case you're wondering why I'm doing this and not this:

.Add(GetDetailsOf(file, 13) + "|" + GetDetailsOf(file, 14) + "|" + GetDetailsOf(file, 15) + "|" + GetDetailsOf(file, 26) + "|" + GetDetailsOf(file, 21) + "|" + FilePath);

and then retrieving it with .Split['|'], is because of the simple fact that the '|' can be placed in Artist, Album, and Title values, so it'd get a bit hard on those files. I'm not say I have any, but I'd like to get rid of the possibility of that happening.

So any help at all would be very much appreciated.
Jun 23 '11 #1
4 1699
GaryTexmo
1,501 Expert 1GB
I'll be honest, I'm having a hell of a time understanding your code, so before we get to the heart of the matter, here's a few tips :)

Tip #1: Why are you putting an underscore character before all your object/variable names? I've noticed people use underscores to denote private members in a class (others use m_) but class names generally don't have it. I see you have public members as well (you can also rewrite these as properties if you wanted, but that's just fine) so anything exposed publicly should really exclude the underscore. It just makes your code a bit harder to read.

Tip #2: Using an integer to denote the column number makes it very hard to read. I'd suggest using a constant, or better yet, an enum. Something like...

Artist: 13 | Album: 14 | Year: 15 | Track Number: 26 | Title: 21
Expand|Select|Wrap|Line Numbers
  1. public enum FileDetailsColumn
  2. {
  3.   Artist = 13,
  4.   Album = 14,
  5.   Year = 15,
  6.   Title = 21,
  7.   TrackNumber = 26
  8. }
Then you can change your GetDetailsOf.File method to take a parameter of type FileDetailsColumn (instead of an int). This changes your calls to something like...

Expand|Select|Wrap|Line Numbers
  1. GetDetailsOf.File(FilePath, FileDetailsColumn.Artist)
This has much more meaning to someone else reading the code.

Now, to your actual question...
I need a way so that if _GetDetailsOf.File(FilePath, 13) returns an artist that already is in _Library._Music, it'll then check if the album exists, and if the album exists, it'll add the file to album as a new _Track. Hopefully you understand this.
What you have suggested is a bit hard to read but you've pretty clearly defined what you want. Your Library class is a list of Artists, so you should be able to fairly easily find out if your Artist exists in there; however, you won't be able to use Contains as this method will take an object of type Artist and it sounds like you just want to know if the Artist's Name exists in the library. So you'll need to add a method to your Library class to find this for you.

Expand|Select|Wrap|Line Numbers
  1. public class Library
  2. {
  3.   public static List<Artist> Music = new List<Artist>();
  4.  
  5.   public bool GetArtistByName(string artistName)
  6.   {
  7.     Artist retArtist = null;
  8.  
  9.     foreach (Artist a in Music)
  10.     {
  11.       if (a.Name == artistName)
  12.       {
  13.         retArtist = a;
  14.         break;
  15.       }
  16.     }
  17.  
  18.     return retArtist;
  19.   }
  20. }
This will let you find out if your artist name exists already. Also, to avoid making many, many calls to GetDetailsOf.File(...) load them into variables at the start (You could also consider making a class with a LoadFromFile method).

Expand|Select|Wrap|Line Numbers
  1. string artistName = GetDetailsOf.File(...);
  2. string albumTitle = ...
  3. string albumYear = ...
  4. string trackNumber = ...
  5. string trackTitle = ...
  6.  
  7. Artist artist = Library.GetArtistByName(artistName);
  8. if (artist != null)
  9. {
  10.   // Artist exists, now go find the album in a similar fashion
  11. }
  12. else
  13. {
  14.   // Artist doesn't exist, take appropriate action (I assume you'll build a new one and add it to Libary.Music)
  15. }
Hopefully that gives you an idea... you'll probably want to add a similar method to the Artist class called GetAlbumByTitle or GetAlbumByTitleAndYear so you can search an artist's album list and find any that match your criteria. If you anticipate multiple matches in your Artist and Album lists, you might want to actually return a List object instead of a single item.

Hopefully that helps you out and gives you a place to start. Feel free to post back with any more questions :)
Jun 23 '11 #2
Thank you for your reply, it's exactly what I needed, but one another note, I'd like to reply to your tips.

1) The reason I'm using underscores is because if you start typing anything, the side list that appears to show the closest match, or previous used item starting with that text, is useful, but I find it easier to use with the underscore for my own created objects, because if I type in the underscore, all of my custom objects are right there and not through out the entire list.

2) The reason I didn't use an enum is because, 1) I've barely ever used them. 2) I've practically mesmerized those numbers. 3) Even if I forget, the code

Expand|Select|Wrap|Line Numbers
  1.         /// <summary>
  2.         /// Used to get details of a specified file
  3.         /// </summary>
  4.         /// <param name="FilePath"></param>
  5.         /// <param name="ColumnNumber">Artist: 13 | Album: 14 | Year: 15 | Track Number: 26 | Title: 21</param>
  6.         /// <returns></returns>
  7.  
displays it well enough for me. If I needed access to all of the numbers (over 280), I would definitely use an enum.

And on a side note, this is just a very bad rough draft, which will be put to full use now thanks to you. I do feel rather stupid for not thinking of it myself.

And on another side note, the only programing courses I took, were two years ago in high school, and they were a beginners class to C++ and another beginners class to html & xml. I've taught myself C# from scratch because I thought it'd be fun, and I was right, I'm having a blast.

And once again, thanks for your help, I really appreciate it, it's the final piece I needed for my speech recognition program running the axWindowsMediaPlayer1 plugin in it.
Jun 23 '11 #3
Actually, there's one more thing I'd like to ask. By any chance would you know how to serialize that list into xml or something so that it'll load quickly?
Jun 23 '11 #4
GaryTexmo
1,501 Expert 1GB
1) And if that's your system, that's your system :D Most folks tend to try to copy Microsoft's approach to keep things fairly standard. For internal stuff using the underscore (or m_) is fairly standard, but for public properties (which a public member effectively mimics) the standard is to not use that. Also keep in mind that as your program grows, your system kind of falls apart since you'll have a lot of custom classes. Note that you can also organize things by namespace (for class names at any rate) and can find what you want by first looking in the namespace. It's entirely up to you, I'm just pointing these things out so you're informed :)

2) In the above case and this one, it's more about readability. You know what those numbers mean and have them memorized but other people reading your code or working with it in the future haven't. It makes your code a lot more maintainable when you use enums. Again, it's up to you of course, but I'd highly recommend it.

As for how to serialize it, it's actually quite easy :) Have a look at the following links:
http://www.switchonthecode.com/tutor...-serialization
http://support.microsoft.com/kb/815813

There are many more that you can find via google and if you get stuck, feel free to post here for help (though please make a separate thread for it). Good luck! :)
Jun 24 '11 #5

Sign in to post your reply or Sign up for a free account.

Similar topics

3
by: Michael Lauzon | last post by:
This is not for a class, I have a group on SourceForge, this is what one of the Developers is asking; the more advanced you can make it right off all the better!: Can someone please create...
8
by: Robertico | last post by:
Hi, I found a tutorial to create an oline photo album (php and mySQL). Great, but i'd like to add some user management and that's not in the tutorial :-)) So i'am looking for an example,...
21
by: brink | last post by:
I work in the parts department of a new car dealership. We ar frequently asked by customers for fluid capacities and types of thei vehicles. Unfortunately this information is not in our parts...
6
by: Frank Wilson | last post by:
Tom, It sounds to me like ASP, not ASP.NET is handling the request for WebForm1.aspx. This is most likely an IIS config issue that may have been caused by order of installation or...
5
by: Rudy | last post by:
Hello All! Can you dynamicly create a folder in ASP.net? Here is the set up. I have a folder where I will store pics. What I would like to do is create a cookie, track the user who has logged...
6
by: daykirby | last post by:
Sorry if this is a crosspost - I think I put this in the wrong group before. I have an asp.net app that takes a few minutes to start on the server site (unavoidable) but once running it...
10
by: SM | last post by:
Hello I'm trying to create a multi dimensional array in JavaScript, but after some reading i still can't figure out how to apply it to my model. Here it is: I have a list A and for each item...
1
by: ssaini | last post by:
Hi, I am consuming a java web service in .NET C# smart client, which is based upon Microsoft's SCSF application block.The SCSF is a wrapper over WinForms. When I make a call to any of the Java...
1
by: Nimesh dadhaniya | last post by:
Hi All, I am trying to learn EJB's on my own. I installed the WebLogic Server Trial version and then wrote the Home Interface, Component Interface and the Bean Class. Now I started the WebLogic...
1
by: shapper | last post by:
Hello, How do I create the following XML file at runtime? <?xml version="1.0" encoding="UTF-8"?> <gallery> <album title="Album Title" description="Album Description" lgPath="../MyAlbum/Lg/"
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
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...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
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...

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.