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
7 1512
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... - List<string> strList;
-
-
// Instantiate strList
-
strList = new List<string>();
- 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.
this is a working program. When button 0 is pressed on ifkit1 servo 6 on eyes_and_wheels moves - namespace Robot
-
{
-
class Program
-
{
-
static AdvancedServo eyes_and_wheels; //Declare an servo object
-
static InterfaceKit ifKit1; //Declare an InterfaceKit object
-
-
int wheelLeft = 6;
-
-
ifKit1.InputChange += new InputChangeEventHandler(ifKit1_InputChange); //Hook the phidget spcific event handlers
-
-
eyes_and_wheels.open(99364); //opens eyes_and_wheels connection
-
eyes_and_wheels = null; // clear eyes_and_wheels from memory
-
eyes_and_wheels.close(); // close eyes_and_wheels connection
-
}
-
-
static void ifKit1_InputChange(object sender, InputChangeEventArgs e)
-
{
-
if (e.Index == 0)
-
-
if (e.Value == true)
-
{
-
ifKit1.outputs[0] = true;
-
-
eyes_and_wheels.servos[6].Engaged = true; //wheelLeft
-
-
}
-
}
-
to make it easier for me I changed the name of servo 6 to wheelLeft - namespace Robot
-
{
-
class Program
-
{
-
static AdvancedServo eyes_and_wheels; //Declare an servo object
-
static InterfaceKit ifKit1; //Declare an InterfaceKit object
-
-
int wheelLeft = 6;
-
-
ifKit1.InputChange += new InputChangeEventHandler(ifKit1_InputChange); //Hook the phidget spcific event handlers
-
-
eyes_and_wheels.open(99364); //opens eyes_and_wheels connection
-
eyes_and_wheels = null; // clear eyes_and_wheels from memory
-
eyes_and_wheels.close(); // close eyes_and_wheels connection
-
}
-
-
static void ifKit1_InputChange(object sender, InputChangeEventArgs e)
-
{
-
if (e.Index == 0)
-
-
if (e.Value == true)
-
{
-
ifKit1.outputs[0] = true;
-
-
eyes_and_wheels.servos[wheelLeft].Engaged = true; //wheelLeft
-
-
}
-
}
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?
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.
This whole block is your problem: - class Program
-
{
-
static AdvancedServo eyes_and_wheels; //Declare an servo object
-
static InterfaceKit ifKit1; //Declare an InterfaceKit object
-
-
int wheelLeft = 6;
-
-
ifKit1.InputChange += new InputChangeEventHandler(ifKit1_InputChange); //Hook the phidget spcific event handlers
-
-
eyes_and_wheels.open(99364); //opens eyes_and_wheels connection
-
eyes_and_wheels = null; // clear eyes_and_wheels from memory
-
eyes_and_wheels.close(); // close eyes_and_wheels connection
-
}
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: - class Program
-
{
-
private AdvancedServo eyes_and_wheels; //Declare an servo object
-
private InterfaceKit ifKit1; //Declare an InterfaceKit object
-
-
static void Main(string[] args)
-
{
-
//this line may not work; instantiate it however you're supposed to
-
ifKitl = new InterfaceKit();
-
-
int wheelLeft = 6;
-
-
ifKit1.InputChange += new InputChangeEventHandler(ifKit1_InputChange); //Hook the phidget spcific event handlers
-
-
eyes_and_wheels.open(99364); //opens eyes_and_wheels connection
-
eyes_and_wheels = null; // clear eyes_and_wheels from memory
-
eyes_and_wheels.close(); // close eyes_and_wheels connection
-
}
-
}
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.
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 - namespace Robot
-
{
-
class Program
-
{
-
static InterfaceKit ifKit1; //Declare an InterfaceKit object
-
static AdvancedServo eyes_and_wheels; //Declare an servo object
-
int wheelLeft = 6;
-
-
-
static void Main(string[] args)
-
{
-
try
-
{
-
ifKit1 = new InterfaceKit(); //Initialize the InterfaceKit1 object
-
eyes_and_wheels = new AdvancedServo(); //Initialize the AdvancedServo object
-
-
ifKit1.InputChange += new InputChangeEventHandler(ifKit1_InputChange); //Hook the phidget spcific event handlers
-
-
ifKit1.open(117172); //Open the object for device connections
-
eyes_and_wheels.open(99364);
-
-
Console.Read(); //Wait for user input so that we can wait and watch for some event data from the phidget
-
-
ifKit1 = null; //set the object to null to get it out of memory
-
ifKit1.close(); //User input was rad so we'll terminate the program, so close the object
-
-
eyes_and_wheels = null; // clear eyes_and_wheels from memory
-
eyes_and_wheels.close(); // close eyes_and_wheels connection
-
}
-
catch (PhidgetException ex)
-
{
-
Console.WriteLine(ex.Description);
-
}
-
}
-
-
static void ifKit1_InputChange(object sender, InputChangeEventArgs e)
-
{
-
-
if (ifKit1.SerialNumber == 117172)
-
{
-
if (e.Index == 0)
-
-
if (e.Value == true)
-
{
-
eyes_and_wheels.servos[wheelLeft].Engaged = true; //wheelLeft
-
}
-
}
-
}
-
}
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.
Sign in to post your reply or Sign up for a free account.
Similar topics
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...
|
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();
|
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...
|
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)...
|
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...
|
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...
|
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...
|
by: Pyenos |
last post by:
class One:
Two() #can't instantiate
class Two:
Three() #can't instantiate
class Three:pass
|
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,...
|
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...
|
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,...
|
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$) {
}
...
|
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...
|
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...
|
by: nemocccc |
last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
|
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...
|
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...
|
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,...
|
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...
| |