Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. C#
  4. An exception thrown that I can't figure out how to fix

An exception thrown that I can't figure out how to fix

Scheduled Pinned Locked Moved C#
csharphelptutorialhtmllinq
17 Posts 6 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • D Offline
    D Offline
    Darrall
    wrote on last edited by
    #1

    This is from a tutorial from Home & Learn: http://www.homeandlearn.co.uk/csharp/csharp_s13p3.html[^] The problem I am having is with an uninitialized variable but I can't figure out how to do it:

    using System;
    using System.Collections.Generic;
    using System.ComponentModel;
    using System.Data;
    using System.Drawing;
    using System.Linq;
    using System.Text;
    using System.Windows.Forms;

    namespace Multiple_Forms
    {
    public partial class Form1 : Form
    {
    public Form1()
    {
    InitializeComponent();
    }
    Form2 secondForm = new Form2();
    public static TextBox tb = new TextBox();

        private void btnFormTwo\_Click(object sender, EventArgs e)
        {
            secondForm.ShowDialog();
        }
    
        private void Form1\_Load(object sender, EventArgs e)
        {
            
            tb = txtChangeCase;
        }
    }
    

    }

    The problem is with the last line. I can see why...just can't figure out what to do to fix it. Anyone know the answer?

    L D 2 Replies Last reply
    0
    • D Darrall

      This is from a tutorial from Home & Learn: http://www.homeandlearn.co.uk/csharp/csharp_s13p3.html[^] The problem I am having is with an uninitialized variable but I can't figure out how to do it:

      using System;
      using System.Collections.Generic;
      using System.ComponentModel;
      using System.Data;
      using System.Drawing;
      using System.Linq;
      using System.Text;
      using System.Windows.Forms;

      namespace Multiple_Forms
      {
      public partial class Form1 : Form
      {
      public Form1()
      {
      InitializeComponent();
      }
      Form2 secondForm = new Form2();
      public static TextBox tb = new TextBox();

          private void btnFormTwo\_Click(object sender, EventArgs e)
          {
              secondForm.ShowDialog();
          }
      
          private void Form1\_Load(object sender, EventArgs e)
          {
              
              tb = txtChangeCase;
          }
      }
      

      }

      The problem is with the last line. I can see why...just can't figure out what to do to fix it. Anyone know the answer?

      L Offline
      L Offline
      Lost User
      wrote on last edited by
      #2

      Why is tb static? What is txtChangeCase? And what is the exception? (I can only think of 1 case where a direct assignment to a field (not through an indexer or anything like that) could throw an exception: when there is an implicit cast that throws an exception (which is rare))

      D 1 Reply Last reply
      0
      • L Lost User

        Why is tb static? What is txtChangeCase? And what is the exception? (I can only think of 1 case where a direct assignment to a field (not through an indexer or anything like that) could throw an exception: when there is an implicit cast that throws an exception (which is rare))

        D Offline
        D Offline
        Darrall
        wrote on last edited by
        #3

        This is a multi form Windows Application and apparently that code is to allow interaction between Form1 and Form2. If you'd like you can look at the whole lesson at the following site: http://www.homeandlearn.co.uk/csharp/csharp_s13p3.html[^]

        L L D 3 Replies Last reply
        0
        • D Darrall

          This is a multi form Windows Application and apparently that code is to allow interaction between Form1 and Form2. If you'd like you can look at the whole lesson at the following site: http://www.homeandlearn.co.uk/csharp/csharp_s13p3.html[^]

          L Offline
          L Offline
          Lost User
          wrote on last edited by
          #4

          That tutorial fails on so many levels that I would advice against ever even thinking about it, but I'd like someone else's opinion about that too I mean come on: - a static textbox as a way to communicate between forms (please no.) - creating a new textbox and then almost immediately afterward throwing it away without ever having used it (why?) - writing ==true (seriously, wtf, you might as well write if((x == true) == true && x != false) just to be very sure that x is indeed true) - referring to a computer program as "programme". huh? shows lack of knowledge of the jargon IMO, and I already got the impression that the writer of that tutorial is a noob (he also uses CreateGraphics inside the Paint event later on)

          L 1 Reply Last reply
          0
          • D Darrall

            This is from a tutorial from Home & Learn: http://www.homeandlearn.co.uk/csharp/csharp_s13p3.html[^] The problem I am having is with an uninitialized variable but I can't figure out how to do it:

            using System;
            using System.Collections.Generic;
            using System.ComponentModel;
            using System.Data;
            using System.Drawing;
            using System.Linq;
            using System.Text;
            using System.Windows.Forms;

            namespace Multiple_Forms
            {
            public partial class Form1 : Form
            {
            public Form1()
            {
            InitializeComponent();
            }
            Form2 secondForm = new Form2();
            public static TextBox tb = new TextBox();

                private void btnFormTwo\_Click(object sender, EventArgs e)
                {
                    secondForm.ShowDialog();
                }
            
                private void Form1\_Load(object sender, EventArgs e)
                {
                    
                    tb = txtChangeCase;
                }
            }
            

            }

            The problem is with the last line. I can see why...just can't figure out what to do to fix it. Anyone know the answer?

            D Offline
            D Offline
            Dan Mos
            wrote on last edited by
            #5

            that is not a tuturial. It's a what to avoid tutorial. linkig two forms by static fields => really bad any many other stuff. Don't use that site unless you want to become a rumorous c# developer.

            1 Reply Last reply
            0
            • D Darrall

              This is a multi form Windows Application and apparently that code is to allow interaction between Form1 and Form2. If you'd like you can look at the whole lesson at the following site: http://www.homeandlearn.co.uk/csharp/csharp_s13p3.html[^]

              L Offline
              L Offline
              Luc Pattyn
              wrote on last edited by
              #6

              I hadn't seen that site before, judging from that one page it isn't worth a penny, it being inaccurate and confusing. Buy and study a book, you may learn all you need and more in less than a week. This[^] explains my view on books. Trying to learn from a free site only gives you what you paid for, i.e. nothing much. :)

              Luc Pattyn [Forum Guidelines] [Why QA sucks] [My Articles]


              I only read code that is properly formatted, adding PRE tags is the easiest way to obtain that.
              [The QA section does it automatically now, I hope we soon get it on regular forums as well]


              A 1 Reply Last reply
              0
              • L Lost User

                That tutorial fails on so many levels that I would advice against ever even thinking about it, but I'd like someone else's opinion about that too I mean come on: - a static textbox as a way to communicate between forms (please no.) - creating a new textbox and then almost immediately afterward throwing it away without ever having used it (why?) - writing ==true (seriously, wtf, you might as well write if((x == true) == true && x != false) just to be very sure that x is indeed true) - referring to a computer program as "programme". huh? shows lack of knowledge of the jargon IMO, and I already got the impression that the writer of that tutorial is a noob (he also uses CreateGraphics inside the Paint event later on)

                L Offline
                L Offline
                Luc Pattyn
                wrote on last edited by
                #7

                harold aptroot wrote:

                he also uses CreateGraphics inside the Paint event

                he is to be shot on sight! :eek:

                Luc Pattyn [Forum Guidelines] [Why QA sucks] [My Articles]


                I only read code that is properly formatted, adding PRE tags is the easiest way to obtain that.
                [The QA section does it automatically now, I hope we soon get it on regular forums as well]


                1 Reply Last reply
                0
                • D Darrall

                  This is a multi form Windows Application and apparently that code is to allow interaction between Form1 and Form2. If you'd like you can look at the whole lesson at the following site: http://www.homeandlearn.co.uk/csharp/csharp_s13p3.html[^]

                  D Offline
                  D Offline
                  DaveyM69
                  wrote on last edited by
                  #8

                  Ditto the comments from the others. Probably the most correct way to acheive the result that the tutorial was aiming for would be to: 1. Create a Case enum. 2. Have a private field of type Case in the dialog form that is set according to the radio buttons and expose this field in a public read only property. 3. Check the DialogResult of the dialog form in the main form, if OK then read the property before disposing of the form. 4. Use the Case obtained from the property in a switch block and change the TextBox's Text property accordingly. What is it you were wanting to learn from that tutorial - changing the case of a string, using a dialog to get a user selection that may be returned to the instanciating form, or general inter-object communication?

                  Dave
                  BTW, in software, hope and pray is not a viable strategy. (Luc Pattyn)
                  Why are you using VB6? Do you hate yourself? (Christian Graus)

                  D 1 Reply Last reply
                  0
                  • L Luc Pattyn

                    I hadn't seen that site before, judging from that one page it isn't worth a penny, it being inaccurate and confusing. Buy and study a book, you may learn all you need and more in less than a week. This[^] explains my view on books. Trying to learn from a free site only gives you what you paid for, i.e. nothing much. :)

                    Luc Pattyn [Forum Guidelines] [Why QA sucks] [My Articles]


                    I only read code that is properly formatted, adding PRE tags is the easiest way to obtain that.
                    [The QA section does it automatically now, I hope we soon get it on regular forums as well]


                    A Offline
                    A Offline
                    AspDotNetDev
                    wrote on last edited by
                    #9

                    Luc Pattyn wrote:

                    Trying to learn from a free site only gives you what you paid for, i.e. nothing much.

                    I don't know, I can think of at least one site that is free and is a pretty good learning resource. It may not compare to a book, but it is a little more than "nothing much". :rolleyes:

                    [Forum Guidelines]

                    L 1 Reply Last reply
                    0
                    • A AspDotNetDev

                      Luc Pattyn wrote:

                      Trying to learn from a free site only gives you what you paid for, i.e. nothing much.

                      I don't know, I can think of at least one site that is free and is a pretty good learning resource. It may not compare to a book, but it is a little more than "nothing much". :rolleyes:

                      [Forum Guidelines]

                      L Offline
                      L Offline
                      Luc Pattyn
                      wrote on last edited by
                      #10

                      that must be an exceptional site then. :)

                      Luc Pattyn [Forum Guidelines] [Why QA sucks] [My Articles]


                      I only read code that is properly formatted, adding PRE tags is the easiest way to obtain that.
                      [The QA section does it automatically now, I hope we soon get it on regular forums as well]


                      1 Reply Last reply
                      0
                      • D DaveyM69

                        Ditto the comments from the others. Probably the most correct way to acheive the result that the tutorial was aiming for would be to: 1. Create a Case enum. 2. Have a private field of type Case in the dialog form that is set according to the radio buttons and expose this field in a public read only property. 3. Check the DialogResult of the dialog form in the main form, if OK then read the property before disposing of the form. 4. Use the Case obtained from the property in a switch block and change the TextBox's Text property accordingly. What is it you were wanting to learn from that tutorial - changing the case of a string, using a dialog to get a user selection that may be returned to the instanciating form, or general inter-object communication?

                        Dave
                        BTW, in software, hope and pray is not a viable strategy. (Luc Pattyn)
                        Why are you using VB6? Do you hate yourself? (Christian Graus)

                        D Offline
                        D Offline
                        Darrall
                        wrote on last edited by
                        #11

                        Thanks Dave. Basically the idea was to introduce using multiforms with the second form used to perform the changes on the first form. As you have all pointed out I am obviously using a poor source of information.

                        D 1 Reply Last reply
                        0
                        • D Darrall

                          Thanks Dave. Basically the idea was to introduce using multiforms with the second form used to perform the changes on the first form. As you have all pointed out I am obviously using a poor source of information.

                          D Offline
                          D Offline
                          DaveyM69
                          wrote on last edited by
                          #12

                          I'll knock you up a simple demo this weekend and post back here if that would be helpful. Colin Mackay has an article here[^] about passing data between forms. It's .NET 1.1 IIRC but the principles are still valid :thumbsup: In this situation, you only need the simpler Parent to Child relationship as that method can be used for both writing to and reading from properties. The Child to Parent is only needed when the Child needs to inform the parent when something has changed.

                          Dave
                          BTW, in software, hope and pray is not a viable strategy. (Luc Pattyn)
                          Why are you using VB6? Do you hate yourself? (Christian Graus)

                          D 1 Reply Last reply
                          0
                          • D DaveyM69

                            I'll knock you up a simple demo this weekend and post back here if that would be helpful. Colin Mackay has an article here[^] about passing data between forms. It's .NET 1.1 IIRC but the principles are still valid :thumbsup: In this situation, you only need the simpler Parent to Child relationship as that method can be used for both writing to and reading from properties. The Child to Parent is only needed when the Child needs to inform the parent when something has changed.

                            Dave
                            BTW, in software, hope and pray is not a viable strategy. (Luc Pattyn)
                            Why are you using VB6? Do you hate yourself? (Christian Graus)

                            D Offline
                            D Offline
                            Darrall
                            wrote on last edited by
                            #13

                            Thanks Dave...I'd appreciate that. You are the first one that has given me any kind of answer. I know the tutorial I am using is crappy but I'd still like to solve this particular problem. Thanks again. Darrall

                            D 1 Reply Last reply
                            0
                            • D Darrall

                              Thanks Dave...I'd appreciate that. You are the first one that has given me any kind of answer. I know the tutorial I am using is crappy but I'd still like to solve this particular problem. Thanks again. Darrall

                              D Offline
                              D Offline
                              DaveyM69
                              wrote on last edited by
                              #14

                              OK, this does the same as the tutorial but it does it correctly. I have included an example of both Parent to Child (Parent reads a property in the child) and Child to Parent (Parent listens for event that the Child raises). For a beginners tutorial to events, have a look at my Events Made Simple article[^]. Anyway, here's the code - any questions, just ask! I've included all the control creation and initialization so you can copy and paste and it should work without having to add any controls in the designer.

                              // Case.cs
                              public enum Case
                              {
                              None,
                              Upper,
                              Lower,
                              Proper
                              }

                              // FormChild.cs
                              using System;
                              using System.Drawing;
                              using System.Windows.Forms;

                              public partial class FormChild : Form
                              {
                              #region Events

                              public event EventHandler SelectedCaseChanged;
                              
                              #endregion
                              
                              #region Fields
                              
                              private Case selectedCase;
                              
                              /\* Would normally be in the Designer.cs file \*/
                              private RadioButton radioUpper;
                              private RadioButton radioLower;
                              private RadioButton radioProper;
                              private Button buttonOK;
                              private Button buttonCancel;
                              
                              #endregion
                              
                              #region Constructors
                              
                              public FormChild()
                              {
                                  InitializeComponent();
                                  CustomInitialize();
                              
                                  selectedCase = Case.None;
                                  radioUpper.Tag = Case.Upper;
                                  radioLower.Tag = Case.Lower;
                                  radioProper.Tag = Case.Proper;
                              }
                              
                              #endregion
                              
                              #region Properties
                              
                              /\* Public read only public property \*/
                              public Case SelectedCase
                              {
                                  get { return selectedCase; }
                                  private set
                                  {
                                      if (selectedCase != value)
                                      {
                                          selectedCase = value;
                                          OnSelectedCaseChanged(EventArgs.Empty);
                                      }
                                  }
                              }
                              
                              #endregion
                              
                              #region Methods
                              
                              private void RadioButton\_CheckedChanged(object sender, EventArgs e)
                              {
                                  RadioButton selectedRadio = sender as RadioButton;
                                  if (sender != null)
                                      SelectedCase = (Case)selectedRadio.Tag;
                              }
                              
                              protected virtual void OnSelectedCaseChanged(EventArgs e)
                              {
                                  EventHandler eh = SelectedCaseChanged;
                                  if (eh != null)
                                      eh(this, e);
                              }
                              
                              /\* Creates all controls and sets properties
                               \* Would normally be in the Deisgner.cs file \*/
                              
                              D 1 Reply Last reply
                              0
                              • D DaveyM69

                                OK, this does the same as the tutorial but it does it correctly. I have included an example of both Parent to Child (Parent reads a property in the child) and Child to Parent (Parent listens for event that the Child raises). For a beginners tutorial to events, have a look at my Events Made Simple article[^]. Anyway, here's the code - any questions, just ask! I've included all the control creation and initialization so you can copy and paste and it should work without having to add any controls in the designer.

                                // Case.cs
                                public enum Case
                                {
                                None,
                                Upper,
                                Lower,
                                Proper
                                }

                                // FormChild.cs
                                using System;
                                using System.Drawing;
                                using System.Windows.Forms;

                                public partial class FormChild : Form
                                {
                                #region Events

                                public event EventHandler SelectedCaseChanged;
                                
                                #endregion
                                
                                #region Fields
                                
                                private Case selectedCase;
                                
                                /\* Would normally be in the Designer.cs file \*/
                                private RadioButton radioUpper;
                                private RadioButton radioLower;
                                private RadioButton radioProper;
                                private Button buttonOK;
                                private Button buttonCancel;
                                
                                #endregion
                                
                                #region Constructors
                                
                                public FormChild()
                                {
                                    InitializeComponent();
                                    CustomInitialize();
                                
                                    selectedCase = Case.None;
                                    radioUpper.Tag = Case.Upper;
                                    radioLower.Tag = Case.Lower;
                                    radioProper.Tag = Case.Proper;
                                }
                                
                                #endregion
                                
                                #region Properties
                                
                                /\* Public read only public property \*/
                                public Case SelectedCase
                                {
                                    get { return selectedCase; }
                                    private set
                                    {
                                        if (selectedCase != value)
                                        {
                                            selectedCase = value;
                                            OnSelectedCaseChanged(EventArgs.Empty);
                                        }
                                    }
                                }
                                
                                #endregion
                                
                                #region Methods
                                
                                private void RadioButton\_CheckedChanged(object sender, EventArgs e)
                                {
                                    RadioButton selectedRadio = sender as RadioButton;
                                    if (sender != null)
                                        SelectedCase = (Case)selectedRadio.Tag;
                                }
                                
                                protected virtual void OnSelectedCaseChanged(EventArgs e)
                                {
                                    EventHandler eh = SelectedCaseChanged;
                                    if (eh != null)
                                        eh(this, e);
                                }
                                
                                /\* Creates all controls and sets properties
                                 \* Would normally be in the Deisgner.cs file \*/
                                
                                D Offline
                                D Offline
                                Darrall
                                wrote on last edited by
                                #15

                                Thank you so much for your time and effort. I have spent all day at this and finally got it to work but yours looks much tidier. I will try it out tomorrow and let you know. Thanks again. Darrall

                                D 1 Reply Last reply
                                0
                                • D Darrall

                                  Thank you so much for your time and effort. I have spent all day at this and finally got it to work but yours looks much tidier. I will try it out tomorrow and let you know. Thanks again. Darrall

                                  D Offline
                                  D Offline
                                  DaveyM69
                                  wrote on last edited by
                                  #16

                                  No problem :-D In addition to my article I previously linked to, I have posted a Tip/Trick here[^] which is a little more concise and should help in understanding the basics of raising/handling events.

                                  Dave
                                  BTW, in software, hope and pray is not a viable strategy. (Luc Pattyn)
                                  Why are you using VB6? Do you hate yourself? (Christian Graus)

                                  D 1 Reply Last reply
                                  0
                                  • D DaveyM69

                                    No problem :-D In addition to my article I previously linked to, I have posted a Tip/Trick here[^] which is a little more concise and should help in understanding the basics of raising/handling events.

                                    Dave
                                    BTW, in software, hope and pray is not a viable strategy. (Luc Pattyn)
                                    Why are you using VB6? Do you hate yourself? (Christian Graus)

                                    D Offline
                                    D Offline
                                    Darrall
                                    wrote on last edited by
                                    #17

                                    Thanks again :) With all your help I should come out on top...maybe

                                    1 Reply Last reply
                                    0
                                    Reply
                                    • Reply as topic
                                    Log in to reply
                                    • Oldest to Newest
                                    • Newest to Oldest
                                    • Most Votes


                                    • Login

                                    • Don't have an account? Register

                                    • Login or register to search.
                                    • First post
                                      Last post
                                    0
                                    • Categories
                                    • Recent
                                    • Tags
                                    • Popular
                                    • World
                                    • Users
                                    • Groups