- Home /
Error passing class in method c#
I want to pass a variable with type class : "bpButtonID" in a method so the method will change the values of the variable. Main code:
public static void check(bpButtonID id, int newID)
{
id[] array = new id[8];
for (int i = 0; i < array.Length; ++i)
{
array[i] = new bpButtonID(5);
print(array[i].GetID());
array[i].SetID(newID);//Put the new id
print(array[i].GetID());
}
}
and then somewhere in the update i will call. this is just an example method so it doesnt really do something.
bpButtonID class:
private int id;
public ID(int id)
{
this.id = id;
}
public int GetID(){
return this.id;
}
public void SetID(int id){
this.id = id;
}
}
And i get an error (probably because of this line: "id[] array = new id[8];") because i dont know how to call it properly. The error:
The type or namespace name `id' could not be found. Are you missing a using directive or an assembly reference?
Answer by DoTA_KAMIKADzE · Apr 16, 2015 at 01:18 AM
So because you say that it is an example code I'll not dig into it then and just answer your question.
The error basically says that you can't use Instance of your class to instantiate array of your class. And yes you got it right. To create properly your new array of your Class you should use class not its instance, like that:
bpButtonID[] array = new bpButtonID[8];
P.S. So in the comment below you say you have initialized class already and pass it to your check method like that:
check(myInitializedArray, someInt);
But then what you want to do with your class? That isn't obvious from your code. Specify that.
For now I'll just try to guess once more - you just forgot to initialize your bpButtonID as array? Then just initialize it as an array and pass as an array and change the value of it, you don't need to create another array:
//make array:
bpButtonID[] mainArray;
//pass array:
check(mainArray, someInt);
//then do something with your array:
public static void check(bpButtonID[] id, int newID)
{
for (int i = 0; i < id.Length; ++i)
{
id[i] = new bpButtonID(5);
print(id[i].GetID());
id[i].SetID(newID);
print(id[i].GetID());
}
}
I'll make the second guess, you want to pass array and clone it, then:
public static void check(bpButtonID[] id, int newID)
{
bpButtonID[] array = new bpButtonID[id.Length];
Array.Copy(id, array, id.Length);
for (int i = 0; i < array.Length; ++i)
{
array[i] = new bpButtonID(5);
print(array[i].GetID());
array[i].SetID(newID);//Put the new id
print(array[i].GetID());
}
}
If in the end you'll find out that those^ are still not what you want then just state clearly what you want...
I know i have to use it like that, but i want to be able to make a variable at the start of the class like this: bpButtonID mainArray;
and then in the method use the variable in the method to change its values. What you wrote going to make an array every time and i dont want it i want one array so i can get and set values from it
Answer by ndrummer31 · Apr 16, 2015 at 05:21 AM
Hi rhbrr5hrfgdfgw! That error is usually caused by having a leak in a class. Check to make sure you have all your curly brackets.
Also, instead of using two separate functions to get and set the id, why not just use a property?
private int _id
public int ID
{
get { return this._id; }
set { this._id = value; }
}
You may already be aware of this and be doing that for a reason. In the case you aren't, I hope you learned something.
I hoped this helped! Happy coding!
Since i am using unity 4.2 or something like that i dont think i can use property. But thank you.
Answer by rhbrr5hrfgdfgw · Apr 16, 2015 at 11:03 AM
Well i kinda fixed it, so if anyone need to know:
Instead of calling the variable from the method parameters i did:
public bpButtonID[] bpArray;
And then i changed the length in the inspector to what i was needed, and then in the method:
bpArray[i] = new bpButtonID();
So now i can get the values from the arrays.
Your answer
Follow this Question
Related Questions
Set default length for an array of elements of a custom class in inspector 0 Answers
Script Wrapper Class 1 Answer
Why is my C# array of objects fully populated when some of them should be null? 3 Answers
How do i access the variables of a class that is in an array? 1 Answer
Need my function to work with different lists of different values (classes) 1 Answer