Noob needs a little help with C#

How the hell do i call a function i have made in form1 from form3?

Berry might have different feedback (and you should listen to him over me every single time), but I would suggest you take those shared functions and place them in a .cs page. You could call this page MyTools.cs.

Then, you need to initiate a new instance of MyTools, which I like to use private/public combo's in my code. Someone else could probably explain the benefits and drawbacks, I just know this is how I create giant containers of tools and processes and then call them.
Code:
private static readonly MyTools _helper = new MyTools();
public static MyTools HelpMe { get { return _helper; } }

[...]

public void SomeFunction() {
     HelpMe.Calculate();
}

This is preferrable over calling a method from Form1 because if you do so, you will either have to make it static (which changes how it behaves there too) or call Form1 another way (like a 'cast as' call).
 
sounds like a good idea to have a separate .cs file with all the custom functions.

Can you edit this code so it would work?

Form1.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;

namespace TestApp
{
    public partial class FormMain : Form
    {
        public FormMain()
        {
            InitializeComponent();
        }

        private void FormMain_Load(object sender, EventArgs e)
        {
            ClickFunction();

        }

        private void button1_Click(object sender, EventArgs e)
        {
            label1.Text = "it works";

        }
    }
}

Tools.cs
Code:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace TestApp
{
    class Tools
    {
        ClickFunction()
        {
             button1.PerformClick();
        }

    }
}


This app should make the lable1.text = it works automatically on startup.
 
Last edited:
Setting Labels on startup go in your 'constructor'

The concept of using a separate class is a little different, but can still work as you asked.

Form1.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;

namespace TestApp
{
    private static readonly Tools _helpme = new Tools();
    public static HelpMe { get { return _helpme; } }

    public partial class FormMain : Form
    {
        public FormMain() {
            //This is where you put startup calls...

            label1.Text = HelpMe.InitialTextValue;

            InitializeComponent();
        }

        private void button1_Click(object sender, EventArgs e) {
            
            //This is an example of using a Tools class
            var tryParse = HelpMe.CalculateAverage(inputField1.Text, inputField2.Text);

            if(tryParse == "FAIL") {
                 //Do something that lets the user know they're stupid
                 return; //Go back to the form
            }
            
            //If we get here, the above call worked and we can set the value to whatever call was
            resultLabel.Text = tryParse;

        
           //   Should only set values after eval, but the Click event can call any 
           // class from Tools through the "HelpMe" container.

        }
    }
}

Tools.cs
Code:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace TestApp {
    class Tools {
        
        public readonly string InitialTextValue = "it works";

        public string CalculateAverage(string one, string two) {

          int firstNumber;
          int secondNumber;

          var ParseOne = Int32.TryParse(one, out firstNumber);
          var ParseTwo = Int32.TryParse(two, out secondNumber);
          //These return "true" if the numbers were parsed

          //So kick back something we know if it fails.
          if (!ParseOne || !ParseTwo)
              return "FAIL";

          //Return the real result if okay
          return ((firstNumber + secondNumber)/2).ToString();
             
        }
    }
}
 
Last edited:
I appreciate the help.
Unfortunately It gives me a build error when i copy paste it into my project.

I am also have trouble understanding this code

Code:
private static readonly Tools _helpme = new Tools();
public static HelpMe { get { return _helpme; } }
 
Unfortunately It gives me a build error when i copy paste it into my project.

What is the error in the ErrorList window? (View > ErrorList)

I am also have trouble understanding this code

Code:
private static readonly Tools _helpme = new Tools();
public static HelpMe { get { return _helpme; } }

You're creating a public property (HelpMe) that is returning to you the Tools class. It allows you to use the Tools.cs page like it is an object on the page. Any method or variable you put in Tools will be accessible through the HelpMe object.

You can set variables with just a "{get; set;}" which just allow info to be read from / written to. This allows only a read from the tools class, but still allows processing.

One (of many) caution here is, you don't want to use that method if you need to initiate a class on each form. (e.g. Form1, Form2 and Form3 all have that code) because each time the form is opened, Tools is reinitialized. If you're storing data in it, or changing values, you lose them each time that form is loaded.

In that scenario, you might have to declare/access as static.
 
interesting.

When i hit play a message pubs up that say "There were build errors"

I created a new c# windows form project.
I copied all your form1.cs code and replaced everything with it.
I then chose "add new item". I chose class and named it "Tools".
Then copied all your Tools code into it. am i doing anything wrong here?
 
No, but I think you're missing a reference somewhere. I am currently installing VS 2005 to fix some old reporting project, so I can't get into Visual Studio atm.

However,
Copying ALL code is never a good idea... you're changing namespace declarations which cause other issues.

The process of adding a tools page is accurate.

I suggest doing the same thing and make a class called BikersTools. Next, in the Form, try to reference that class ("var somethinghere = new BikersTools();") and see if it asks for a reference? (e.g. "using thisApp.BikersTools;" not literal)
 
Back
Top Bottom