Go Back   Computer Forums > General Computing > Programming
Click Here to Login
Join Computer forums Today


Reply
 
Thread Tools Search this Thread Display Modes
 
Old 05-13-2014, 08:15 AM   #21
..m.0,0.m..
Site Team
 
iPwn's Avatar
 
Join Date: May 2010
Location: USA
Posts: 3,870
Default Re: Noob needs a little help with C#

Quote:
Originally Posted by BikerEcho View Post
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).
__________________

__________________
Me: You'd think as the dominant species we wouldn't be so effing stupid.
J: We're just intelligent enough to be completely effing stupid.
iPwn is offline   Reply With Quote
Old 05-13-2014, 09:34 AM   #22
Fully Optimized
 
BikerEcho's Avatar
 
Join Date: Feb 2011
Location: Denmark
Posts: 4,013
Default Re: Noob needs a little help with C#

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.
__________________

__________________
MOBO: Asus Maximus 6 Hero - GPU: Gigabyte GTX 980ti xtreme - CPU: I7-4770K - PSU: Corsair AX760
RAM: Corsair Vantage 2x4GB - SSD: Samsung 840 EVO 512 + LiteOn LCS-256 - Case: NZXT H440
BikerEcho is offline   Reply With Quote
Old 05-13-2014, 10:32 AM   #23
..m.0,0.m..
Site Team
 
iPwn's Avatar
 
Join Date: May 2010
Location: USA
Posts: 3,870
Default Re: Noob needs a little help with C#

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();
             
        }
    }
}
__________________
Me: You'd think as the dominant species we wouldn't be so effing stupid.
J: We're just intelligent enough to be completely effing stupid.
iPwn is offline   Reply With Quote
Old 05-13-2014, 12:11 PM   #24
Fully Optimized
 
BikerEcho's Avatar
 
Join Date: Feb 2011
Location: Denmark
Posts: 4,013
Default Re: Noob needs a little help with C#

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; } }
__________________
MOBO: Asus Maximus 6 Hero - GPU: Gigabyte GTX 980ti xtreme - CPU: I7-4770K - PSU: Corsair AX760
RAM: Corsair Vantage 2x4GB - SSD: Samsung 840 EVO 512 + LiteOn LCS-256 - Case: NZXT H440
BikerEcho is offline   Reply With Quote
Old 05-13-2014, 12:38 PM   #25
..m.0,0.m..
Site Team
 
iPwn's Avatar
 
Join Date: May 2010
Location: USA
Posts: 3,870
Default Re: Noob needs a little help with C#

Quote:
Originally Posted by BikerEcho View Post
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)

Quote:
Originally Posted by BikerEcho View Post
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.
__________________
Me: You'd think as the dominant species we wouldn't be so effing stupid.
J: We're just intelligent enough to be completely effing stupid.
iPwn is offline   Reply With Quote
Old 05-13-2014, 01:03 PM   #26
Fully Optimized
 
BikerEcho's Avatar
 
Join Date: Feb 2011
Location: Denmark
Posts: 4,013
Default Re: Noob needs a little help with C#

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?
__________________
MOBO: Asus Maximus 6 Hero - GPU: Gigabyte GTX 980ti xtreme - CPU: I7-4770K - PSU: Corsair AX760
RAM: Corsair Vantage 2x4GB - SSD: Samsung 840 EVO 512 + LiteOn LCS-256 - Case: NZXT H440
BikerEcho is offline   Reply With Quote
Old 05-13-2014, 01:26 PM   #27
..m.0,0.m..
Site Team
 
iPwn's Avatar
 
Join Date: May 2010
Location: USA
Posts: 3,870
Default Re: Noob needs a little help with C#

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)
__________________

__________________
Me: You'd think as the dominant species we wouldn't be so effing stupid.
J: We're just intelligent enough to be completely effing stupid.
iPwn is offline   Reply With Quote
Reply

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off



All times are GMT -5. The time now is 02:55 AM.


Powered by vBulletin® Version 3.8.8 Beta 4
Copyright ©2000 - 2017, vBulletin Solutions, Inc.
Search Engine Friendly URLs by vBSEO 3.6.0