Netduino home hardware projects downloads community

Jump to content


The Netduino forums have been replaced by new forums at community.wildernesslabs.co. This site has been preserved for archival purposes only and the ability to make new accounts or posts has been turned off.
Photo

static bool


  • Please log in to reply
11 replies to this topic

#1 perkunas

perkunas

    Advanced Member

  • Members
  • PipPipPip
  • 108 posts

Posted 28 November 2012 - 03:55 PM

Trying to use static bool in my program, as I don't want it continually checking the input, but only at the beginning of the loop. Not working what am I doing wrong?.
public static void Method_7() // reads the nutrient controller realy contactcts if opend/closed when closed it does this
        {
            static bool nutrientState = false;
            nutrientState = phcontroller.Read();
            while (nutrientState == true)
            
            {
                a.Write(false);
                Thread.Sleep(5454);
                a.Write(true);
                b.Write(false);
                Thread.Sleep(3000);
                b.Write(true);
                Thread.Sleep(5 * MinuteMs);
            }
        }


#2 Mario Vernari

Mario Vernari

    Advanced Member

  • Members
  • PipPipPip
  • 1768 posts
  • LocationVenezia, Italia

Posted 28 November 2012 - 03:59 PM

Oh, the "boolean" type is a value type, and every time you access it, it returns the object value. What you mean is something like a delegate instead, but that still does NOT what you want. You really cannot create any "surrogate" that magically invokes the "read" function without explicitly calling it. Cheers
Biggest fault of Netduino? It runs by electricity.

#3 perkunas

perkunas

    Advanced Member

  • Members
  • PipPipPip
  • 108 posts

Posted 28 November 2012 - 04:27 PM

Ok so whats the answer, maybe a If Then thing IDK shooting in the dark here

#4 Geancarlo2

Geancarlo2

    Advanced Member

  • Members
  • PipPipPip
  • 70 posts

Posted 28 November 2012 - 04:56 PM

You can't create static members in the scope of a method. If the compiler/vs doesn't let you, you are probably wrong...

This does what you want:

public static void Method_7()
        {
            bool nutrientState = phcontroller.Read();
            while (nutrientState == true)            
            {
                a.Write(false);
                Thread.Sleep(5454);
                a.Write(true);
                b.Write(false);
                Thread.Sleep(3000);
                b.Write(true);
                Thread.Sleep(5 * MinuteMs);
            }
        }


phcontroller.Read() is a method call and returns a bool. nutrientState stores the value returned. When you compare it in "nutrientState == true", phcontroller.Read() is not called again, the value stored in nutrientState is used.

Note that the way it is coded, the loop will run forever until the thread is aborted.

#5 perkunas

perkunas

    Advanced Member

  • Members
  • PipPipPip
  • 108 posts

Posted 28 November 2012 - 06:05 PM

Thanks I owe you a beer!!! "Note that the way it is coded, the loop will run forever until the thread is aborted." Just noticed I didn't deal with that one either. abort.thread is a no no these days what do you suggest.

#6 Geancarlo2

Geancarlo2

    Advanced Member

  • Members
  • PipPipPip
  • 70 posts

Posted 28 November 2012 - 06:43 PM

No problem :P
in that case you can create the variable outside the scope of the method.

static bool nutrientState = false;//initial value
public static void Method_7()
        {
            nutrientState = phcontroller.Read();//read the port and assign result to nutrientState
            while (nutrientState == true)//run until nutrientState is set to false            
            {
                a.Write(false);
                Thread.Sleep(5454);
                a.Write(true);
                b.Write(false);
                Thread.Sleep(3000);
                b.Write(true);
                Thread.Sleep(5 * MinuteMs);
            }
        }

Here it is a static class member and you can set it to false somewhere else. Of course if you set nutrientState to false while it is performing the stuff inside the loop, it'll complete it first and then check nutrientState - if this is not the behaviour you want, you must take a moment and rethink how to implement your logic.

#7 perkunas

perkunas

    Advanced Member

  • Members
  • PipPipPip
  • 108 posts

Posted 28 November 2012 - 07:18 PM

In the end I used the static bool Idea thanks.
got it trying to meet two conditions
seems easier then trying to kill the thread.

bool phState = phcontroller.Read();
            while (phState == true && method_state_6 == true)


Many thanks you guys are the best

#8 perkunas

perkunas

    Advanced Member

  • Members
  • PipPipPip
  • 108 posts

Posted 29 November 2012 - 01:01 PM

Ok one last thing, is this right, the Idea is that it checks it at the beginning of every loop
Its not just going to check it just once is it?.


public static void Method_6() // reads the PH controller realy contactcts if opend/closed when closed it does this
        {
            bool phState = phcontroller.Read();
            while (phState == true && method_state_6 == true)
            {
                ph.Write(false);
                Thread.Sleep(1 * SecondMs);
                ph.Write(true);
                Thread.Sleep(5 * MinuteMs);

            }


#9 Geancarlo2

Geancarlo2

    Advanced Member

  • Members
  • PipPipPip
  • 70 posts

Posted 29 November 2012 - 01:45 PM

The way that is coded it checks only once. To get the behaviour you want(check on each iteration):

public static void Method_6()
        {
            while (phcontroller.Read() && method_state_6)
            {
                ph.Write(false);
                Thread.Sleep(1 * SecondMs);
                ph.Write(true);
                Thread.Sleep(5 * MinuteMs);

            }
}

or +verbose:
public static void Method_6()
        {
            while (phcontroller.Read()==true && method_state_6 == true)
            {
                ph.Write(false);
                Thread.Sleep(1 * SecondMs);
                ph.Write(true);
                Thread.Sleep(5 * MinuteMs);

            }
}

or ++(really unnecessary)verbose:
public static void Method_6()
        {
            bool phState = phcontroller.Read();
            while (phState == true && method_state_6 == true)
            {
                ph.Write(false);
                Thread.Sleep(1 * SecondMs);
                ph.Write(true);
                Thread.Sleep(5 * MinuteMs);
                phState = phcontroller.Read();
            }
}

To be honest, I think you should study a good c# book before moving forward with your project... your questions are language/logic related. Don't let the difficulties discourage you, gather more knowledge and when you succeed it'll be even more gratifying.

#10 Dave M.

Dave M.

    Advanced Member

  • Members
  • PipPipPip
  • 53 posts

Posted 06 December 2012 - 01:01 AM

or +verbose:

public static void Method_6()
        {
            while (phcontroller.Read()==true && method_state_6 == true)
            {
                ph.Write(false);
                Thread.Sleep(1 * SecondMs);
                ph.Write(true);
                Thread.Sleep(5 * MinuteMs);

            }
}

or ++(really unnecessary)verbose:
public static void Method_6()
        {
            bool phState = phcontroller.Read();
            while (phState == true && method_state_6 == true)
            {
                ph.Write(false);
                Thread.Sleep(1 * SecondMs);
                ph.Write(true);
                Thread.Sleep(5 * MinuteMs);
                phState = phcontroller.Read();
            }
}


These two methods are not equivalent. The last one only Reads once. Did you mean something like this instead, which is definitely way too verbose?

public static delegate bool PhStateDelegate();

public static bool CheckPhState()
{
    return phcontroller.Read();
}

public static void Method_6()
{
    PhStateDelegate phState = CheckPhState;

    while (phState() == true && method_state_6 == true)
            {
                ph.Write(false);
                Thread.Sleep(1 * SecondMs);
                ph.Write(true);
                Thread.Sleep(5 * MinuteMs);
                phState = phcontroller.Read();
            }
}


#11 Geancarlo2

Geancarlo2

    Advanced Member

  • Members
  • PipPipPip
  • 70 posts

Posted 06 December 2012 - 02:23 AM

These two methods are not equivalent. The last one only Reads once. Did you mean something like this instead, which is definitely way too verbose?


I think you missed the last line inside the loop since you tried to assign a bool to a delegate :P

#12 Dave M.

Dave M.

    Advanced Member

  • Members
  • PipPipPip
  • 53 posts

Posted 06 December 2012 - 04:59 AM

I think you missed the last line inside the loop since you tried to assign a bool to a delegate :P


haha!! You are correct, I *completely* missed that line. Guess I should have written this in the compiler before posting. :)




0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users

home    hardware    projects    downloads    community    where to buy    contact Copyright © 2016 Wilderness Labs Inc.  |  Legal   |   CC BY-SA
This webpage is licensed under a Creative Commons Attribution-ShareAlike License.