|
-
Nov 27th, 2006, 05:16 PM
#1
Thread Starter
Admodistrator
[RESOLVED] [2005] Losing values
Okay im making a class for my MP3 to read ID3 Tags,
VB Code:
Public Class MP3
Private Structure ID3
Dim SONGNAME As String
Dim ALBUM As String
Dim ARTIST As String
Dim YEAR As String
Dim COMMENT As String
Dim GENRE As String
End Structure
Dim _FILENAME As String
Private mID3 As ID3
Public Sub New(ByVal fileName As String)
_FILENAME = fileName
readFile()
End Sub
Private Sub readFile()
Dim fs As New IO.FileStream(_FILENAME, IO.FileMode.Open, IO.FileAccess.Read)
Dim br As New IO.BinaryReader(fs)
br.BaseStream.Seek(-128, IO.SeekOrigin.End)
If br.ReadChars(3) = "TAG" Then
With mID3
.SONGNAME = br.ReadChars(30)
.ARTIST = br.ReadChars(30)
.ALBUM = br.ReadChars(30)
.YEAR = br.ReadChars(4)
.COMMENT = br.ReadChars(30)
.GENRE = br.ReadChars(1)
End With
Else
fs.Close()
br.Close()
End If
End Sub
Public Function getSongName() As String
Return mID3.SONGNAME
End Function
Public Function getAlbum() As String
Return mID3.ALBUM
End Function
Public Function getArtist() As String
Return mID3.ARTIST
End Function
Public Function getYear() As String
Return mID3.YEAR
End Function
Public Function getComment() As String
Return mID3.COMMENT
End Function
Public Function getGenre() As String
Return mID3.GENRE
End Function
End Class
VB Code:
Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
Dim myMP3 As New MP3("C:\Documents and Settings\Jason\My Documents\My Music\iTunes\iTunes Music\Trapt\Unknown Album\01 Headstrong.mp3")
With myMP3
Debug.Print(.getAlbum)
Debug.Print(.getArtist)
Debug.Print(.getComment)
Debug.Print(.getSongName)
Debug.Print(.getYear)
Debug.Print(.getGenre)
End With
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.
-
Nov 27th, 2006, 05:47 PM
#2
New Member
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.
-
Nov 27th, 2006, 05:50 PM
#3
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.
-
Nov 27th, 2006, 05:53 PM
#4
Fanatic Member
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!
-
Nov 27th, 2006, 06:11 PM
#5
Re: [2005] Losing values
 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.
-
Nov 27th, 2006, 06:41 PM
#6
Thread Starter
Admodistrator
Re: [2005] Losing values
Gee. Cant believe i missed that. Thanks JM
-
Nov 27th, 2006, 06:47 PM
#7
Thread Starter
Admodistrator
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.
-
Nov 28th, 2006, 09:36 AM
#8
Fanatic Member
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!
-
Nov 28th, 2006, 08:06 PM
#9
Thread Starter
Admodistrator
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.
-
Nov 30th, 2006, 09:52 PM
#10
Thread Starter
Admodistrator
Re: [2005] Losing values
Can you tell me how you got it to work? Because I cant
-
Nov 30th, 2006, 10:23 PM
#11
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.
-
Dec 1st, 2006, 12:00 PM
#12
Thread Starter
Admodistrator
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
-
Dec 1st, 2006, 06:18 PM
#13
Re: [2005] Losing values
 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.
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
|