473,386 Members | 1,812 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,386 software developers and data experts.

how can instantiate this?

82
Hi I am getting this error

An object reference is required for the non-static field, method, or property

eyes_and_wheels.servos[wheelLeft].Engaged = true;

I think I need to instantiate wheelLeft, but I've no idea how to. Can anybody help?

mrcw2
Jan 10 '11 #1
7 1512
GaryTexmo
1,501 Expert 1GB
Is that the entire error? It should tell you what it's referring to. Is this a compile error or a run-time error?

Instantiation means creating a new object of a set type. For example, if I'm instantiating a new List of strings, I'd do the following...

Expand|Select|Wrap|Line Numbers
  1. List<string> strList;
  2.  
  3. // Instantiate strList
  4. strList = new List<string>();
Jan 10 '11 #2
Curtis Rutland
3,256 Expert 2GB
Expand|Select|Wrap|Line Numbers
  1. eyes_and_wheels.servos[wheelLeft] = new Whatever();
Replace whatever with whatever the type is, and add parameters as necessary.

Of course, that assumes that class exposes a constructor. It could use a factory pattern. Without knowing what class it is, it's hard to say. But by and large, most have constructors so you should be fine.
Jan 10 '11 #3
mrcw
82
this is a working program. When button 0 is pressed on ifkit1 servo 6 on eyes_and_wheels moves

Expand|Select|Wrap|Line Numbers
  1. namespace Robot
  2. {
  3.     class Program
  4.     {
  5.        static AdvancedServo eyes_and_wheels; //Declare an servo object
  6.        static InterfaceKit ifKit1; //Declare an InterfaceKit object
  7.  
  8.        int wheelLeft = 6;
  9.  
  10.        ifKit1.InputChange += new InputChangeEventHandler(ifKit1_InputChange);  //Hook the phidget spcific event handlers
  11.  
  12.        eyes_and_wheels.open(99364); //opens eyes_and_wheels connection
  13.        eyes_and_wheels = null; // clear eyes_and_wheels from memory
  14.        eyes_and_wheels.close(); // close eyes_and_wheels connection
  15.     }
  16.  
  17.      static void ifKit1_InputChange(object sender, InputChangeEventArgs e)
  18.         {
  19.           if (e.Index == 0)
  20.  
  21.                     if (e.Value == true)
  22.                     {
  23.                         ifKit1.outputs[0] = true;
  24.  
  25.                         eyes_and_wheels.servos[6].Engaged = true; //wheelLeft
  26.  
  27.                     }
  28.         }
  29.  
to make it easier for me I changed the name of servo 6 to wheelLeft

Expand|Select|Wrap|Line Numbers
  1. namespace Robot
  2. {
  3.     class Program
  4.     {
  5.        static AdvancedServo eyes_and_wheels; //Declare an servo object
  6.        static InterfaceKit ifKit1; //Declare an InterfaceKit object
  7.  
  8.        int wheelLeft = 6;
  9.  
  10.        ifKit1.InputChange += new InputChangeEventHandler(ifKit1_InputChange);  //Hook the phidget spcific event handlers
  11.  
  12.        eyes_and_wheels.open(99364); //opens eyes_and_wheels connection
  13.        eyes_and_wheels = null; // clear eyes_and_wheels from memory
  14.        eyes_and_wheels.close(); // close eyes_and_wheels connection
  15.     }
  16.  
  17.      static void ifKit1_InputChange(object sender, InputChangeEventArgs e)
  18.         {
  19.           if (e.Index == 0)
  20.  
  21.                     if (e.Value == true)
  22.                     {
  23.                         ifKit1.outputs[0] = true;
  24.  
  25.                         eyes_and_wheels.servos[wheelLeft].Engaged = true; //wheelLeft
  26.  
  27.                     }
  28.         }
now I get the error "An object reference is required for the non-static field, method, or property " when I try and run the program. I think it needs to be instantiated, but I'm not sure. Any ideas?
Jan 12 '11 #4
GaryTexmo
1,501 Expert 1GB
I don't understand how this code works... your input change method is outside the scope of the class, how does it access those variables? According to what you wrote, ifKit1, eyes_and_wheels, and wheelLeft should not be defined and should give the appropriate errors.

I may be confused and/or not understanding something here... but the way I'm seeing it, the event handler is outside the scope of the Program class.
Jan 12 '11 #5
Curtis Rutland
3,256 Expert 2GB
This whole block is your problem:

Expand|Select|Wrap|Line Numbers
  1. class Program
  2.     {
  3.        static AdvancedServo eyes_and_wheels; //Declare an servo object
  4.        static InterfaceKit ifKit1; //Declare an InterfaceKit object
  5.  
  6.        int wheelLeft = 6;
  7.  
  8.        ifKit1.InputChange += new InputChangeEventHandler(ifKit1_InputChange);  //Hook the phidget spcific event handlers
  9.  
  10.        eyes_and_wheels.open(99364); //opens eyes_and_wheels connection
  11.        eyes_and_wheels = null; // clear eyes_and_wheels from memory
  12.        eyes_and_wheels.close(); // close eyes_and_wheels connection
  13.     }
This code obviously belongs in a method, but you are writing it inside a class! You can't just have executable code hanging out on its own like that. Classes are for declaring properties, fields, and methods.

I'm assuming this should be your Main method (the one that executes when the program starts).

So try doing something like this:
Expand|Select|Wrap|Line Numbers
  1. class Program
  2. {
  3.     private AdvancedServo eyes_and_wheels; //Declare an servo object
  4.     private InterfaceKit ifKit1; //Declare an InterfaceKit object
  5.  
  6.     static void Main(string[] args)
  7.     {
  8.         //this line may not work; instantiate it however you're supposed to
  9.         ifKitl = new InterfaceKit();
  10.  
  11.         int wheelLeft = 6;
  12.  
  13.         ifKit1.InputChange += new InputChangeEventHandler(ifKit1_InputChange);  //Hook the phidget spcific event handlers
  14.  
  15.         eyes_and_wheels.open(99364); //opens eyes_and_wheels connection
  16.         eyes_and_wheels = null; // clear eyes_and_wheels from memory
  17.         eyes_and_wheels.close(); // close eyes_and_wheels connection
  18.     }
  19. }
Note, I added a line to instantiate the object, but it may not work, since I don't know if the constructor requires parameters or not. Also, you'll eventually need to instantiate your AdvancedServo object as well.
Jan 12 '11 #6
mrcw
82
sorry I hate cut and paste it is bad when it copies wrong. The correct code is here, It gives the error when I try and run it

Expand|Select|Wrap|Line Numbers
  1. namespace Robot
  2. {
  3.     class Program
  4.     {
  5.        static InterfaceKit ifKit1; //Declare an InterfaceKit object     
  6.        static AdvancedServo eyes_and_wheels; //Declare an servo object
  7.        int wheelLeft = 6;
  8.  
  9.  
  10.       static void Main(string[] args)
  11.         {
  12.             try
  13.             {
  14.                 ifKit1 = new InterfaceKit(); //Initialize the InterfaceKit1 object
  15.                 eyes_and_wheels = new AdvancedServo(); //Initialize the AdvancedServo object
  16.  
  17.                 ifKit1.InputChange += new InputChangeEventHandler(ifKit1_InputChange);  //Hook the phidget spcific event handlers
  18.  
  19.                 ifKit1.open(117172); //Open the object for device connections              
  20.                 eyes_and_wheels.open(99364);
  21.  
  22.                 Console.Read();  //Wait for user input so that we can wait and watch for some event data from the phidget
  23.  
  24.                 ifKit1 = null;  //set the object to null to get it out of memory
  25.                 ifKit1.close(); //User input was rad so we'll terminate the program, so close the object              
  26.  
  27.                 eyes_and_wheels = null; // clear eyes_and_wheels from memory              
  28.                 eyes_and_wheels.close(); // close eyes_and_wheels connection
  29.             }
  30.         catch (PhidgetException ex)
  31.             {
  32.                 Console.WriteLine(ex.Description);
  33.             }
  34.     }
  35.  
  36.     static void ifKit1_InputChange(object sender, InputChangeEventArgs e)
  37.       {
  38.  
  39.             if (ifKit1.SerialNumber == 117172)
  40.             {
  41.                 if (e.Index == 0)
  42.  
  43.                     if (e.Value == true)
  44.                     {                      
  45.                         eyes_and_wheels.servos[wheelLeft].Engaged = true; //wheelLeft
  46.                     }               
  47.             }
  48.         }
Jan 13 '11 #7
Curtis Rutland
3,256 Expert 2GB
Lines 24 and 25 should be switched. Likewise for 27 and 28. First you set null, then you call close. You can't call close on null. It needs to be the other way around.

Actually, you don't need to set them to null at all. Your program is ending. The memory they took up will be automatically cleaned up by the garbage collector. And setting them to null doesn't actually free the memory anyway. It just makes those objects available for garbage collection.
Jan 13 '11 #8

Sign in to post your reply or Sign up for a free account.

Similar topics

2
by: Colin Mc Mahon | last post by:
Hi all, I currently use a class to interface with my databases, allowing me to insert, update, delete and retrieve records from the database as methods of the class. I have now created a...
5
by: Glenn Serpas | last post by:
I have Class A and Class B .. Class B has a private member that is a pointer to a Class A object. private: B *mypointer ; I instantiate the A object A* myobject new = A();
4
by: A. Gonzalez | last post by:
Hi everyone, Does anyone know how to instantiate, or create an XML document file (either using DOM or Readers) from an XML schema (xsd) file? I'm trying to develop an application that can...
9
by: the_grove_man | last post by:
I guess my question can go in two directions. I create applications that run multiple queries against a database. Generally speaking in the past I have used a Data Control (calling it dat1)...
1
by: Jason Lopez via .NET 247 | last post by:
I'm having a lot of trouble trying to instantiate a C++ classfrom a DLL in a C# application. The DLL was written in C++(Visual Studio 6.0). I have the full source code, as well as the compiled...
6
by: Chris Kennedy | last post by:
I have a dataset, datatable and data row which I have declared public in my class. I instantiate them in one sub routine and go to use them again and I get an 'Cannot find table 0' wrror Public...
2
by: Pyenos | last post by:
class model:pass class view: model() class controller: model() I can instantiate clsss model from inside class view but I can't instantiate class model from inside controller, due to the...
5
by: Pyenos | last post by:
class One: Two() #can't instantiate class Two: Three() #can't instantiate class Three:pass
3
by: y-man | last post by:
Hi, I am trying to get an overloaded operator to work inside the class it works on. The situation is something like this: main.cc: #include "object.hh" #include "somefile.hh" object obj,...
4
by: Tomas | last post by:
A newbie question: How can I instantiate objects dynamically in VB.NET. E.g. I have the object 'Player' and I would like to instantiate it with the several instances (James, Gunner, etc.), without...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.