Results 1 to 13 of 13

Thread: [RESOLVED] [2005] Losing values

  1. #1

    Thread Starter
    Admodistrator |2eM!x's Avatar
    Join Date
    Jan 2005
    Posts
    3,900

    Resolved [RESOLVED] [2005] Losing values

    Okay im making a class for my MP3 to read ID3 Tags,

    VB Code:
    1. Public Class MP3
    2.     Private Structure ID3
    3.         Dim SONGNAME As String
    4.         Dim ALBUM As String
    5.         Dim ARTIST As String
    6.         Dim YEAR As String
    7.         Dim COMMENT As String
    8.         Dim GENRE As String
    9.     End Structure
    10.     Dim _FILENAME As String
    11.  
    12.     Private mID3 As ID3
    13.     Public Sub New(ByVal fileName As String)
    14.         _FILENAME = fileName
    15.         readFile()
    16.     End Sub
    17.  
    18.     Private Sub readFile()
    19.         Dim fs As New IO.FileStream(_FILENAME, IO.FileMode.Open, IO.FileAccess.Read)
    20.         Dim br As New IO.BinaryReader(fs)
    21.         br.BaseStream.Seek(-128, IO.SeekOrigin.End)
    22.         If br.ReadChars(3) = "TAG" Then
    23.             With mID3
    24.                 .SONGNAME = br.ReadChars(30)
    25.                 .ARTIST = br.ReadChars(30)
    26.                 .ALBUM = br.ReadChars(30)
    27.                 .YEAR = br.ReadChars(4)
    28.                 .COMMENT = br.ReadChars(30)
    29.                 .GENRE = br.ReadChars(1)
    30.             End With
    31.         Else
    32.             fs.Close()
    33.             br.Close()
    34.         End If
    35.     End Sub
    36.  
    37.     Public Function getSongName() As String
    38.         Return mID3.SONGNAME
    39.     End Function
    40.  
    41.     Public Function getAlbum() As String
    42.         Return mID3.ALBUM
    43.     End Function
    44.  
    45.     Public Function getArtist() As String
    46.         Return mID3.ARTIST
    47.     End Function
    48.  
    49.     Public Function getYear() As String
    50.         Return mID3.YEAR
    51.     End Function
    52.  
    53.     Public Function getComment() As String
    54.         Return mID3.COMMENT
    55.     End Function
    56.  
    57.     Public Function getGenre() As String
    58.         Return mID3.GENRE
    59.     End Function
    60.  
    61. End Class
    VB Code:
    1. Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
    2.         Dim myMP3 As New MP3("C:\Documents and Settings\Jason\My Documents\My Music\iTunes\iTunes Music\Trapt\Unknown Album\01 Headstrong.mp3")
    3.         With myMP3
    4.             Debug.Print(.getAlbum)
    5.             Debug.Print(.getArtist)
    6.             Debug.Print(.getComment)
    7.             Debug.Print(.getSongName)
    8.             Debug.Print(.getYear)
    9.             Debug.Print(.getGenre)
    10.         End With
    11.     End Sub

    It all works great, the values get filled. But as soon as I try and acess the values through .getArtist etc the mID3.ARTIST becomes Null..Why is the value being reset?
    Last edited by |2eM!x; Nov 30th, 2006 at 09:52 PM.

  2. #2
    New Member
    Join Date
    Nov 2006
    Location
    Dallas, TX
    Posts
    7

    Re: [2005] Losing values

    There is a free DLL that does all of the MP3 tag ID stuff for you. It is called
    UltraIDLib. Here is the URL http://home.fuse.net/honnert/hundred/?UltraID3Lib

    Of course it is more fun to roll your own.

  3. #3
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,221

    Re: [2005] Losing values

    In your readFile method you are creating an ID3 object but only in a local variable. You never assign it to the mID3 member variable. You should do away with the local variable altogether because it doesn't serve a useful purpose. Local variables that hide member variables with the same name are generally a bad idea for this very reason: they make it easier to make this kind of mistake.
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

  4. #4
    Fanatic Member
    Join Date
    Aug 2006
    Location
    Chicago, IL
    Posts
    514

    Re: [2005] Losing values

    Got it to work.

    Change the values from a structure to fields in the class itself. Each time it's accessed, it creates that structure and erases all previous data. If you make them private fields, it works fine (which means remove the mID3 object).
    Warren Ayen
    Senior C# Developer
    DLS Software Studios (http://www.dlssoftwarestudios.com/)

    I use Microsoft Visual Studio 2005, 2008, working with Visual Basic and Visual C#
    Hey! If you like my post, or I solve your issue, please Rate Me!

  5. #5
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,221

    Re: [2005] Losing values

    Quote Originally Posted by warrenayen
    Got it to work.

    Change the values from a structure to fields in the class itself. Each time it's accessed, it creates that structure and erases all previous data. If you make them private fields, it works fine (which means remove the mID3 object).
    That's not true. Accessing a structure doesn't recreate it. The only time a new structure is created is in the readFile method and, as I said, that value is never assigned to the mID3 member variable. The reason it would have worked for you is because you removed the ID3 structure altogether, thus you also removed the local variable in the readFile method.

    Note that this is also a good reason to always qualify your member variables with "Me." when using them. I've seen it written that that is bad practice but to me it increases clarity. Doing so may have prevented this error by highlighting the fact that it was a local variable being accessed and not a member variable of the same name.
    Last edited by jmcilhinney; Nov 27th, 2006 at 06:17 PM.
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

  6. #6

    Thread Starter
    Admodistrator |2eM!x's Avatar
    Join Date
    Jan 2005
    Posts
    3,900

    Re: [2005] Losing values

    Gee. Cant believe i missed that. Thanks JM

  7. #7

    Thread Starter
    Admodistrator |2eM!x's Avatar
    Join Date
    Jan 2005
    Posts
    3,900

    Re: [2005] Losing values

    Wait I lied. Even though that last error was definately going to turn up an error, it still returns nothing in the later calls.

  8. #8
    Fanatic Member
    Join Date
    Aug 2006
    Location
    Chicago, IL
    Posts
    514

    Re: [2005] Losing values

    You're right. I thought that it was being recreated because that's what I changed - not realizing there was a local variable and that's why it wasn't working. Either way, I got it to work, anyway

    And yes, using ME is a great idea. I always use ME at all times...
    Last edited by warrenayen; Nov 28th, 2006 at 09:36 AM. Reason: me
    Warren Ayen
    Senior C# Developer
    DLS Software Studios (http://www.dlssoftwarestudios.com/)

    I use Microsoft Visual Studio 2005, 2008, working with Visual Basic and Visual C#
    Hey! If you like my post, or I solve your issue, please Rate Me!

  9. #9

    Thread Starter
    Admodistrator |2eM!x's Avatar
    Join Date
    Jan 2005
    Posts
    3,900

    Re: [2005] Losing values

    Any other ideas? I removed the definition in the ReadFile sub, still is nothing though. I cant understand this, never ran into this problem before in this manner.

  10. #10

    Thread Starter
    Admodistrator |2eM!x's Avatar
    Join Date
    Jan 2005
    Posts
    3,900

    Re: [2005] Losing values

    Can you tell me how you got it to work? Because I cant

  11. #11
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,221

    Re: [2005] Losing values

    Have you debugged your readFile method? I just ran your code on a couple of MP3 files. The first one successfully read a song name, artist and genre only, and the genre was a non-ASCII character. All that information was successfully written to the Immediate window in one line. The second one entered the Else block so never read any information, hence all ID3 fields were Nothing.
    Last edited by jmcilhinney; Dec 1st, 2006 at 12:34 AM.
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

  12. #12

    Thread Starter
    Admodistrator |2eM!x's Avatar
    Join Date
    Jan 2005
    Posts
    3,900

    Re: [2005] Losing values

    Im sooo stupid. I changed the file to a file with no ID3 tags and thats why it wasnt working. Also that else block was definately not how i intended it so numerous things were going wrong. Ive been writing Java code for too long now I guess :S

  13. #13
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,221

    Re: [2005] Losing values

    Quote Originally Posted by |2eM!x
    Ive been writing Java code for too long now I guess :S
    I was thinking that all those getSomething functions looked like Java. The .NET way would be a to declare a ReadOnly property.
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width