Issue with passing an int field in as a parameter argument, but all others work perfectlyy how is this even possible? - unity3d

ScreenShot of CodeCould someone please explain to me how everything works in this script except a simple int counter that I pass in as a parameter? However if I directly pass in the int counter field into the method instead of using/ref. the para, it works just fine, how is this even possible? HELP!

By default parameters you give to the function, are evaluated and its value is passed (eg. not int xy is passed but the value of int xy, so 5).
So if you change the value directly eg. CounterAi -= 1; you are just changing the value you've passed on not the underlying variable. So if you want to use Pass by Reference in these cases you must use out or ref.
If you change a parameter of the passed value however it's value will be changed without needing to use ref or out.
Example:
public void Example1(int myValue) {
// This won't change the actual variable, just the value of the parameter,
// that has been passed
myValue -= 1;
}
public void Example2(ref int myValue) {
// This will change the actual variable,
// it's changing just the value of the parameter again,
// but we're using pass by reference
myValue -= 1;
}
public void Example3(Transform finishLine) {
// This will change the actual variable,
// because it's changing the data within the object,
// that the parameter value refers to.
finishLine.position = flSpts[Random.Range(0, flSpots.Count)].position;
}

Related

Multi if statement in class parameters setting

I know that in the latest version of dart we can use if else statements inside the build method. Does anyone know if we can use also if else statement when we setting class parameters? I know I can do inline statement there but inline is a bit hard to read when there are multiple conditions
const int i = 0;
class Person {
// NewClass n = NewClass(a: i == 0 ? 'a' : 'b'); //<- inline statement working
NewClass n = NewClass(a: if(i == 0) 'a' else 'b'); //<- if statement doesn't
}
class NewClass {
final String a;
const NewClass({this.a});
}
Edit:
Basically in my case I've got an TextField widget where I set its's type parameter from enum (Type.text, Type.numeric...) According to this parameter I want to set The textField parameters (textCapitalization, maxLength and so on)
As per your comment, you are already creating an enum for specifying the type of the fields.
enum Type {text, numeric}
Now for specifying the properties of that particular type, you can add an extension on this enum, as shown below:
extension TextFieldProperties on Type {
int get maxLength {
if (this == Type.text) {
return 10;
}
return 12;
}
}
So in your field class you already have a type defined, you can use that type variable to get the properties of that particular type of field.
Type type = Type.text;
print(type.maxLength); // Will print 10
type = Type.numeric;
print(type.maxLength); // Will print 12
Note: It will work only in Dart 2.7 and above
You want the conditional expression (?:), not the conditional statement or literal entry (if), as you have already discovered.
The reason if doesn't work is that if only works as a statement or as a collection literal entry. It doesn't work in arbitrary expressions.
The reason for the distinction is that the if syntax allows you to omit the else branch. That only makes sense in places where "nothing" is a valid alternative. For a statement, "doing nothing" is fine. For a collection, "adding nothing" is also fine.
In an expression context, you must evaluate to a value or throw. There is no reasonable default that we can use instead of "nothing", so an if is not allowed instead of an expression.
Doesn't work because this syntax doesn't exist in Dart. The only way to do what you would like to do is to use the ternary operator.
If you try it in the DartPad you will get an error.
I suggest you to use a function to return the right value.

Understanding constructors and final [duplicate]

class X extends Y {
X(int a, int b) : super(a,b);
}
Can someone give me an explanation about the syntax meaning of the colon :?
This feature in Dart is called "initializer list".
It allows you to initialize fields of your class, make assertions and call the super constructor.
This means that it is not the same as the constructor body. As I said, you can only initialize variables and only access static members. You cannot call any (non-static) methods.
The benefit is that you can also initialize final variables, which you cannot do in the constructor body. You also have access to all parameters that are passed to the constructor, which you do not have when initializing the parameters directly in the parentheses.
Additionally, you can use class fields on the left-hand of an assignment with the same name as a parameter on the right-hand side that refers to a parameter. Dart will automatically use the class field on the left-hand side.
Here is an example:
class X {
final int number;
X(number) : number = number ?? 0;
}
The code above assigns the parameter named number to the final field this.number if it is non-null and otherwise it assigns 0. This means that the left-hand number of the assignment actually refers to this.number. Now, you can even make an assertion that will never fail (and is redundant because of that, but I want to explain how everything works together):
class X {
final int number;
X(number): number = number ?? 0, assert(number != null);
}
Learn more.
It's ok to access non static member in initializer list.
class Point {
num x, y;
Point(this.x, this.y);
Point.origin(): this.x = 10, this.y = 10;
}
main() {
Point p = Point.origin();
print(p.x); // 10
}

How to assign method output to variable by reference MQL4

I'm guessing this question is going to apply to many similar languages other than MQL4 such as c++ (which I also forget how to use) which require you manually specify when you are passing by reference.
Method reference:
int[] previous = GetPrevious(i, ZigZagBuffer);
Method definition:
int GetPrevious[](int current, const double& buffer[])
{
int count = 0;
int result[];
// calculate count
ArrayResize(result,count);
// fill array
return result;
}
The resulting compile error is:
"Invalid Array Access"
From what I understand, this is because array's can only be passed by reference, but you have to explicitly state that you are passing it by reference. But the more I look up the syntax for passing by reference, the more I find articles about passing parameter by reference. (which I already know how to do as you can see in the code example.)
What is the syntax to assign the output of a method to a variable?
In case it matters, I only need to read the array multiple times; I'm not needing to alter or re-assign it after it is declared.
You cannot return array. You have to create it and pass into the function, fill inside the function and that's it.
OnTick(){
double array[]; //declaration of the array
fillArray(array,10); //passing array by ref, updating it there
Print(array[0]=0 && array[9]=9);//returns true
}
void fillArray(double &array[],int size){
ArrayResize(array,size);
for(int i=0;i<size;i++){array[i]=i;}
}

Incrementing primitive wrapper class passed as a parameter to a method which has no affect on invoker

I'm writing the Java SE 8 app on Eclipse IDE. The issue that I came across is following.
private Object[][] adjustIndexTaskValueAdded(int size){
Integer adjustingIndex = 0;
Object[][] tasksDisplay = new Object[size][taskValues[0].length];
for (int i = 0; i < size; i++) {
tasksDisplay[i][0] = taskValues[i][0];//phase colour
tasksDisplay[i][1] = identifyNextRowIDTaskTable(adjustingIndex, i);// the index
}
return tasksDisplay;
}
So, I've got adjustingIndex Integer wrapper class which I pass to the identifyNextRowIDTaskTable() method. So that the local var can store the value which gets modified at the child method.
private String identifyNextRowIDTaskTable(Integer adjustingIndex, int currentRowID){
if(UtilityOperations.isPhaseRow(phaseColourCurrent)){//it's a phase row
adjustingIndex++;
return "";
}
else{//it's a task row
int displayID = tableID - adjustingIndex;
adjustingIndex = 0;
return String.valueOf(displayID);
}
}
The above methods displays the method which modifies the Integer wrapper class which I pass to.
Now when I run the app, the new value is not reflected at the invoker method. It appears that value changes/adjusts at the child method, but the parent method does not see the changes. In the end, the outcome becomes erroneous.
The displayed source-code is simplified...
So, what the problem is?
I pass reference type var, and it is not a recursive operation.
I could use object's state to store the value instead, of-course. Yet, I want to understand the current pitfall.
Best regards
Consider
adjustingIndex++;
This is unboxing the value from the Integer to get an int and incrementing that value, this is equivalent to:
int tmp = adjustingIndex.intValue();
tmp++;
adjustingIndex = Integer.valueOf(tmp);
This resets the parameter adjustingIndex to be a reference to a new Integer, it does not change the value of the adjustingIndex variable in the calling method - that is a separate reference.
Again consider:
adjustingIndex = 0;
Again this resets the parameter adjustingIndex to be a reference to a new Integer, it does not change the value of the adjustingIndex variable in the calling method.
One alternative would be to use AtomicInteger
AtomicInteger adjustingIndex = new AtomicInteger(0);
increment with
adjustingIndex.incrementAndGet();
set back to zero with
adjustingIndex.set(0);
AtomicInteger has methods to change the value of the integer it contains, in contrast Integer is immutable and its value can't be changed.

Passing string parameters MVC 3

I'm trying to pass a string parameter in MVC3.
The URL that gets generated is:
http://localhost:50164/Property/Browse/Oregon
This appears to be the Property controller class in the function "Browse" with parameter "Oregon".
The right function gets called, but the string appears to be empty.
public ViewResult Browse(string location)
{
//counted_properties: 3 counted_properties_here: 0
ViewData["counted_properties"] = db.Properties.Count(); // debug 3 total
ViewData["counted_properties_here"] = db.Properties.Where(p => p.location == "Oregon").Count(); // debug 1 (the right answer!)
//return View(db.Properties.Where(p => p.location == tmp_location).ToList()); // 0 (bad!)
ViewData["the_location"] = location;
int size = tmp_location.Length; // unhandled null exception when tmp_location = location
ViewData["location_length"] = size;
// return View(db.Properties.Where(p => p.location == "Oregon").ToList()); // Right! but hardcoded
return View(db.Properties.Where(p => p.location == location).ToList());
}
When I return the lambda using the hard coded string (Oregon), I get the right answer.
When I return the lambda using the parameter (string location) I get zero results.
When I try to display ViewData["the_location"] which is a direct copy of the parameter it is blank. Maybe I'm not trying to display it correctly. Here is my view:
Location: #Html.Encode(ViewData["the_location"] )
When I call location.Length I get an unhandled null exception, but this works when I call Length on a hard coded parameter like "Oregon".
It looks like somehow the string is empty, but this doesn't make sense since I see "Oregon" in the URL when it gets to the property/browse function.
I'm beginning to wonder if I am allowed to pass strings as arguments in MVC as I haven't seen any examples with string parameters.
This is a continuation of an unsolved problem:
C#/ASP.NET lambda conversion
By the way, I'm not trying to promote Oregon here. I've never been there. Maybe it's a good place, though.
Check your routing. Traditionally URLs like the one you give will map the value ("Oregon") to the "id" parameter, rather than the location parameter that you're trying to use. Either rename the parameter or change the route to use "{controller}/{action}/{location}".