So this is probably another newbie question, but i cant seem to solve it by my own.
I wanted to have a seperate class for the thread where i want to constantly update the integer Distance. The Problem is I get 2 errors:
The access for ObjectAvoidance.Program.distance_us3 is denied because of the securitylevel.
c# object reference is required for the non-static field method or property ObjectAvoidance.Program.distance_us3
I have this so far:
using System;
using System.Collections;
using System.Threading;
using Microsoft.SPOT;
using Microsoft.SPOT.Presentation;
using Microsoft.SPOT.Presentation.Controls;
using Microsoft.SPOT.Presentation.Media;
using Microsoft.SPOT.Presentation.Shapes;
using Microsoft.SPOT.Touch;
using Gadgeteer.Networking;
using GT = Gadgeteer;
using GTM = Gadgeteer.Modules;
using Gadgeteer.Modules.GHIElectronics;
namespace ObjectAvoidance
{
public partial class Program
{
void ProgramStarted()
{
Thread joystickThread = new Thread(ObjectAvoidance.DistanceReadThread);
joystickThread.Start();
}
}
public class ObjectAvoidance
{
public static void DistanceReadThread()
{
while (true)
{
int Distance = Program.distance_US3.GetDistanceInCentimeters();
}
}
}
}
@ Architect - tried that but then i get another error:
c# object reference is required for the non-static field method or property ObjectAvoidance.Program.ObjectAvoidance.DistanceReadThread
using System;
using System.Collections;
using System.Threading;
using Microsoft.SPOT;
using Microsoft.SPOT.Presentation;
using Microsoft.SPOT.Presentation.Controls;
using Microsoft.SPOT.Presentation.Media;
using Microsoft.SPOT.Presentation.Shapes;
using Microsoft.SPOT.Touch;
using Gadgeteer.Networking;
using GT = Gadgeteer;
using GTM = Gadgeteer.Modules;
using Gadgeteer.Modules.GHIElectronics;
namespace ObjectAvoidance
{
public partial class Program
{
void ProgramStarted()
{
Thread DistanceThread = new Thread(ObjectAvoidance.DistanceReadThread);
DistanceThread.Start();
}
}
public class ObjectAvoidance
{
public void DistanceReadThread()
{
while (true)
{
int Distance = Program.distance_US3.GetDistanceInCentimeters();
}
}
}
}
I meant the code that you have tried. Anyways, try this:
using System;
using System.Collections;
using System.Threading;
using Microsoft.SPOT;
using Microsoft.SPOT.Presentation;
using Microsoft.SPOT.Presentation.Controls;
using Microsoft.SPOT.Presentation.Media;
using Microsoft.SPOT.Presentation.Shapes;
using Microsoft.SPOT.Touch;
using Gadgeteer.Networking;
using GT = Gadgeteer;
using GTM = Gadgeteer.Modules;
using Gadgeteer.Modules.GHIElectronics;
namespace ObjectAvoidance
{
public partial class Program
{
void ProgramStarted()
{
Thread joystickThread = new Thread(DistanceReadThread);
joystickThread.Start();
}
public void DistanceReadThread()
{
while (true)
{
int Distance = distance_US3.GetDistanceInCentimeters();
}
}
}
}
@ Architect - Yeah im aware that i can do it this way, but i want to have an own class for the program so i can later add it to my menu and don’t have endless methods in one class.
Well ok my bad :S I kind of learned my way into c# through videos, but some things are not always explained very well, especially those static fields … or i’m just too stupid .
But thanks anyways.
Don’t get me wrong - it can be done with separate classes. You just have to make sure that things are passed around correctly. Understanding the difference between static vs non-static will help you a lot.