You are viewing our Forum Archives. To view or take place in current topics click here.
Help [C#] How to Call From a Class?
Posted:

Help [C#] How to Call From a Class?Posted:

Vectorizing
  • Prospect
Status: Offline
Joined: Jun 10, 201014Year Member
Posts: 648
Reputation Power: 27
Status: Offline
Joined: Jun 10, 201014Year Member
Posts: 648
Reputation Power: 27
okay i have no idea how to describe this so i will just type an example.


Form1.cs Contains:

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 Form1
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }
    }
}


Class1.cs Contains:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace Form1
{
    class Class1
    {
        bool test = true;
    }
}


How can I use the bool variable "test" in form1? whenever i try to i get errors, like so

Here is what im trying to do:

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 Form1
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        private void test()
        {
            if (test == false)
            {
                MessageBox.Show("Hi");
            }
        }
    }
}



Thanks. in other words, how can i use a bool variable (or any other variable for that matter) from a class i made?


Thanks
#2. Posted:
Imp
  • Shoutbox Hero
Status: Offline
Joined: Jan 01, 201113Year Member
Posts: 1,957
Reputation Power: 401
Status: Offline
Joined: Jan 01, 201113Year Member
Posts: 1,957
Reputation Power: 401
Its all about Scopes.

Your test boolean variable, in the way you coded it, is local to your own class. If you wanted to get the value of the variable there are a couple of ways to do it.

Either;

Retrieve the value of test from a public function in your Class.



using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace Form1
{
    class Class1
    {
        bool test = true;

        public bool GetTest()
        {
            return test;
        }
    }
}


Then in Form1


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 Form1
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        private void test()
        {
            if (Class1.GetTest() == false)
            {
                MessageBox.Show("Hi");
            }
        }
    }
}


Or just declare the test boolean and make it public.


using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace Form1
{
    class Class1
    {
        public bool test = true;
    }
}



Then do almost the same as before


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 Form1
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        private void test()
        {
            if (Class1.test == false)
            {
                MessageBox.Show("Hi");
            }
        }
    }
}

#3. Posted:
lee74saurusr3x
  • Ladder Climber
Status: Offline
Joined: Feb 18, 201014Year Member
Posts: 333
Reputation Power: 12
Status: Offline
Joined: Feb 18, 201014Year Member
Posts: 333
Reputation Power: 12
Yeah, just make it public.
#4. Posted:
ODST_107
  • Resident Elite
Status: Offline
Joined: Oct 07, 201014Year Member
Posts: 247
Reputation Power: 9
Status: Offline
Joined: Oct 07, 201014Year Member
Posts: 247
Reputation Power: 9
Whenever making cross class calls you have to setup and invoke them or else you will get cross thread errors and it will cause you a lot of problems. For accessing a variable on another class I would setup a function in that class that returns the value of the variable that you want. Here's an example:

Form1.cs:
bool getMe;
private void Form1_Load(object sender, EventArgs e){
getMe = FALSE;
}
public bool getVariable1(){
return getMe;
}

Class1.cs:
Form1 caller;
bool begotten;
public Class Class1{
public void Class1(Form1 x){//to get the proper variable we have to give it the form we're //using

caller = x;
begotten = caller.getVariable1();
}
}


Theres some invoke code that should go into the Form1.cs code to check if the call needs an invoke and if so how to execute it, but I can't remember it right of the top of my head. Just look up cross thread referencing in C# or something along those lines.
#5. Posted:
Vectorizing
  • Prospect
Status: Offline
Joined: Jun 10, 201014Year Member
Posts: 648
Reputation Power: 27
Status: Offline
Joined: Jun 10, 201014Year Member
Posts: 648
Reputation Power: 27
-Rango- wrote Its all about Scopes.

Your test boolean variable, in the way you coded it, is local to your own class. If you wanted to get the value of the variable there are a couple of ways to do it.

Either;

Retrieve the value of test from a public function in your Class.



using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace Form1
{
    class Class1
    {
        bool test = true;

        public bool GetTest()
        {
            return test;
        }
    }
}


Then in Form1


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 Form1
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        private void test()
        {
            if (Class1.GetTest() == false)
            {
                MessageBox.Show("Hi");
            }
        }
    }
}


Or just declare the test boolean and make it public.


using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace Form1
{
    class Class1
    {
        public bool test = true;
    }
}



Then do almost the same as before


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 Form1
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        private void test()
        {
            if (Class1.test == false)
            {
                MessageBox.Show("Hi");
            }
        }
    }
}



I did all that and here is what i got:

[ Register or Signin to view external links. ]
#6. Posted:
XG_R4PiDzZ
  • Junior Member
Status: Offline
Joined: Sep 28, 201014Year Member
Posts: 82
Reputation Power: 3
Status: Offline
Joined: Sep 28, 201014Year Member
Posts: 82
Reputation Power: 3
Vectorizing wrote
I did all that and here is what i got:

[ Register or Signin to view external links. ]


You need to create and instance of the class.


Class1 yourClass = new Class1();
Jump to:
You are viewing our Forum Archives. To view or take place in current topics click here.