disposing error

This is a discussion on disposing error within the C# Programming forums, part of the General Programming Boards category; Code: using System; using System.Windows.Forms; namespace WindowsApplication2 { public partial class Form1 : Form { protected int TextBoxCount = 5; ...

  1. #1
    Registered User
    Join Date
    Feb 2009
    Posts
    4

    Wink ineccessible due to protection level error

    Code:
    using System;
    using System.Windows.Forms;
    
    namespace WindowsApplication2
    {
        public partial class Form1 : Form
        {
           protected int TextBoxCount = 5; // number of TextBoxes on Form
    
            //enumeration constants specify textbox indices
            public enum TextBoxIndices
            {
                VIDEOAUDIO,
                TITLE,
                CATEGORY,
                CERTIFICATION,
                COPIES
            }
    
            //parameterless constructor
            public Form1()
            {
                InitializeComponent();
            }
            //end constructor
    
            //clear all TExtBoxes
            public void ClearTextBoxes()
            {
                //iterate through every control on form
                for (int i = 0; i < Controls.Count; i++)
                {
                    Control myControl = Controls[i]; // get control
    
                    //determine wheterh control is textbox
                    if (myControl is TextBox)
                    {
                        //clear text property (set to empty string )
                        myControl.Text = "";
                    }//end if
                }//end for
            }//end method ClearTextBoxes
    
            //set text box values to string array values
            public void SetTextBoxValues(string[] values)
            {
                //determine wethere string array has correct lenght
                if (values.Length != TextBoxCount)
                {
                    //throw exception if not correct length
                    throw (new ApplicationException(" There must be " +
                        (TextBoxCount + 1) + " string in the array"));
                }//end if
    
    
                //set array values if array has correct lenght
                else
                {
                    //seet array values to text box values
                    videoaudiocodeTextBox.Text = values[(int)TextBoxIndices.VIDEOAUDIO];
                    titleTextBox.Text = values[(int)TextBoxIndices.TITLE];
                    categoryTextBox.Text = values[(int)TextBoxIndices.CATEGORY];
                    certificationTextBox.Text = values[(int)TextBoxIndices.CERTIFICATION];
                    copiesTextBox.Text = values[(int)TextBoxIndices.COPIES];
                }//end else
            }//end method SetTExtValues
    
            //return text box values as string array
            public string[] GetTextBoxValues()
            {
                string[] values = new string[TextBoxCount];
    
                //copy text box fields to string array
                values[(int)TextBoxIndices.VIDEOAUDIO] = videoaudiocodeTextBox.Text;
                values[(int)TextBoxIndices.TITLE] = titleTextBox.Text;
                values[(int)TextBoxIndices.CATEGORY] = categoryTextBox.Text;
                values[(int)TextBoxIndices.CERTIFICATION] = certificationTextBox.Text;
                values[(int)TextBoxIndices.COPIES] = copiesTextBox.Text;
    
                return values;
            }
    
          
           
            }//end method
        }//end class





    Code:
    using System;
    using System.Collections.Generic;
    using System.Text;
    
    namespace WindowsApplication2
    {
       public class Class1
       {
          private int videoandaudio;
          private string title;
          private string category;
          private string certification;
          private int copies;
    
          //parameterless constructor sets members to default values
          public Class1()
             : this(0, "", "", "", 0)
          {
          }//end constructor
    
          //overloaded constructor sets members to parameter values 
          public Class1(int videoandaudioValue, string titleValue, string categoryValue,
              string certificationValue, int copiesValue)
          {
             Videoandaudio = videoandaudioValue;
             Title = titleValue;
             Category = categoryValue;
             Certification = certificationValue;
             Copies = copiesValue;
          }//end constructor
    
          //property that gets and sets account
          public int Videoandaudio
          {
             get
             {
                return videoandaudio;
             }//end get
    
             set
             {
                videoandaudio = value;
             }//endset
          }//end property Videoandaudio
    
          //property that gets and sets title
          public string Title
          {
             get
             {
                return title;
             }//end get
    
             set
             {
                title = value;
             }//endset
          }//end property title
    
          //property that gets and sets category
          public string Category
          {
             get
             {
                return category;
             }//end get
    
             set
             {
                category = value;
             }//endset
          }//end property category
    
          //property that gets and sets certification
          public string Certification
          {
             get
             {
                return certification;
             }//end get
    
             set
             {
                certification = value;
             }//endset
          }//end property certification
    
          //property that gets and sets coopies
          public int Copies
          {
             get
             {
                return copies;
             }//end get
    
             set
             {
                copies = value;
             }//endset
          }//end property copies
    
    
    
    
          }//end class
       }

    Code:
    using System;
    using System.Collections.Generic;
    using System.Text;
    using System.Windows.Forms;
    using System.IO;
    
    namespace WindowsApplication2
    {
       public partial class Class2 : Form1
       {
          private StreamWriter fileWriter; // writes datea to text file
          private FileStream output; // maintains connection  to file
    
            //pramaterless constructor
            public  Class2()
            {
                InitializeComponent();                 ----this has protection level      
                                                                                                         error       
            }//end constructor
    
            //event handelr for Save button
            private void saveButton_Click(object sender, EventArgs e)
            {
                //create dialog box enabling uer to save file
                SaveFileDialog fileChooser = new SaveFileDialog();
                DialogResult result = fileChooser.ShowDialog();
                string fileName; // name of file to save data
    
                fileChooser.CheckFileExists = false; // allow user to create file
    
                //exit event handler if user clicked "Cancel"
                if (result == DialogResult.Cancel)
                    return;
    
                fileName = fileChooser.FileName; // get specified file name
    
                //show error if user specified invalid file
                if (fileName == "" || fileName == null)
                    MessageBox.Show(" Invalid File Name", "Error",
                        MessageBoxButtons.OK, MessageBoxIcon.Error);
                else
                {
                    //save file via filestream if user specified valid file
                    try
                    {
                        //open ifle with write access
                        output = new FileStream(fileName,
                            FileMode.OpenOrCreate, FileAccess.Write);
    
                        //sets file to where data is written
                        fileWriter = new StreamWriter(output);
    
                        //disable save button and enable enter button
                        saveButton.Enabled = false;
                        enterButton.Enabled = true;                 ---these two have protection
                                                                                                                 error too
                    }//end try
    
                    //handle exception if ther is a problem opening the file
                    catch (IOException)
                    {
                        //notify user if file doesnot exist
                        MessageBox.Show("Error opening file", "error",
                          MessageBoxButtons.OK, MessageBoxIcon.Error);
                    }//end catch
                }//end else
            }//end method saveButton_click
    
            //handler for enterButton_click
            private void enterButton_Click(object sender, EventArgs e)
            {
                //store Textbox values string array
                string[] values = GetTextBoxValues();
    
                //record containg textbox values to serialize
                Class1 class1 = new Class1();
    
                //determine whether textbox account field is empty
                if ( values [ ( int ) TextBoxIndices.VIDEOAUDIO ] != "" )
                {
                    // store textbox values in record and serialize record
                    try
                    {
                        // get video and audio code value from textbox
                        int videoandaudioCode = Int32.Parse( values[ ( int ) TextBoxIndices.VIDEOAUDIO ] );
    
                        //determine wheterh videoandaudioCode is valid
                        if ( videoandaudioCode > 0 )
                        {
                            //store textbox fields in record
                            class1.Videoandaudio = videoandaudioCode;
                            class1.Title = values[ ( int ) TextBoxIndices.TITLE ];
                            class1.Category = values[ ( int ) TextBoxIndices.CATEGORY ];
                            class1.Certification = values[ ( int ) TextBoxIndices.CERTIFICATION ];
                            class1.Copies = Int32.Parse( values[ ( int ) TextBoxIndices.COPIES ]);
                        }//end if
                        else{
                            //notify user if invalid video and audio code
                            MessageBox.Show( " invalid code ", "error",
                                MessageBoxButtons.OK, MessageBoxIcon.Error ) ;
                        }//end else
                    }//endtry
                    //notify user if error occurs in seialization
                    catch ( IOException )
                    {
                        MessageBox.Show( "error writing to file" , "error",
                            MessageBoxButtons.OK, MessageBoxIcon.Error );
                    }//end catch
                    //notify user if eror occurs regarding parameter format
                    catch ( FormatException ) 
                    {
                        MessageBox.Show( "invalid format", "error",
                            MessageBoxButtons.OK, MessageBoxIcon.Error );
                    }//end catch
                }//end if
    
                ClearTextBoxes(); //clear textbox values
            }//end method enter button_ click
    
            //handler for exitButton Click
            private void exitButton_Click(object sender, EventArgs e)
            {
                //dtermine wheteher file exists
                if (output != null)
                {
                    try
                    {
                        fileWriter.Close(); //close streamwriter
                        output.Close(); //close file
    
                    }// end try
                    //notify user of error closing file
                    catch (IOException)
                    {
                        MessageBox.Show(" cannot close file", "error",
                            MessageBoxButtons.OK, MessageBoxIcon.Error);
                    }//end catch
                }//end if
    
                Application.Exit();
            }//end method
        }// end class
    }





    i based this code from my book for my project theyre pretty the same except for the names...now before i put the code for the class2 this program were running good...on the class2 there are errors saying "unaccessible due to protection level"
    and the underlined words are InitializeComponent(); ,saveButton.Enabled = false; and
    enterButton.Enabled



    Code:
    
    ............. public  Class2()
            {
                InitializeComponent();                 ----this has protection level      
                                                                                                         error       
            }//end constructor
    
            //event handelr for Save button
            private void saveButton_Click(object sender, EventArgs e)
            {
                //create dialog box enabling uer to save file
                SaveFileDialog fileChooser = new SaveFileDialog();
                DialogResult result = fileChooser.ShowDialog();
                string fileName; // name of file to save data
    
                fileChooser.CheckFileExists = false; // allow user to create file
    
                //exit event handler if user clicked "Cancel"
                if (result == DialogResult.Cancel)
                    return;
    
                fileName = fileChooser.FileName; // get specified file name
    
                //show error if user specified invalid file
                if (fileName == "" || fileName == null)
                    MessageBox.Show(" Invalid File Name", "Error",
                        MessageBoxButtons.OK, MessageBoxIcon.Error);
                else
                {
                    //save file via filestream if user specified valid file
                    try
                    {
                        //open ifle with write access
                        output = new FileStream(fileName,
                            FileMode.OpenOrCreate, FileAccess.Write);
    
                        //sets file to where data is written
                        fileWriter = new StreamWriter(output);
    
                        //disable save button and enable enter button
                        saveButton.Enabled = false;
                        enterButton.Enabled = true;                 ---these two have protection
                                                                                                                 error too
                    }//end try
    
    
    .................


    i think form1 has something to do with this with the word "protective int"
    iam not sure
    can someone explain why is this happen
    thx
    Last edited by dropper166; 03-30-2009 at 09:04 PM.

  2. #2
    Registered User
    Join Date
    Mar 2009
    Location
    england
    Posts
    209
    You've not actually stated what the error message says, or which particular line of the Dispose method it is crashing on. Nor have you said when the error occurs: when the program first starts... when it's closing?

    In my experience Dispose will throw an exception when a control within the form's collection has been set to null but still exists within the collection, but without further information it's impossible to say exactly why it's happening to you.

  3. #3
    the hat of redundancy hat nvoigt's Avatar
    Join Date
    Aug 2001
    Location
    Hannover, Germany
    Posts
    3,139
    The lines you underlined are probably accessing private members of your base class (Form1). You cannot do this. Either make them protected (not recommended) or write properties for Form1 that are public or protected and in turn set those private variables.
    hth
    -nv

    She was so Blonde, she spent 20 minutes looking at the orange juice can because it said "Concentrate."

    When in doubt, read the FAQ.
    Then ask a smart question.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Getting an error with OpenGL: collect2: ld returned 1 exit status
    By Lorgon Jortle in forum C++ Programming
    Replies: 6
    Last Post: 05-08-2009, 08:18 PM
  2. An error is driving me nuts!
    By ulillillia in forum C Programming
    Replies: 5
    Last Post: 04-04-2009, 09:15 PM
  3. Making C DLL using MSVC++ 2005
    By chico1st in forum C Programming
    Replies: 26
    Last Post: 05-28-2008, 01:17 PM
  4. Connecting to a mysql server and querying problem
    By Diod in forum C++ Programming
    Replies: 8
    Last Post: 02-13-2006, 09:33 AM
  5. Couple C questions :)
    By Divx in forum C Programming
    Replies: 5
    Last Post: 01-28-2003, 12:10 AM

Tags for this Thread


1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21