9 Replies - 1096 Views - Last Post: 24 October 2008 - 08:47 AM Rate Topic: -----

#1 ragingben  Icon User is offline

  • D.I.C Addict
  • member icon

Reputation: 177
  • View blog
  • Posts: 646
  • Joined: 07-October 08

Problem with class not in scope

Post icon  Posted 24 October 2008 - 05:06 AM

Hi everyone, I'm having a problem that is hopefully very simple to solve!

I have a program that uses a number of class libraries. Say I create a class in one class library (which happens to be an external controller) and I want to create an instance of the class and access its methods and properties from within the other classes of the same library how would I do it? Or is this a stupid thing to be doing this? Because if I create a instance of the class in the main program file it is obviously not in scope for the other classes. But these classes need to know about this class so that they can retrieve data from it.

I'm lost, please can someone point me in the right direction :)

Is This A Good Question/Topic? 0
  • +

Replies To: Problem with class not in scope

#2 gbertoli3  Icon User is offline

  • DIC at Heart + Code
  • member icon

Reputation: 41
  • View blog
  • Posts: 1,166
  • Joined: 23-June 08

Re: Problem with class not in scope

Posted 24 October 2008 - 06:47 AM

If it is a public class you should be able to use it.

EXAMPLE:
2 Clases, 1 is called MainClass, the other TestClass.
public class MainClass
{
    public void Main()
    {
        TestClass.TestMethod();
    }
}


Was This Post Helpful? 0
  • +
  • -

#3 ragingben  Icon User is offline

  • D.I.C Addict
  • member icon

Reputation: 177
  • View blog
  • Posts: 646
  • Joined: 07-October 08

Re: Problem with class not in scope

Posted 24 October 2008 - 07:03 AM

I can use the class at any point within any of the librarys, but what I want to be able to do is access an instance of the class from any point in the solution.

For Example

Solution
|
-ClassLibrary1
|
-Class myClass
|
|
-ClassLibrary2
|
-instance.whatever = whatever;
|
|
-ConsoleApplication1
|
-myClass instance = new myClass();
Was This Post Helpful? 0
  • +
  • -

#4 gbertoli3  Icon User is offline

  • DIC at Heart + Code
  • member icon

Reputation: 41
  • View blog
  • Posts: 1,166
  • Joined: 23-June 08

Re: Problem with class not in scope

Posted 24 October 2008 - 07:07 AM

What exactly isn't working? The Declaration for the myClass?
Was This Post Helpful? 0
  • +
  • -

#5 ragingben  Icon User is offline

  • D.I.C Addict
  • member icon

Reputation: 177
  • View blog
  • Posts: 646
  • Joined: 07-October 08

Re: Problem with class not in scope

Posted 24 October 2008 - 07:15 AM

No the decleration works fine and I can access all the methods and proerties fine from the application. but when you try to access the properties or methods from within the methods of anouther class it is not in scope, regardless if it is public or not.

Heres more on my problem....
I have a class (e.g. Lights) and another class (Controller).
Create instances of these -lights, controller in the console apllication, NOT the class library

Lights has a method (turn on) which should then set a property on controller to actually turn the lights on. However controller is out of scope, Controller class is not.
Was This Post Helpful? 0
  • +
  • -

#6 gbertoli3  Icon User is offline

  • DIC at Heart + Code
  • member icon

Reputation: 41
  • View blog
  • Posts: 1,166
  • Joined: 23-June 08

Re: Problem with class not in scope

Posted 24 October 2008 - 07:18 AM

Hmm...
I don't know or I don't understand. Hopefully PsychoCoder will come around and answer your problem or someone more knowledgeable than me.
Was This Post Helpful? 0
  • +
  • -

#7 eclipsed4utoo  Icon User is offline

  • Not Your Ordinary Programmer
  • member icon

Reputation: 1533
  • View blog
  • Posts: 5,972
  • Joined: 21-March 08

Re: Problem with class not in scope

Posted 24 October 2008 - 07:25 AM

View Postgbertoli3, on 24 Oct, 2008 - 10:18 AM, said:

Hmm...
I don't know or I don't understand. Hopefully PsychoCoder will come around and answer your problem or someone more knowledgeable than me.


I believe this is what he wants to do....

Say he has 15 forms, and 1 class. He wants to make an instance of Class1 on Form1, and use that instance in the other 14 forms.

Did I get that right, ragingben?

You could do this using a "Globals" class and use the Singleton method. Don't know if it's the best way, but it's doable.
Was This Post Helpful? 0
  • +
  • -

#8 ragingben  Icon User is offline

  • D.I.C Addict
  • member icon

Reputation: 177
  • View blog
  • Posts: 646
  • Joined: 07-October 08

Re: Problem with class not in scope

Posted 24 October 2008 - 07:38 AM

Yeh thats it! Sorry I havn't been explaining very well... I'll try again!

My problem is this: I am creating a new system for a machine that links up to a PC. The machine has a controller that controls all the motors, pnumatics, lights etc.

All the motors, pnumatics, lights etc are classes in one library, Components, and each has their own properties and methods. The controller is also a class, with its own methods, things like set output 1, return values basic stuff like that.

So I want to be able to make instances of all these classes so that I could do something like Motor.Move(position), or Lights.LightsOn() etc.

However by calling Motor.Move() I would need within the method for it to set a property of the Controller e.g Controller.Output1 = On

The thing is that the instance of the controller is a global thing -it is used not only by the Components class, but also by the other projects in this solution. They know that there is a object of Controller type, but I want to use a specific instance of the Controller from all the classes that refer to it.

Hope thats clearer :) thanks for both of your help
Was This Post Helpful? 0
  • +
  • -

#9 Jayman  Icon User is offline

  • Student of Life
  • member icon

Reputation: 421
  • View blog
  • Posts: 9,532
  • Joined: 26-December 05

Re: Problem with class not in scope

Posted 24 October 2008 - 08:41 AM

I'm not sure if I exactly understand but if all you need is the classes to be accessible from within each other, then put them all in the same Namespace. Then just add a reference to this Namespace in each class. As long as all the classes are in the same Namespace then you can instantiate any of the classes inside of another class because they will all be in the same scope.

Namespace

Hope that helps.
Was This Post Helpful? 0
  • +
  • -

#10 ragingben  Icon User is offline

  • D.I.C Addict
  • member icon

Reputation: 177
  • View blog
  • Posts: 646
  • Joined: 07-October 08

Re: Problem with class not in scope

Posted 24 October 2008 - 08:47 AM

Hey again guys, I figured it out wooohooo (I could give myself a pat on the back if I hadn't been so dumb in the first place!)

Any way heres my solution...

	public static class Contoller
	{
		public static String GetVer()
		{
			return esb.AAABuild.ToString();
		}

		static MintControllerClass esb = new MintControllerClass();

	}



So using this I can access the appropriate fields (providing I have set up methods) from any class library or project where the namespace containg the controller class is in scope!

Sorry for having a dumb moment there, tahnks for all your help :)
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1