- Home /
Returning new class from a method not working
Hi,
I am trying to run a method that returns a class with an int and a bool. Problem is that no matter what int and bool value the method returns it always is received as 0 and false.
Here is my method that runs the operation:
public void CheckSomething(something)
{
ReturnClass returnClass = otherScript.CheckForItem(something);
Debug.Log(returnClass.exists); //prints false to the console
Debug.Log(returnClass.index); //prints 0 to the console
if (returnClass.exists)
{
DoSomething(returnClass.index, something); //this should run, but does not because returnClass.exists == false
}
}
And here is the method that returns the class:
(I simplified it to always return the same values for testing purposes)
public ReturnClass CheckForItem(string item)
{
//checking if the item exists and the index of the item
return new ReturnClass(true, 3);
}
And here is the class I am returning:
public class ReturnClass
{
public int index;
public bool exists;
public ReturnClass(bool foundItem, int indexOfItem)
{
int index = indexOfItem;
bool exists = foundItem;
}
}
Am I doing something wrong?
Thanks in advance for any answers :)
$$anonymous$$ay just be a typo but why do you have public void CheckSomething(something)
and not
public void CheckSomething(string something)?
Also,
public ReturnClass CheckForItem(string item)//<- string isn't doing anything..
{
//checking if the item exists and the index of the item
return new ReturnClass(true, 3);
}
you should have something more like
public ReturnClass CheckForItem(int itemIndex)
{
//checking if the item exists and the index of the item
return new ReturnClass(true, itemIndex);
}
or
public Bool CheckForItem(string something,int itemIndex)
{
//checking if the item exists and the index of the item
if(Equals(ReturnClass.items[itemIndex],itemIndex)){
Debug.Log(something);
return Equals(ReturnClass.Items[itemIndex], itemIndex);
} else {
return Equals(ReturnClass.Items[itemIndex], 0);
}
}
Further more you could add each new int and bool to a list like so:
public class ReturnClass
{
public static List<int> indexs;
public static List<boo>l exists;
public ReturnClass(bool foundItem, int indexOfItem)
{
if(!Equals(indexs.Contains(indexOfItem),true){
indexs.Add(indexOfItem);
} else {
Debug.Log("List already contains the value.")
}
if(!Equals(indexs.Contains(indexOfItem),true){
exists.Add(foundItem);
} else {
Debug.Log("List already contains the value.")
}
}
}
now you can call the class directly for the variables:
public void CheckSomething(int index, string something)
{
if(Equals(ReturnClass.indexs,null)){
ReturnClass.indexs.Add(0);
}
bool checker= otherScript.CheckForItem(something, index);
//[Edit]Added
if(!Equals(checker,true)){
return;
}
for(int i=0;i<ReturnClass.indexs.Count;i++){
Debug.Log(ReturnClass.exists[i]); //prints false to the console
Debug.Log(ReturnClass.indexs[i]); //prints 0 to the console
if (returnClass.exists[i])
{
DoSomething(returnClass.index, something);
}
}
}
also forgot a way to create new ReturnClass's to add to those lists.. enough coding for me you figure it out [Edit] I'm also not using the bool check anywhere after setting it. just thought you should know -> Corrected
Answer by RobAnthem · Feb 16, 2019 at 03:38 PM
Notice how in this class you define "int index" and "bool exists" inside the constructor?
public class ReturnClass
{
public int index;
public bool exists;
public ReturnClass(bool foundItem, int indexOfItem)
{
int index = indexOfItem;
bool exists = foundItem;
}
}
By defining the type of variable or field, you actually allocate ram to a new local field of that type, IE, you're parameters are never actually being passed to the fields of the class. You need to use THIS keyword in front of the fields or reference the fields by default, like this.
public class ReturnClass
{
public int index;
public bool exists;
public ReturnClass(bool foundItem, int indexOfItem)
{
index = indexOfItem;
exists = foundItem;
}
}
Right. I've converted your comment into an answer since this is essentially the issue. Note that it would be highly recommended to use a struct for this kind of information ins$$anonymous$$d of a class. Though a common C# technique is to use an out parameter. So "CheckForItem" could look just like this:
public ReturnClass CheckForItem(string item, out int index)
{
index = -1;
//checking if the item exists and the index of the item
index = 3;
return true;
}
To use this the common pattern looks like this:
public void CheckSomething(something)
{
int index;
if (otherScript.CheckForItem(something, out index))
{
DoSomething(index, something);
}
}
Though when working with indices it's actually simpler to just return an invalid index to indicate that no item was found. Commonly "-1" is used since indices almost always start at 0. For example this is used by string.IndexOf.
@Bunny83 You always seem to have decent answers, in my post I'd rather ask your opinion, am i wrong? i know i'm missing something but, this isn;t my code and i don;t wanna work on it LOL.
This seems interesting, thanks for explaining :)
Your answer
![](https://koobas.hobune.stream/wayback/20220612191818im_/https://answers.unity.com/themes/thub/images/avi.jpg)
Follow this Question
Related Questions
Moving Coroutines To One Method 1 Answer
How do I display the final score? 1 Answer
How to return value from UnityWebRequest? 0 Answers
Return value from coroutine 2 Answers
member names cannot be the same as their enclosing type 2 Answers