I have the following code
public class IDFMeasure
{
,,,,,,,,,,
........
public float [][] GetIDFMeasure(string[] documents)
{
_docs = documents;
_numDocs = documents.Length;
MyInit();
return _termWeight;
}
}
I want to call this class in the main program. How I do it ? I'm getting confused with class types.. Can any help please? Many thanks
You don't call a class. You instantiate a class and call its methods. You can do so like this:
public void main() {
IDFMeasure measure = new IDFMeasure(); // instantiate class
float[][] vals = measure.GetIDFMeasure(documents); // now call method on that class
}
Create object of class and then call the method.
IDFMeasure obj = new IDFMeasure ();
float [][] arr = obj.GetIDFMeasure(new string[]{"1"});
If you only need to call the method and do not need object any more then you can create anonymous object and call method on it.
float [][] arr = new IDFMeasure().GetIDFMeasure(new string[]{"1"});
Related
I am writing in Unity and not all classes can be instantiated as they inheriting Monobehaviour. I have several classes and am trying to Return an array to one class, but keep getting a null reference exception.
I have made sure the array is indexed and initialized, yet it returns null.
However in a same construction without an array, eg an Int, it works.
public class StartUp : MonoBehaviour //Unity class
{
public int x = 30,y = 30;
public PlayField entireField;
void Awake()
{
entireField = new PlayField(x,y);
entireField.InitArray(entireField, x, y);
}
}
public class CharacterController : MonoBehaviour
{
//edit: playField is not supposed hold value, just to get
//access to a method in PlayField
PlayField playField;
FieldData fData;
void Start(){
playField = new PlayField();
fData = new FieldData();
}
void Update(){
fData = playField.GetFieldData(5,6); //We're just telling it go to that method
}
}
public class PlayField
{
public SingleField[,] singleField;
public PlayField()
{
}
public PlayField(int x, int y)
{
singleField = new SingleField[x,y];
}
void InitArray(PlayField playfield, int x, int y){
//In an effort to save the data I also did
this.singleField = new SingleField[x,y];
for(int j ...
{
for (int x...
{
playfield.singleField[x, y] = new SingleField();
playfield.singleField[x, y].fielData = new FieldData();
playfield.singleField[x, y].fielData.height = GetRandom();
//and here
this.singleField[x,y] = new SingleField();
this.singleField[x,y].fieldData = new FieldData();
}
}
//to ..
this.singleField = playfield.singleField;
}
public FieldData GetFieldData(int x, int y){
//Here it keeps returning null reference
return this.singleField[x,y].fieldData;
}
}
public class SingleField
{
public FieldData fieldData;
public GameObject fieldObject;
}
public class FieldData
{
public float height;
public Vector3 position = new Vector3();
}
I know I can use a static variable but I'd like to know what I am doing wrong here or how I can get the values from entireField in StartUp class to the FieldData fData in CharacterController using the none-MonoBehaviour class PlayField?
I thought the this.singleField-array would have values, but not during the Method call from CharacterController?
Erkan, I think you're going to need to take a step back and rethink how you're approaching this. Here's why I say that:
//edit: playField is not supposed hold value, just to get
//access to a method in PlayField
PlayField playField;
... that's not how things work. If you've got a 'main' PlayField that holds all your values, and initialize another one ... any time you call functions on that second one, it'll use the values from the second one. In your case, you're trying to call the GetFieldData function, which will grab field data from the second instance of PlayField - not the one you're going for.
So Where Do You Go From Here?
First, you might want to brush up a bit on Object Oriented Programming concepts. I'm not trying to be condescending - but it'll be hard to continue on with your program if you don't have a decent grasp on them.
Second, consider the use of 'static'. Static says, "I only have one instance of this throughout my program". Take the following for example:
class Playfield
{ /* code */ }
class SomeAlreadyExistingMainClassInYourCode
{
static Playfield globalFieldInstance = new Playfield();
/* rest of the class */
}
... at this point, you've got a single Playfield that's accessible throughout your program: SomeAlreadyExistingMainClassInYourCode.globalFieldInstance.
You shouldn't overuse/abuse this - but if you're only intending on having a single instance of an object being created, creating a static instance of it is a good way to do.
#m.rogalski Yes I was trying to avoid using a static too that's how this started.
Have solved the nullreference error. Really didn't think I had to initialize everything when I just wanted to use it only to call a method* in another class and use another instance of the same class for Return.
public class CharacterController : MonoBehaviour
{
//edit: playField is not supposed hold value, just to get
//access to a method in PlayField
PlayField playField;
FieldData fData;
void Start(){
playField = new PlayField();
InitArray(playField,30,30);
fData = new FieldData();
}
void InitArray(PlayField playfield int x, int y){
playfield.singleField = new SingleField[x,y];
for (int j =0; j< y;.. {
for (int i.. {
playfield.singleField[x,y] = new SingleField();
playfield.singleField[x,y].fieldData = new FieldData();
}
}
}
void Update(){
//The return was not supposed to be a field in this instance of
//PlayField, but come from the PlayField class itself as
//this.singleField[x,y] (a array in PlayField)
fData = playField.GetFieldData(5,6); //We're just telling it go to that method
}
}
public class PlayField
{
public SingleField[,] singleField = new SingleField[30,30];//evrything initailized and so on in the rest of the class here ommited. ect.
public FieldData GetFieldData(int x,int y){
//Here was the catch. Return values form this Class
// and not read from the instance created in CharacterController
//was giving me a nullrefence while I was using only FieldData
return this.singleField[x,y].fieldData;
}
}
This is solved for me.
On to the next question.
Thanks
I have a problem to determine where my failure is. I think it has something to do with my list in a list ... but I am not sure.
Class:
public class ChunkTerrainData
{
public int OriginX;
public int OriginZ;
public string ChunkMaterialData;
public int[,] ChunkHeightmap;
public string[,] ChunkInventory;
public List<GameObject> InventoryGameObjects;
}
Code Problem:
public static List<ChunkTerrainData> ListOfChunks = new List<ChunkTerrainData>();
//Start()
ChunkData = GenerateTerrain(ThisChunkOriginX, ThisChunkOriginZ, WithChunkData);
ListOfChunks.Add(ChunkData);
//Update()
GameObject Tree;
Tree = (GameObject)Instantiate(Tree_a, InstatiateTreeStone_Position, InstatiateTreeStone_Rotation);
Tree.transform.parent = TerrainMesh.transform;
ListOfChunks[i].InventoryGameObjects.Add(Tree); //some ListOfChunks
at This last line I get a Error (but the transform works well):
NullReferenceException: Object reference not set to an instance of an object
I could also not do (as I get the same error):
Debug.Log(ListOfChunks[i].InventoryGameObjects.Count);
what works (with the same List):
Debug.Log(ListOfChunks.Count);
how can I store and access this type of a list in a List? afaik the assignement of "Tree" is ok (the Cast from Object to GameObject). Can you tell me what to do? :)
Edit: maybe it has something to do with this List Initialisation - but I have no clue how to do it in C#
You never initialize your InventoryGameObjects field. Here is how it should look:
public static List<ChunkTerrainData> ListOfChunks = new List<ChunkTerrainData>();
//Start()
ChunkData = GenerateTerrain(ThisChunkOriginX, ThisChunkOriginZ, WithChunkData);
ChunkData.InventoryGameObjects = new List<GameObject>();
ListOfChunks.Add(ChunkData);
//Update()
GameObject Tree = (GameObject)Instantiate(Tree_a, InstatiateTreeStone_Position, InstatiateTreeStone_Rotation);
Tree.transform.parent = TerrainMesh.transform;
ListOfChunks[i].InventoryGameObjects.Add(Tree); //some ListOfChunks
Give that a try.
The Answer as supposed was:
//...
ChunkData = GenerateTerrain(ThisChunkOriginX, ThisChunkOriginZ, WithChunkData);
ListOfChunks.Add(ChunkData);
ListOfChunks[ListOfChunks.Count - 1].InventoryGameObjects = new List<GameObject>();
// ...
I have defined a class which has List<>. I have shortened my Code. It is too large. There are too many List<>& in Method1() there is lots of code. Here is my code :-
public class Time : ITime
{
public List<Table1> Setts1 = new List<Table1>();
public List<Tabl2> Setts2 = new List<Table2>();
public void LoadSettings1(int companyId)
{
Setts1 = ctx.tblSett1.Where(a => a.CompanyId == companyId).Select(a => a).ToList();
}
public double Method1()
{
var data = Setts1.Where(m => m.SetType == "TYPE1").Select(m => m.Value1).FirstOrDefault();
......
......
}
}
I want to use Method1() in another class. My issue is Setts1 which is preloaded in the Time Class. So when it is used in within the Time class it has Records. But when i call it from another class obviously Setts1 will have no records. I tried to initialize it from another class like this :-
public class Class
{
.....
Time cls = new Time();
cls.Setts1 = ....;
cls.Method1();
}
But Setts1 shows no records when in Method1. How to initialize the List<> from another class?
Exposing field members of a class, outside of the class is not a good practice. So I recommend using properties like this:
//Mark the field member as private
private List<Table1> _Setts1 = new List<Table1>();
//Use Property to access the field outside of the class
public List<Table1> Setts1
{
get
{
if (_Setts1==null || _Setts1.Count()==0) //or any other logic you need
{
//Initialize the field memeber
_Setts1 = ctx.tblSett1.Where(a => a.CompanyId == companyId).Select(a => a).ToList();
}
return _Setts1
}
}
This way you can forget about methods like LoadSettings1 and it doesn't matter whether you use the Setts property inside the class or outside, it will be initialized at the right time.
You have to call 'LoadSettings1(int companyId)'. This is the method which brings the records and populates your 'List'.
public class Class
{
.....
Time cls = new Time();
cls.LoadSettings1(1);
cls.Setts1 = ....;
cls.Method1();
}
public class Something
{
private Time cls = new Time();
public Something(int companyId)
{
cls.LoadSettings1(companyId);
}
public void CallMethod1()
{
cls.Method1();
}
}
Something like this? Using constructor for your "other class" to LoadSettings.
cls.Setts1 = ....;
Actually I don't see how your code would not work, even if as Hossein said, it's bad practice. Look into how you're setting cls.Setts1 (the .... part). That's most probably the culprit
So I have a array of class Piece called board.
There are some sub classes of piece such as Bishop and Knight and Rook ect...
The array looks like this:
Piece[,] board = new Piece[8,8];
board[0,0] = new Bishop(constructor stuff);
board[0,1] = new Rook(constructor stuff);
ect...
Each time I initialize a new instance of Bishop/Knight/Rook I want it to have it's own array so I can do
the following:
board[0,0].array[0] = ect...
How would I do that?
Add the array to the Piece class. All the subclasses will get access to it.
class Piece
{
public int[] array = new int[100]; // or whatever
// rest of class definition
}
Your Piece class would need to have a field or property for the array defined within it.
public abstract class Piece
{
public Something[] array = new Something[ARRAY_SIZE];
...
}
Do consider though whether this is actually a good design.
Add a variable to the classes called array which is an array?
Ex.
public class Bishop : Piece
{
// ...
public T[] array;
public Bishop()
: base()
{
// Initialize array
}
}
Where T is the type of the array.
You could make the class generic in case the array type might differ.
public class Bishop<T> : Piece
Then initialize it like:
board[0,0] = new Bishop<int>(); // array is int[]
I'd suggest you actually learning the basics of the language.
Say I have this C# class
public class MyClass {
int a;
int[] b = new int[6];
}
Now say I discover this class using reflection and while looking at the fields I find that one of them is of type Array (ie: b)
foreach( FieldInfo fieldinfo in classType.GetFields() )
{
if( fieldInfo.FieldType.IsArray )
{
int arraySize = ?;
...
}
}
I know it's not guaranteed that the array has a field initializer that creates the array but if it does I would like to know the size of the array created by the field initializer.
Is there a way to call the field initializer ?
If there was I would do something like this:
Array initValue = call field initializer() as Array;
int arraySize = initValue.Length;
The only was I found is to create an instance of the whole class but I would rather not do it like this as it's overkill...
Well, you can't.
The following code:
public class Test
{
public int[] test = new int[5];
public Test()
{
Console.Read();
}
}
will be compiled as:
public class Program
{
public int[] test;
public Program()
{
// Fields initializers are inserted at the beginning
// of the class constructor
this.test = new int[5];
// Calling base constructor
base.ctor();
// Executing derived class constructor instructions
Console.Read();
}
}
So, until you create an instance of the type, there is no way to know about the array size.
I dont think you have an option but to create an instance of the class as it doesnt exist until you do that.