Problems with Modules and Multiple Classes

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:

  1. The access for ObjectAvoidance.Program.distance_us3 is denied because of the securitylevel.
  2. 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();
            }
        }
    }
}

Just make thread function a non-static method of the Program class and use distance_US3 directly. It is a non-static member of the Program class.

@ 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

Show your code again, please.

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();
            }
        }
    }   
}

The name ObjectAvoidance is used as a namespace and class name. Try with different names.

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.

@ Mike - I tried it it doesn’t matter

Then you have to understand that you can’t access a non-static member of one class from a static method of any class

distance_US3 is an instance member (non-static) of the Program class

DistanceReadThread is a static method of the ObjectAvoidance 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 :wink: .
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.

I’ll try to get more in depth with those :wink: