C# definition

I feel a little bit silly but when watching tutorial and now trying it myself, I cannot make this work properly. What do I need to change?

  double fRangeLength(double start, double end, double length){
    if (start > end) double jump = -length;
    else double jump = -length;
    return jump;
  }

example01.gh (4.2 KB)

should be

jump -= length

or do you want to assign the negative length?

even though you do it twice so the if statement doesnt make any sense.
also, you try to create the jump variable twice inside if block and return it outside the if block.

should look something like:

  double fRangeLength(double start, double end, double length)
{
   double jump = 0;

    if (start > end) 
       jump = -1 * length;
    else 
     jump -= length;

    return jump;
}

changing the if/else block to what you really want, be it invert or substract(even it doesnt make a difference here)

1 Like
 double fRangeLength(double start, double end, double length){
    // Function context
    if (start > end) {
        // if true context
        double jump = -length;
    } else { 
        // if false context
        double jump = -length;
    }
    return jump;
  }

If you declare a variable in a if-true-body, you just can use it in that context. In the same way, if you declare a variable in a function, you can not access it from a parent context.

1 Like

I have a curiosity question. Why is the public void with all functions and stuff below the private void?

So, by that, different from Python, C# first reads all the code and then starts working with it in the private void while in Python you have to place all functions before.
Is it correct that C# first reads all the code and then gonna work with it in the private void?

see here for a starter: https://www.geeksforgeeks.org/cil-or-msil-microsoft-intermediate-language-or-common-intermediate-language/

putting private or public methods/properties/fields first/second is a convention thing.

1 Like

private or public or protected are access modfiers. The are used to constrain the context of the calls. For example, if RunScript() is private, that means that you can not call Script_Instance.RunScript() from an upper context, just inside the class. Protected is similar, but the call can be done by a derived class.

void is a reserved word for methods that doesn’t return anything.

Well, first the precompiler read all the code to check that can be compiled without syntax erros. Do you mean that?

1 Like

oooohh, interesting, yes I meant that

So yes, before it start running the code, C# already knows what it can use. You don’t need to declare a method before you call it.

1 Like

About the braces. if I worked for one of you. Am I allowed to type as in example C?

Example A:

if (start > end) {
    // if true context
    double jump = -length;
} else { 
    // if false context
    double jump = -length;
}

Example B:

if (start > end){
  double jump = -1 * length;}
else{
  double jump -= length;}

Example C:

if (start > end){double jump = -1 * length;}
else{double jump -= length;}

When typing as in example C, you will not start looking like the character below because of the (misplaced) braces?

image

I am now thinking of placing everything in one line.

I dont know why you would do any of these things.

Refer to this post for some conventions in c#:

Its never wrong to stick to conventions people working on these things for decades have thought of(and yes, differing from them is also ok). No reason to make code harder to read.

1 Like

Yes, and you don’t need the braces if the if body is a single line. Even you can do a inline if statement:

double jump = (start > end)  ?  ifTrueDoSomething : ifFalseDoSomething;

But in that examples you need to declare jump a single time outside of the if corpus. You must do

jump = something;

rather than

double jump = something;

if you want to access jump outside the if context.

1 Like

I do have one more question. I want to check if ‘jump’ is made a value. But, I do not know how to check it.

Right now it says that jump does not exist.

example02.gh (5.2 KB)

You need to know how C# works with scopes (what I was referinb before as context). Variables are not globally accesible as in py.

because it only exists inside the if-block. refer to my first post and for further info search for lifetime/scope

https://www.ict.ru.ac.za/Resources/ThinkSharply/ThinkSharply/scope_and_lifetime.html

As others have pointed out, if you declare something within brackets, the definition is valid only inside that bracket (or “context”). So the following makes the “jump” variable isolated (isnide the if clauses brackets):

public double fRangeLength(...)
{
    if (true) 
    {
        // jump is "visible" only to code within these *nearest* brackets
        double jump = - 1 * length; 
    } 
    else
    {
        // Same here, the "jump" variable above is unkown here,  and declaring 
        // it anew won't make it visible outside of these brackets either:
        double jump =  -length; 
    } 
    // So, jump is unknown here! (again, its known existence is isolated
    // between the nearest brackets in which it was declared)
}

Therefore, make sure you declare “jump” between the brackets which encloses the entire function, then it will be known within en entire function, like so:

public double fRangeLength(...)
{
    double jump = 0.0; // <-- Here! Now "jump" is known/visible until the 
                       // function's corresponding closing bracket far below
    if (true) 
    {
        jump = - 1 * length;  // jump is "visible" here
    } 
    else
    {
        jump =  -length;  // same here, jump is "visible" also here
    } 
    // and it follows, "jump" is known/visible also here, even all the
    // way down to the  function's closing bracket!
    ...
    ...
    ...
    ...
}  // <- jump is visible all the way down to here

// Rolf

2 Likes
  public double fRangeLength(double start, double end, double length){
    double jump = 0.0;
    if (start > end){
      jump = -1 * length;
    }
    else{
      jump = -length;
    }
    
    List<Double> vals = new List<Double>();
    double temp = 0.0;
    if ((start - end) == 0){
      vals.Add(0);
    }
    else if (jump < 0){
      temp = start;
      while(temp > end){
        vals.Add(temp);
        temp += jump;
      }
    }
    else if (jump > 0){
      temp = start;
      while(temp < end){
        vals.Add(temp);
        temp += jump;
      }
      vals.Add(end);
    }
    return vals;
  }

I am sorry, but can you tell me what I did wrong here?

Error (CS0029): Cannot implicitly convert type ‘System.Collections.Generic.List’ to ‘double’ (line 94)

Just curious, @ForestOwl, but what tutorial are you following?

1 Like

To make it work with a list, modify the return type to a List of double, like so:

public List<double> fRangeLength(... )
{
    ...
    return vals;
}

BTW, be careful with the case. You have both Double and double. Be consistent in C#, otherwise you may be out for surprises…

// Rolf

Hi Pirateboy :smiley:

The Pokedex, I am not completely sure to use it, some girls are jigglypuffs, some are charmanders. Jikes, now I spotted a Jynx.

image

Do not know what to do with it.

image

What does Machamp do in this picture?

For my professional research I use my Pokémon logic. It is very refined.

But, to answer your question, I am now also following this playlist on YouTube from Stuttgart. :smiley:

1 Like

I see you have a cake icon behind your name

Happy Birthday :smiley: