Help with Collections
-
This might take a few words to explain, so please bear with me if you will: I'm chopping the data of a .WAV file into segments each of length 256 samples. After this segmentation is done, let's say I have N segments. My N by M multidimensional array is called WaveSegments[N,M]. Now, I want to display each of these segments in a UserControl I've made, and want to cycle through the segments by means of an up/down control. So, for example, clicking once on the up control would change the number from 1 to 2, and the display would change from the samples of WaveSegment[1] to WaveSegment[2]. It seems that using collections would ease coding tremendously, but I seem to be running into any number of syntactical problems. Could somebody please demonstrate to me how I could do this? Thank you.
-
This might take a few words to explain, so please bear with me if you will: I'm chopping the data of a .WAV file into segments each of length 256 samples. After this segmentation is done, let's say I have N segments. My N by M multidimensional array is called WaveSegments[N,M]. Now, I want to display each of these segments in a UserControl I've made, and want to cycle through the segments by means of an up/down control. So, for example, clicking once on the up control would change the number from 1 to 2, and the display would change from the samples of WaveSegment[1] to WaveSegment[2]. It seems that using collections would ease coding tremendously, but I seem to be running into any number of syntactical problems. Could somebody please demonstrate to me how I could do this? Thank you.
Actually, using any kind of
IEnumerable
orIListSource
would be great. You can data-bind the collection to the up/down control and use theCurrencyManager.Position
property to adjust the position. For more information about data-binding properties and data sources, seeControl.BindingContext
andCurrencyManager
. This would save you from the mess of handling all this yourself. When used correctly, other controls bound to the data source are updated when the position of the current element in the data source is changed. At any rate, what are the "syntactical" problems? For ease, extendCollectionBase
and override the methods (or implement new methods with strongly-typed params that call theCollectionBase
's methods) and most of the rest of the work is already done (which uses anArrayList
to back the elements).-----BEGIN GEEK CODE BLOCK----- Version: 3.21 GCS/G/MU d- s: a- C++++ UL@ P++(+++) L+(--) E--- W+++ N++ o+ K? w++++ O- M(+) V? PS-- PE Y++ PGP++ t++@ 5 X+++ R+@ tv+ b(-)>b++ DI++++ D+ G e++>+++ h---* r+++ y+++ -----END GEEK CODE BLOCK-----
-
Actually, using any kind of
IEnumerable
orIListSource
would be great. You can data-bind the collection to the up/down control and use theCurrencyManager.Position
property to adjust the position. For more information about data-binding properties and data sources, seeControl.BindingContext
andCurrencyManager
. This would save you from the mess of handling all this yourself. When used correctly, other controls bound to the data source are updated when the position of the current element in the data source is changed. At any rate, what are the "syntactical" problems? For ease, extendCollectionBase
and override the methods (or implement new methods with strongly-typed params that call theCollectionBase
's methods) and most of the rest of the work is already done (which uses anArrayList
to back the elements).-----BEGIN GEEK CODE BLOCK----- Version: 3.21 GCS/G/MU d- s: a- C++++ UL@ P++(+++) L+(--) E--- W+++ N++ o+ K? w++++ O- M(+) V? PS-- PE Y++ PGP++ t++@ 5 X+++ R+@ tv+ b(-)>b++ DI++++ D+ G e++>+++ h---* r+++ y+++ -----END GEEK CODE BLOCK-----
Heath, thanks a lot for your post. I'm new to programming, and started with C# as that's the language of choice at my company. Could you give me a code snippet showing how to implement this? Thanks!
-
Heath, thanks a lot for your post. I'm new to programming, and started with C# as that's the language of choice at my company. Could you give me a code snippet showing how to implement this? Thanks!
No need - see the documentation for the
CollectionBase
class in the .NET Framework SDK (just type "CollectionBase" (without quotes) in the Index) and you'll see an example. Be sure to read the documentation - if not skim it over to see what's available in the base class library - especially when starting out.-----BEGIN GEEK CODE BLOCK----- Version: 3.21 GCS/G/MU d- s: a- C++++ UL@ P++(+++) L+(--) E--- W+++ N++ o+ K? w++++ O- M(+) V? PS-- PE Y++ PGP++ t++@ 5 X+++ R+@ tv+ b(-)>b++ DI++++ D+ G e++>+++ h---* r+++ y+++ -----END GEEK CODE BLOCK-----
-
No need - see the documentation for the
CollectionBase
class in the .NET Framework SDK (just type "CollectionBase" (without quotes) in the Index) and you'll see an example. Be sure to read the documentation - if not skim it over to see what's available in the base class library - especially when starting out.-----BEGIN GEEK CODE BLOCK----- Version: 3.21 GCS/G/MU d- s: a- C++++ UL@ P++(+++) L+(--) E--- W+++ N++ o+ K? w++++ O- M(+) V? PS-- PE Y++ PGP++ t++@ 5 X+++ R+@ tv+ b(-)>b++ DI++++ D+ G e++>+++ h---* r+++ y+++ -----END GEEK CODE BLOCK-----
Thanks Heath, Will get back to you if I run into problems.
-
No need - see the documentation for the
CollectionBase
class in the .NET Framework SDK (just type "CollectionBase" (without quotes) in the Index) and you'll see an example. Be sure to read the documentation - if not skim it over to see what's available in the base class library - especially when starting out.-----BEGIN GEEK CODE BLOCK----- Version: 3.21 GCS/G/MU d- s: a- C++++ UL@ P++(+++) L+(--) E--- W+++ N++ o+ K? w++++ O- M(+) V? PS-- PE Y++ PGP++ t++@ 5 X+++ R+@ tv+ b(-)>b++ DI++++ D+ G e++>+++ h---* r+++ y+++ -----END GEEK CODE BLOCK-----
Heath, I went over the CollectionBase documents, but I guess it's just not very clear to me. I've included some of my code below and it would be great if you could help me out. // Code for class WaveformSegment. public class WaveformSegment { // First we need an object of class "WavFile" to read the contents of the file. // All data is accessed through objWaveToSegment.Data[] public WaveFile objWaveToSegment; // Now we obtain the filename of the file we chose private string m_FileOpened; public string FileOpened { set { m_FileOpened = value; } get { return m_FileOpened; } } // We now formulate a method to extract the information we need public void ChopIntoSegments() { // Obtain access to the data in the .WAV file objWaveToSegment = new WaveFile( m_FileOpened ); objWaveToSegment.Read( ); // Definition of multidimensional array WaveSegments int[,] WaveSegments = new int[17,256]; for(int i = 0; i < 17; i++) { for(int j = 0; j < 256; j++) { WaveSegments[i,j] = objWaveToSegment.Data[(i*64) + j]; } } } // End of ChopIntoSegments()
-
No need - see the documentation for the
CollectionBase
class in the .NET Framework SDK (just type "CollectionBase" (without quotes) in the Index) and you'll see an example. Be sure to read the documentation - if not skim it over to see what's available in the base class library - especially when starting out.-----BEGIN GEEK CODE BLOCK----- Version: 3.21 GCS/G/MU d- s: a- C++++ UL@ P++(+++) L+(--) E--- W+++ N++ o+ K? w++++ O- M(+) V? PS-- PE Y++ PGP++ t++@ 5 X+++ R+@ tv+ b(-)>b++ DI++++ D+ G e++>+++ h---* r+++ y+++ -----END GEEK CODE BLOCK-----
Heath, I went over the CollectionBase documents, but I guess it's just not very clear to me. I've included some of my code below and it would be great if you could help me out. ************************************************** // Code for class WaveformSegment. public class WaveformSegment { // First we need an object of class "WavFile" to // read the contents of the file. // All data is accessed through // objWaveToSegment.Data[] public WaveFile objWaveToSegment; // Now we obtain the filename of the file we chose private string m_FileOpened; public string FileOpened { set { m_FileOpened = value; } get { return m_FileOpened; } } // This function performs the segmentation public void ChopIntoSegments() { // Obtain access to the data in the .WAV file objWaveToSegment = new WaveFile( m_FileOpened ); // Read() is a function in another file, that reads the data in a .WAV file objWaveToSegment.Read( ); // Definition of multidimensional array // WaveSegments int[,] WaveSegments = new int[17,256]; for(int i = 0; i < 17; i++) { for(int j = 0; j < 256; j++) { WaveSegments[i,j] = objWaveToSegment.Data[(i*64) + j]; } } // End of ChopIntoSegments() ************************************************** Now, I want to write a class SegmentCollection. Here is what I've written so far ************************************************** public class SegmentCollection : System.Collections.CollectionBase { public SegmentCollection() {} public void Add( WaveformSegment WaveSegments ) { this.List.Add( WaveSegments ); } public int IndexOf( WaveformSegment WaveSegments ) { return this.List.IndexOf( WaveSegments ); } public virtual WaveformSegment this [int Index] { get { return (WaveformSegment)this.List[Index]; } } ************************************************** Is this right so far, and how do I access each individual segment? Thank you.
-
Heath, I went over the CollectionBase documents, but I guess it's just not very clear to me. I've included some of my code below and it would be great if you could help me out. // Code for class WaveformSegment. public class WaveformSegment { // First we need an object of class "WavFile" to read the contents of the file. // All data is accessed through objWaveToSegment.Data[] public WaveFile objWaveToSegment; // Now we obtain the filename of the file we chose private string m_FileOpened; public string FileOpened { set { m_FileOpened = value; } get { return m_FileOpened; } } // We now formulate a method to extract the information we need public void ChopIntoSegments() { // Obtain access to the data in the .WAV file objWaveToSegment = new WaveFile( m_FileOpened ); objWaveToSegment.Read( ); // Definition of multidimensional array WaveSegments int[,] WaveSegments = new int[17,256]; for(int i = 0; i < 17; i++) { for(int j = 0; j < 256; j++) { WaveSegments[i,j] = objWaveToSegment.Data[(i*64) + j]; } } } // End of ChopIntoSegments()
You're not overriding the functionality of the
CollectionBase
like you're supposed to. See the class documentation itself for an example at the bottom of the page.-----BEGIN GEEK CODE BLOCK----- Version: 3.21 GCS/G/MU d- s: a- C++++ UL@ P++(+++) L+(--) E--- W+++ N++ o+ K? w++++ O- M(+) V? PS-- PE Y++ PGP++ t++@ 5 X+++ R+@ tv+ b(-)>b++ DI++++ D+ G e++>+++ h---* r+++ y+++ -----END GEEK CODE BLOCK-----