Friday, April 8, 2011

Attach variables

Hey everyone,

I want to take two variables (in and in2) and put them together, for example:

in = 1;
in2 = 3;

pin = in.in2; // I want this to set pin to 13

The arduino IDE tells me that in is not a class, so what syntax would I use to accomplish this?

EDIT: I figured out a different way to do it, you can just take in. multiply it by 10 and then set pin to the sum of in plus in2

From stackoverflow
  • try this, i wrote it in c but you get the gist. turn the two items into strings then concatenate and parse it as an integer.

    pin = int.Parse((string)in + (string)in2);

  • If your two variables are definitely integers then

    pin = (in*10)+in2;
    would work.

    If not, read them into strings (maybe with in.toString(), language dependent) and just do

    pin = int.parse(in.toString()+in2.toString());
    (Although, again dependent on language, you may have to do something other than int.parse [in C# you should use int.TryParse() for example])

0 comments:

Post a Comment