- Home /
declaring a list of structs c# ?
Here's my code:
using System;
using System.Collections;
using System.Collections.Generic;
public class AssemblerLibrary {
private static List<AssemblerDefinition> _library = new List<AssemlerDefinition>();
public static void Add (AssemblerDefinition def)
{
_library.Add (def);
}
public static Type GetAssembler(Component component)
{
AssemblerDefinition def = _library.First(a => a.componentType == component.GetType() && a.segmentType == component.owner.GetType() && a.variation == component.owner.variation.amount);
return def.assemblerType;
}
}
public struct AssemblerDefinition{
public Type componentType;
public Type segmentType;
public int variation;
public Type assemblerType;
// constructor
AssemblerDefinition(Type comp, Type seg, int vari, Type ass){
componentType = comp;
segmentType = seg;
variation = vari;
assemblerType = ass;
}
}
and here's the error I get:
Assets/scripts/AssemblerLibrary.cs(7,70): error CS0246: The type or namespace name `AssemlerDefinition' could not be found. Are you missing a using directive or an assembly reference?
I tried moving the definition of the "AssemlerDefinition" struct around. I put it in the assemblerLibrary definition. I put it in its own name space and including it. But I still get this error. Is there something wrong with my syntax?
Just as a note - AssemblerDefinitions appear long lived and in such cases would be much more performant if declared as a class rather than a struct, presu$$anonymous$$g you aren't deliberately exploiting the pass by value nature somewhere else.
Answer by vexe · Feb 12, 2014 at 08:41 AM
Typo "AssemlerDefinition" missing 'b'
so it is. thanks. I kept looking at the way it was written at the start of that line and not noticing the second one.
I wonder what editor are you using. Are you using just a text editor? or an IDE? my guess is a text editor. because any IDE would point you directly to the error. I suggest you use an IDE, either $$anonymous$$onoDeveloper or VisualStudio - A simple editor doesn't cut it :)
Answer by GenuiTix · Feb 12, 2014 at 09:26 AM
One thing is typo, the other thing is that the struct is public, but its constructor is not.
Answer by giano574 · Feb 12, 2014 at 09:30 AM
You constructor should be public.
Change
AssemblerDefinition(Type comp, Type seg, int vari, Type ass)
to
public AssemblerDefinition(Type comp, Type seg, int vari, Type ass)
Answer by getyour411 · Feb 12, 2014 at 08:42 AM
I can't comment on the code/purpose, but you have typo from the error missing "b" in assembler
Your answer
Follow this Question
Related Questions
A node in a childnode? 1 Answer
Optimization on list handling 2 Answers
Runtime IndexOutOfRangeException when testing on iPhone 1 Answer
Lists and Structs instead of Arrays? 4 Answers