How to insert value into the class?

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • aeris
    New Member
    • Dec 2009
    • 18

    How to insert value into the class?

    Sorry to bothering you all. Can someone give me a hand here? How to insert value from a form into a class?

    I have a form, called EnterKeyWindow. cs.

    Code:
            public CarrierFile carrierFile;
    
            public EnterKeyWindow()
            {
    	InitializeComponent();
    	this.carrierFile = new CarrierFile(imageSourceLocation,String.Empty, 0, 0);
             }
    
            public EnterKeyWindow(CarrierFile carrierFile):this()
            {
    	this.carrierFile = carrierFile;
            }
    
            private void EnterKeyWindow_Load(object sender, EventArgs e)
            {
                FillCarrierFile();
            }
    
            private void FillCarrierFile()
            {            
                carrierFile.SourceFileName = imageSourceLocation;
                carrierFile.DestinationFileName = textBox1.Text;
                carrierFile.CountBytesToHide = 0;
            }
    Then I have another .cs, called CarrierFile.cs

    Code:
     public class CarrierFile
        {
            public String SourceFileName;
            public String DestinationFileName;
            public Int32 CountBytesToHide;
            
            public CarrierFile(String sourceFileName, String destinationFileName, Int32 countBytesToHide, int CountBitsToHidePerCarrierUnit)
            {
                this.SourceFileName = sourceFileName;
                this.DestinationFileName = destinationFileName;
                this.CountBytesToHide = countBytesToHide;
            }
    But then, it can run, but the value actually fail to insert into CarrierFile.cs. May I know what is the code to insert the value into the class??

    Thanks in advance,
    By
    Aeris.
  • tlhintoq
    Recognized Expert Specialist
    • Mar 2008
    • 3532

    #2
    Originally posted by OriginalPoster
    How do I get my Form2 to react to something on my Form1?
    How do I make my Form1 control something on my Form2?
    Although you can have Form1 directly access items on Form2 it isn't the recommended way to go. It ties the two forms tightly to each other. If a change is made in Form2 such as removing one of the controls, then code in Form1 breaks.
    It is better to Form1 raise an event, and have Form2 contain its own code for how to react to this. This places responsibility for Form2 within Form2, and Form1 within Form1.
    It keeps Form1 blissfully ignorant of Form2 - and a logging component - and a progress component, and a dozen other little black boxes that can be subscribed to events in Form1, all without Form1 being made responsible for directly affecting controls other than itself.
    Events tutorial (including Form to Form which is the same as class to class)
    This tutorial for a cash register does exactly that: It makes a virtual numeric keyboard.
    Bad: Directly accessing controls of one class/form from another.
    Code:
    bool IsOn = Form2.button1.IsChecked;


    Good: Use a property to get such information
    Code:
    //Form1
    bool IsOn = Form2.IsOn;
    Code:
    //Form 2
    public bool IsOn
    {
       get { return button1.Checked; }
       set { button1.Checked = value; }
    }
    It's a subtle but important difference as your applications become more complex. Using properties means your target class/form (Form2) can be changed and updated a thousand different ways yet won't negatively impact the classes that are reading from it. If you change the name of a control for example: No break in all your other classes. If your target form evolves where it needs to do 10 things when it is turned on, then the responsibility stays within Form2, and that burden on not put on Form1 and a dozen other forms that might be using Form2. The goal is to compartimentali ze the work so that Form2 is responsiblity SOLELY for Form2. From1 should only have to say "Turn on" or "Turn Off"

    Code:
    Form2
    public bool IsOn
    {
       get { return btnMeaningfulName.Checked; }
       set {
                btnMeaningfulName.Checked = value;
                panelDashboard.Visible = value;
                labelStatus = value == true ? "On" : "Off";
                btnRunNow.Enabled = value;
    }
    Now when Form1 tells Form2 to turn on, Form2 will check a box, make an entire panel visible, change its Status label to say 'On' and enable a Run Now button.

    Comment

    • GaryTexmo
      Recognized Expert Top Contributor
      • Jul 2009
      • 1501

      #3
      I don't understand what you mean by the value fails to insert into CarrierFile.cs. .. you mean assigning the value to one of the properties doesn't do anything?

      Which property isn't getting set, and what's the sequence of calls that are happening here? For example, which constructor happens to be called in your program flow right now?

      *Edit: Ohhh! I think I might see the problem. You've got your constructor that takes a CarrierFile object as a paramater also calling the parameterless constructor. So you assign the local carrierFile member, then immediately destroy it by creating a new object.

      This ...
      Code:
              public EnterKeyWindow(CarrierFile carrierFile):this()
              {
          this.carrierFile = carrierFile;
              }
      ... should be changed to this ...
      Code:
              public EnterKeyWindow(CarrierFile carrierFile)
              {
          this.carrierFile = carrierFile;
              }

      Comment

      • aeris
        New Member
        • Dec 2009
        • 18

        #4
        Ya i mean failed to assigning the value to the SourceFileName in the CarrierFile.cs class.

        I don't know how to assign a value to the class from a Form.

        Comment

        • GaryTexmo
          Recognized Expert Top Contributor
          • Jul 2009
          • 1501

          #5
          Please see my edit, that might help you out. Also, T's post should be a great help for you in general, read through it. He's also got some great articles in the insights section that you should check out :)

          Comment

          • aeris
            New Member
            • Dec 2009
            • 18

            #6
            It is still the same eventhough i remove the ":this() ".
            I don't know what is wrong with my codes... I've been fixing it long ago, but still can not assign the value to the class...

            Some one please help...

            Comment

            • tlhintoq
              Recognized Expert Specialist
              • Mar 2008
              • 3532

              #7
              Ok... a critique on the code and a possible source of the problem.

              This is asking for confusion
              Code:
              public EnterKeyWindow(CarrierFile carrierFile):this()
                      {
                  this.carrierFile = carrierFile;
                      }
              Variable names are free. Why reuse something in this manner
              Code:
              public EnterKeyWindow(CarrierFile IncomingcarrierFile)
              {
                  carrierFile = IncomingcarrierFile;
              }
              Personally, I wouldn't create a class named "EnterKeyWindow " because it sounds like a verb/method name. It *sounds* like the method that will execute when you enter [into] the KeyWindow.

              I suspect the real problem of not passing values is here:
              Code:
              private void FillCarrierFile()
              {            
                    carrierFile.SourceFileName = imageSourceLocation;
                    carrierFile.DestinationFileName = textBox1.Text;
                    carrierFile.CountBytesToHide = 0;
              }
              This is a class named "EnterKeyWindow " right? Must be since you have a constructor for it. Where does this textBox1 come from? Is it part of EnterKeyWindow?

              Based on your question
              How to insert value from a form into a class?
              I have to assume that textbox1 is in the form and EnterKeyWindow is the class.

              Which means you are tightly tieing textbox1 of form1 to the class of EnterKeyWindow. STOP! Don't do that. Bad habit to get in to.
              First: This presumes that EnterKeyWindow even knows of form1, let alone textbox1. I presume that you aren't getting the value passed because of this reason: This *instance* of the class has no knowledge of that instance of the form1.
              Second: It breaks as soon as you rename textbox1 to something human-friendly like "tbCustomerName "
              Third: It means your EnterKeyWindow class can't be used/re-used for any other purpose but to work on this form. If you are going to do that just stick the code in the form1.cs and simplify the solution.

              If you want to pass a value from one class to anther then that is what you should do: Pass it deliberatley, not through assumption that one can see the other. When you make your EnterKeyWindow pass the textbox1.text to the new class instance.

              Code:
              EnterKeyWindow bob = new EnterKeyWindow();
              bob.DestinationPath = textbox1.text;

              Comment

              Working...