Click here to Skip to main content
16,011,988 members
Home / Discussions / C#
   

C#

 
GeneralRe: Where in Toronto Ont Canada can I walk in and buy Visual Studio 2008 ? Pin
PIEBALDconsult5-Mar-10 4:31
mvePIEBALDconsult5-Mar-10 4:31 
AnswerRe: Where in Toronto Ont Canada can I walk in and buy Visual Studio 2008 ? Pin
Ravi Bhavnani4-Mar-10 15:45
professionalRavi Bhavnani4-Mar-10 15:45 
AnswerRe: Where in Toronto Ont Canada can I walk in and buy Visual Studio 2008 ? Pin
PIEBALDconsult4-Mar-10 17:42
mvePIEBALDconsult4-Mar-10 17:42 
Questionhow to read and write to xml using dataset [modified] Pin
xiaowenjie4-Mar-10 12:51
xiaowenjie4-Mar-10 12:51 
AnswerRe: how to read and write to xml using dataset Pin
snorkie4-Mar-10 16:37
professionalsnorkie4-Mar-10 16:37 
GeneralRe: how to read and write to xml using dataset Pin
xiaowenjie4-Mar-10 21:47
xiaowenjie4-Mar-10 21:47 
AnswerRe: how to read and write to xml using dataset Pin
shawnzhang4-Mar-10 19:26
shawnzhang4-Mar-10 19:26 
QuestionRemoving All "List<*>" Items Though It Shouldn't ... ?? [SOLVED] Pin
Matt U.4-Mar-10 12:36
Matt U.4-Mar-10 12:36 
Language: C# 2.0
IDE: VS2008 Professional
OS: Windows Vista Home Premium SP2
(Just in case any of this information is needed)

Alright, I'm in the process of writing some software mostly for the fun of it, though it will be useful to others as I have already talked with people about it. Anyhow, on to my issue.

Well, when I started writing the application (a Record Label management system) I was using really inefficient methods and such. I was using things that were not necessary and I wanted this to be more of a quality application than just a "slap-together" project. So I started re-writing some code, re-creating some forms, and so on. The application is MDI and the child windows are for things such as managing sessions, artists, sales, etc.

I have this code which is used to delete an artist. The 'RecordLabel' class is a simple class I wrote that handles the main file I/O (saving/loading the record label data to/from a file). It is referenced like this "RecordLabel recordLabel" in the "MainForm" class. Here is the code from "ArtistsForm":

public Artist GetArtistData()
        {
            // Attempt to initialize a new 'Session' object; alert user and exit on error
            Artist newArtist;
            if (null == (newArtist = new Artist()))
                return null;

            // Set the necessary value for each property in the artist object
            newArtist.UniqueId = txtUniqueId.Text;
            newArtist.StageName = txtStageName.Text;
            newArtist.FirstName = txtFirstName.Text;
            newArtist.LastName = txtLastName.Text;
            newArtist.PrimaryPhone = txtPrimaryPhone.Text;
            newArtist.SecondaryPhone = txtSecondaryPhone.Text;
            newArtist.Email = txtEmail.Text;
            newArtist.HomePageUrl = homePageLink.Text;
            newArtist.MyspaceUrl = myspaceLink.Text;
            newArtist.SoundclickUrl = soundclickLink.Text;
            newArtist.HourlySessionRate = txtRecordingRate.Text;
            newArtist.Notes = txtNotes.Text;

            if (sessionsList.Items.Count > 0)    // We have sessions
            {
                foreach (ListViewItem session in sessionsList.Items)
                    newArtist.SessionIdList.Add(session.SubItems[0].Text);
            }

            return newArtist;
        }

        public bool IsSelectionValid()
        {
            // Selection is valid
            if ((artistsTree.Nodes[0].Nodes.Count > 0) && (artistsTree.SelectedNode.Level > 0))
                return true;
            else    // Invalid
                return false;
        }

private void btnDelete_Click(object sender, EventArgs e)
        {
            Artist artist;
            if ((null != (artist = GetArtistData())) && (IsSelectionValid()))
            {
                DialogResult result = MessageBox.Show("Are you sure you want to delete the artist named \"" +
                    artist.StageName + "\"? This operation cannot be undone.", "L.M.E. - Delete Session",
                    MessageBoxButtons.YesNo, MessageBoxIcon.Error);

                // The user confirmed the choice to delete the artist
                if (result == DialogResult.Yes)
                {
                    MainForm parentForm;
                    if (null != (parentForm = (MainForm)this.MdiParent))
                    {
                        if (parentForm.DeleteArtist(artist))
                        {
                            // Remove the active artist from the tree
                            if (artist.UniqueId != string.Empty)
                            {
                                TreeNode node = artistsTree.SelectedNode;
                                if (node != null)
                                {
                                    artistsTree.Nodes[0].Nodes.Remove(node);
                                    // artistsTree.SelectedNode = artistsTree.Nodes[0];

                                    if (Artists.Count == 0)
                                        System.Diagnostics.Debug.WriteLine("Artists.Count = 0");

                                    foreach (Artist a in Artists)    // Loop through current artists
                                    {
                                        System.Diagnostics.Debug.WriteLine("Unique ID: " + a.UniqueId);
                                        if (a.UniqueId == artist.UniqueId)    // Found a match; remove the artist from the list
                                        {
                                            System.Diagnostics.Debug.WriteLine("Removed: " + a.UniqueId);
                                            System.Diagnostics.Debug.Write("\tArtist ID:" + artist.UniqueId);
                                            Artists.Remove(a);
                                            return;
                                        }
                                    }
                                }
                            }
                        }
                    }
                }
            }
        }


I have used the Debug.WriteLine and Debug.Write functions in order to (hopefully) find the problem. However, I have had no such luck and I am no closer to the solution than when I originally started to encounter this issue.

Here is the 'MainForm.DeleteArtist' method:

public bool DeleteArtist(Artist artistData)
        {
            if ((recordLabel == null) || (recordLabel.Artists == null) || (recordLabel.Artists.Count == 0))
                return false;

            // Find the artist in the current list
            foreach (Artist artist in recordLabel.Artists)
            {
                if (artist.UniqueId == artistData.UniqueId)
                {
                    recordLabel.Artists.Remove(artist);

                    // Save the label data
                    switch (recordLabel.SaveDataToDisk(CurrentDataFilePath))
                    {
                        case IoErrorCode.NoError:    // Notify the user that the file was saved successfully
                            sessionTimerForm.Artists = recordLabel.Artists;
                            sessionsForm.ClearArtistsList();
                            sessionsForm.Artists = recordLabel.Artists;

                            sessionTimerForm.RemoveArtist(artistData.StageName);

                            currentActionStatus.Text = "Data saved successfully.";
                            currentActionStatus.Image = Lab_Management_Environment.Properties.Resources.preferences;
                            return true;
                        default:
                            return false;
                    }
                }
            }

            return false;
        }


Now for the problem. I create two or three example artists and all goes well. It saves all the data appropriately and I can select each artist and the artist's data is displayed properly in all of the input fields.

Once I delete any artist (first, last, third, whatever) it empties my "List<artist> Artists;" object in the "ArtistsForm" class. I have made sure that it is only deleting the artist with the selected artist's ID, and it's correct.

However, it only gets to this point in the code (from "ArtistsForm"):

if (parentForm.DeleteArtist(artist))
                        {
                            // Remove the active artist from the tree
                            if (artist.UniqueId != string.Empty)
                            {
                                TreeNode node = artistsTree.SelectedNode;
                                if (node != null)
                                {
                                    artistsTree.Nodes[0].Nodes.Remove(node);
                                    // artistsTree.SelectedNode = artistsTree.Nodes[0];

                                    if (Artists.Count == 0)
                                        System.Diagnostics.Debug.WriteLine("Artists.Count = 0");


No matter what, it hits the "WriteLine" there. It seems to only happen after the call to "parentForm.DeleteArtist". Any suggestions? This is really irritating me. :-P But I've been patient with it and I can wait for a response. I'm in no hurry and this isn't a work project so I have no deadline. Big Grin | :-D

I apologize for any sloppy coding and such. I'm just trying to get this working before I clean it up. I don't really like looking at this code much anymore because my standard coding practice is quite neat and well put together (at least to me).
modified on Thursday, March 4, 2010 9:55 PM

AnswerRe: Removing All "List" Items Though It Shouldn't ... ?? Pin
Luc Pattyn4-Mar-10 14:54
sitebuilderLuc Pattyn4-Mar-10 14:54 
GeneralRe: Removing All "List" Items Though It Shouldn't ... ?? Pin
Matt U.4-Mar-10 15:19
Matt U.4-Mar-10 15:19 
GeneralRe: Removing All "List" Items Though It Shouldn't ... ?? Pin
Luc Pattyn4-Mar-10 15:27
sitebuilderLuc Pattyn4-Mar-10 15:27 
GeneralRe: Removing All "List" Items Though It Shouldn't ... ?? Pin
Matt U.4-Mar-10 15:56
Matt U.4-Mar-10 15:56 
GeneralRe: Removing All "List" Items Though It Shouldn't ... ?? Pin
Luc Pattyn4-Mar-10 16:06
sitebuilderLuc Pattyn4-Mar-10 16:06 
GeneralRe: Removing All "List" Items Though It Shouldn't ... ?? Pin
Matt U.5-Mar-10 11:37
Matt U.5-Mar-10 11:37 
GeneralRe: Removing All "List" Items Though It Shouldn't ... ?? Pin
Luc Pattyn5-Mar-10 12:00
sitebuilderLuc Pattyn5-Mar-10 12:00 
GeneralRe: Removing All "List" Items Though It Shouldn't ... ?? Pin
Matt U.5-Mar-10 12:04
Matt U.5-Mar-10 12:04 
QuestionChanging colors of an image Pin
Berlus4-Mar-10 9:34
Berlus4-Mar-10 9:34 
AnswerRe: Changing colors of an image Pin
Anubhava Dimri4-Mar-10 18:14
Anubhava Dimri4-Mar-10 18:14 
QuestionWinsock Library Recommendation? [modified] Pin
Richard Andrew x644-Mar-10 9:13
professionalRichard Andrew x644-Mar-10 9:13 
AnswerRe: Winsock Library Recommendation? Pin
snorkie4-Mar-10 10:22
professionalsnorkie4-Mar-10 10:22 
GeneralRe: Winsock Library Recommendation? Pin
Richard Andrew x644-Mar-10 10:29
professionalRichard Andrew x644-Mar-10 10:29 
AnswerRe: Winsock Library Recommendation? Pin
snorkie4-Mar-10 11:22
professionalsnorkie4-Mar-10 11:22 
GeneralRe: Winsock Library Recommendation? Pin
Richard Andrew x644-Mar-10 11:31
professionalRichard Andrew x644-Mar-10 11:31 
GeneralRe: Winsock Library Recommendation? Pin
snorkie4-Mar-10 16:30
professionalsnorkie4-Mar-10 16:30 
AnswerRe: Winsock Library Recommendation? Pin
Ennis Ray Lynch, Jr.4-Mar-10 11:41
Ennis Ray Lynch, Jr.4-Mar-10 11:41 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.