+ Reply to Thread
Results 1 to 7 of 7

Thread: C#, passing variables between forms

  1. #1
    Barn Regular CCCSteve is on a distinguished road CCCSteve's Avatar
    Join Date
    Feb 2010
    Posts
    63
    Rep Power
    2

    C#, passing variables between forms

    Hello all,

    I have a hopefully simple issue. I am trying to take a large block of code and move it to its own CodeFile.cs so I can just reference it with 1 line instead of posting the same code multiple times.

    So I have my code file in place inside of CodeFile.cs which refers to textboxes and such on Form1. My problem is that my CodeFile.cs doesn't have the objects defined in there since they only exist in Form1. How can I fix this so it matches? Hope this made sense.

  2. #2
    Administrator richyrich is a splendid one to behold richyrich is a splendid one to behold richyrich is a splendid one to behold richyrich is a splendid one to behold richyrich is a splendid one to behold richyrich is a splendid one to behold richyrich is a splendid one to behold richyrich's Avatar
    Join Date
    Mar 2008
    Location
    Somewhere only we know...
    Posts
    3,207
    Blog Entries
    14
    Real Name
    Rich
    Rep Power
    14

    Not really sure what you mean. What is it you're trying to achieve?

  3. #3
    Barn Regular CCCSteve is on a distinguished road CCCSteve's Avatar
    Join Date
    Feb 2010
    Posts
    63
    Rep Power
    2

    Alright

    Alright so I have Form1.cs and CodeFile.cs (Add > New Item > Code File)

    I want to add multiple functions in CodeFile.cs and call them in Form1 so I don't have to paste multiple instances of the same code. Example

    [In CodeFile.cs]

    public void returnValue1 ()

    {
    int val1;

    A + B = C

    val1 = c;

    }


    [In Form1]

    Textbox1.Text = CodeFile.returnValue1.val1


    hope this clears it up.

  4. #4
    Barn Regular CCCSteve is on a distinguished road CCCSteve's Avatar
    Join Date
    Feb 2010
    Posts
    63
    Rep Power
    2

    After reading my post

    After reading my post I still don't think I am being clear in my needs so here is the test code I am trying out, perhaps it will help too.

    Code.cs
    Code:
    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;
    using System.Diagnostics;
    using System.Management;
    using System.Threading;
    using System.IO;
    
    namespace ServerReview
    {
        public class myGlobalClass
        {
    
            public static void test()
            {
              string report;
    
               report = "This is a test from Code.cs";
            }
    
    
        }
    }
    
    And Form 1

    Code:
        private void label7_Click(object sender, EventArgs e)
            {
    
                textbox1.Text = myGlobalClass.test.report();
               
            }
    

  5. #5
    Barn Regular CCCSteve is on a distinguished road CCCSteve's Avatar
    Join Date
    Feb 2010
    Posts
    63
    Rep Power
    2

    Got it

    Guess I had to just work through the problem myself because I got it.

    Code.cs

    Code:
    namespace ServerReview
    {
        public class myGlobalClass
        {
    
            public static string passTest()
            {
               string report = "This is a test from Code.cs";
               return report;
            }
    
    
        }
    }
    
    Form 1

    Code:
          private void label7_Click(object sender, EventArgs e)
            {
    
                textbox1.Text = myGlobalClass.passTest();
               
            }
    

  6. #6
    Administrator richyrich is a splendid one to behold richyrich is a splendid one to behold richyrich is a splendid one to behold richyrich is a splendid one to behold richyrich is a splendid one to behold richyrich is a splendid one to behold richyrich is a splendid one to behold richyrich's Avatar
    Join Date
    Mar 2008
    Location
    Somewhere only we know...
    Posts
    3,207
    Blog Entries
    14
    Real Name
    Rich
    Rep Power
    14

    It might be worth you reading this blog by JMH. It explains how to be an object oriented architecture, which it seems is basically what you're trying to do.

    A class is basically an object that can contain properties, constructors and methods. Properties contain the information about the object. Constructors tell .NET how an instance of the object can be created and methods allow you to manipulate the object.

    In your case, you might create your class like so:-
    Code:
        public class myGlobalClass
        {
            //this is a property
            public string report{get; set; }
    
            //this is a constructor
            public myGlobalClass(
                string Report
            )
            {
               this.report = Report;
            }
    
        }
    
    You can then create an instance of your class like so
    Code:
    myGlobalClass obj= new myGlobalClass("This is a test from Code.cs");
    
    Then, to retrieve the value, you can just call the property
    Code:
    obj.report;
    
    The benefit of classes is the ability to reuse them.

    Hope that helps.

  7. #7
    Ask Me About Dragons :D Shadow Wizard has a spectacular aura about Shadow Wizard has a spectacular aura about Shadow Wizard's Avatar
    Join Date
    Jul 2008
    Location
    Israel
    Posts
    795
    Blog Entries
    2
    Real Name
    Yahav
    Rep Power
    5

    using static methods from external file is one good solution.

    if your need is just split your code to several files, you can use the "partial" keyword:
    --File1.cs
    Code:
    public partial class MyFormClass
    {
       ....declaring objects and elements....
    }
    
    --File2.cs
    Code:
    public partial class MyFormClass
    {
       ....using the objects and elements declared in File1.cs....
    }
    
    you can have more than one file using the same class name, thus allowing
    several programmers to work on the same class in the same time.

+ Reply to Thread

Similar Threads

  1. VB6 MODAL and MODELESS forms?
    By theChris in forum Visual Basic Programming
    Replies: 2
    Last Post: December 21st, 2010, 09:59 PM
  2. in visual C# moving to different forms
    By arloguth in forum .NET Development
    Replies: 0
    Last Post: June 5th, 2009, 10:07 PM
  3. Manipulating data with variables
    By nboscaino in forum Microsoft Access
    Replies: 19
    Last Post: December 18th, 2008, 01:32 PM
  4. Looping through variables.
    By mehere in forum SQL Development
    Replies: 2
    Last Post: June 4th, 2008, 01:11 PM

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts

SEO by vBSEO