- Home /
List of Classes (js) - How to Construct Properly
Hi,
I've never worked with Lists before and I'm getting an error that states: "The type 'CEntry' does not have a visible constructor that matches the argument list '(int, int, boolean)'."
I'm sure this error message should mean something obvious to me, but unfortunately it doesn't. Is there anything noticeably wrong in this source code?
import System;
import System.Collections.Generic;
import System.Runtime.Serialization.Formatters.Binary;
import System.IO;
var numOnEach : int[]; // Received by the AgentSelectionDetails script
var uAvail : List.<CEntry> = new List.<CEntry>();
class CEntry{
var fNumber : int;
var uNumber : int;
var isAvailable : boolean = false;
}
function Start() {
for(var i=0;i<numOnEach.Length;i++) {
var fNumber : int = i;
var uNumber : int = numOnEach[i];
var isAvailable : boolean = false;
uAvail.Add(new CEntry(fNumber,uNumber,isAvailable)); // This line causes the error
}
}
Answer by poncho · Feb 04, 2014 at 02:15 AM
you are forgetting the constructor, and an overloaded constructor with your variables
class CEntry{
var fNumber : int;
var uNumber : int;
var isAvailable : boolean = false;
public CEntry(){}
public CEntry(int f, int u, bool avail){
fnumber= f;
uNumber=u;
isAvailable= avail;
}
}
thats the main idea, do not copy paste, understand it, then use it, and google Constructors, and Overloading methods
Good luck and happy coding
Thanks for the help, poncho.
So it looks like you first initialize it ("Contructor"?) and then Overload it by passing variables through the function. I think I'll need to do a bit more research to full understand this.
In any event, I've gotten it working now with your assistance.
Here's the code, in case someone stumbles on this thread later.
import System;
import System.Collections.Generic;
import System.Runtime.Serialization.Formatters.Binary;
import System.IO;
var numOnEach : int[]; // Received by the AgentSelectionDetails script
var uAvail : List.<CEntry> = new List.<CEntry>();
class CEntry {
var fNumber : int;
var uNumber : int;
var isAvailable : boolean = false;
function CEntry(){}
function CEntry(f:int,u:int,avail:boolean){
fNumber = f;
uNumber = u;
isAvailable = avail;
}
}
function Start() {
for(var i=0;i<numOnEach.Length;i++) {
var fNumber : int = i;
var uNumber : int = numOnEach[i];
var isAvailable : boolean = false;
uAvail.Add(new CEntry(fNumber,uNumber,isAvailable)); // This line causes the error
}
}
I do not know about js, but it was similar to c#, now i know that param types are, NameOfParam:TypeOfParam.., I helped you, you helped me, thank you cassius
Your answer
Follow this Question
Related Questions
How can i store the Amount of an item i have in an inventory? 0 Answers
Construct class with enum parameter (javascript) 0 Answers
Unknow identifier"List" 1 Answer
missingGameObject problem 3 Answers