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.
Related
Hey I have the following two classes:
public class YoloCocoP7Model : YoloModel
{
public override int Width { get; set; } = 640;
public override int Height { get; set; } = 640;
public YoloCocoP7Model()
{
}
}
public class YoloScorer<T> : IDisposable where T : YoloModel
{
public YoloScorer(string weights, SessionOptions opts = null) : this()
{
_inferenceSession = new InferenceSession(File.ReadAllBytes(weights), opts ?? new SessionOptions());
}
}
Now I can call a function like this:
public YoloScorer<YoloCocoP7Model> _scorer;
I want to change the Width and Height inside the YoloCocoP7Model class and Initialize it to the YoloScorer class and tried it the following way:
var test = new YoloCocoP7Model();
test.Width = 10;
test.Height = 10;
This works but however If I want to use that changed class then with:
var _scorer = new YoloScorer<test>;
I get an error saying "test" is a "variable" but is used like "Type".
How can I use the changed Class then?
The YoloScorer<T> in your class definition is a generic class, meaning it can work for different types.
You can now implement methods with that type like public T GetNewObject() or public string[] GetAllPropertyNames<T>() that use that Type T.
The type is, however, not the object itself, it's the type of object. In your case, the type is YoloCocoP7Model. The type has no instance.
If you want to give your class YoloScorer a object YoloCocoP7Model, you need to declare a member of that type and add it, i.e. via constructor:
public class YoloScorer : IDisposable
{
public YoloModel Model {get; set;};
public YoloScorer(YoloModel model) : this()
{
Model = model;
}
}
Then, you can modify it by calling
var _scorer = new YoloScorer<test>;
_scorer.Model.Width = 1337;
Adding <T> to a class makes it generic. That means it can work with values of different types. For instance, the List class you might have used before is generic. It can store elements of any types you provide it with.
Therefore, when you want to create a list of type int, for instance, you type new List<int>().
Now, the class you defined is also generic. That means, when you create an instance of it with new YoloScorer<...>(), you create a new YoloScorer that works with objects of type .... This is not exactly what you want, from my understanding.
Instead, you want to pass the specific YoloCocoP7Model to this class. To do that, just add a parameter corresponding to it to the constructor:
public YoloScorer(string weights, YoloCocoP7Model model, SessionOptions opts = null)
Now you can access model from the inside (and probably store it in some sort of private variable to make it accessible at later stages). From my understanding, your class does not need to be generic (<T> is not necessary).
I have just started learning OOP. I am making my own class which builds on an array. It has an attribute called length and an array that is of that length.
However, the actual value of length is only declared in the constructor, so my array is stuck as a private variable within the constructor.
How do I implement one such that the array has a certain user-chosen length and is able to be accessed by the class' methods?
public class myClass
{
private int length; //This is an attribute of my class
public myClass(int myLength)
{
length = myLength;
int[] myArray = new int[length];
}
}
I want myArray to be accessible but this is not possible because it is a local variable in the constructor. I think if it was in Python I could just make it a global variable. (Although I think that I would still like to keep this array private as it is also an attribute).
Thanks!
Note: This is not homework but rather something I've been challenging myself to do.
Here's how your class could look like, the OOP way:
public class MyClass
{
public readonly int Length;
public int[] Data { get; private set; }
public MyClass(int dataLength)
{
Length = dataLength;
Data = new int[dataLength];
}
}
Here:
The Data array is constructed with the user-specified length.
You can access both Length and Data from inside and outside the class
Once instantiated, the Length property cannot be modified
Another way would be to make Length return directly the Length property of the array, as long as it was instantiated:
public class MyClass
{
public int Length { get { return Data == null ? 0 : Data.Length; } }
public int[] Data { get; private set; }
public MyClass(int dataLength)
{
Data = new int[dataLength];
}
}
Revised answer as you have added more code to the question:
You have unwittingly solved your own problem. Take a look at your private int length; declaration. After the object the initialized with the constructor public myClass(int myLength), the length variable is still accessible within the object.
The sample code below has a new public int GetLengthPlusOne() method to access the length variable. Similarly, you can now use the myArray variable in another method.
MyOtherClass
public class MyOtherClass
{
public void SampleMethod()
{
MyClass cls = new MyClass(5);
Console.WriteLine(cls.GetLengthPlusOne()); //Output: 6
var arr = cls.myArray;
}
}
MyClass
public class MyClass
{
private int length; //This is an attribute of my class
/*
* Declaring this within the class instead of the constructor allows it
* to be persisted in an instance of the class. This is a property.
*/
public int[] myArray { get; set; }
public MyClass(int myLength)
{
length = myLength;
myArray = new int[length];
}
public int GetLengthPlusOne()
{
return length + 1;
}
}
Side note on naming conventions:
For C#, Class names start capitalized (MyClass), whilst instances of a class start with a lower-case (myClass). Have a look at the documentation for more info.
The following seems to be a class array?
Chemical.ChemicalName[IndexNumber]
It seems that there are several other fields associated with Chemical, such as Cost, Quantity, SupplierName (Chemical.Cost etc).
I was wondering what this type of variable is called? A class array? I've been searching online about arrays and can't seem to find any documentation on this.
And secondly, how do I declare such a variable?
Assuming it's a property, not an array , so you cannot access using an index,
public class Chemical
{
// Field
public string ChemicalName;
...etc
}
if chemical is an array , then you can declare like this,
Chemical[] Chemicals = new Chemical[200];
Then you can access the particular element using the index,
Chemicals[IndexNumber].ChemicalName
EDIT
If you want to have ChemicalName as a array inside the class,
public class Chemical{
public ChemicalName[] ChemicalNames = new ChemicalName[5];
]
you can access like this,
Chemical[] Chemicals = new Chemical[200];
c[index].ChemicalNames[index];
Variable would look something like that
public class Chemical{
public ChemicalName[] ChemicalNames = new ChemicalName[5];
...
}
So you can invoke it like that
Chemical c = new Chemical();
c.ChemicalNames[index];
OR, you can also declare the Array as static so you wont need an intance of the class to get the array e.g.
public class Chemical{
public static ChemicalName[] ChemicalNames = new ChemicalName[5];
...
}
to call a static, simply use class.variable/method name
Chemical.ChemicalNames[index];
It is a class property that implements an indexer. Usually this is an array, but it can be something else as long as it implements this[int index].
You can declare one by declaring it as a class property. For example,
class Book
{
public Book(int numPages)
{
Pages = new Page[numPages];
}
public Page[] Pages {get;}
}
You can then instantiate an instance and access a page.
Book myBook = new Book(100);
myBook.Pages[50]=new Page("Hi, welcome to Page 50");
Console.Write(myBook.Pages[50].GetText());
Let me consider this statement from the question Chemical.ChemicalName[IndexNumber], We can consider Chemical as a class or as an object of some other class. If it is a class means the ChemicalName will be a static.
Then comes the ChemicalName definitely it will be a collection(List/Array or something like that) or even an object of a class which having an indexer.
Case 1: consider Chemical is class and ChemicalName is a List of string So the Definition will be :
public class Chemical
{
public static List<string> ChemicalNames = new List<string>(){"name1","name 2"};
}
So that you can access a single name like the following:
string someChemicalName=Chemical.ChemicalNames[0]; // will be name1
Case 2: consider Chemical is an object of a class and ChemicalName is a List of string So the Definition will be :
public class Chemicals
{
public List<string> ChemicalNames;
}
Then you can access create the Chemical by using the following code:
Chemicals Chemical= new Chemicals();
Chemical.ChemicalNames=new List<string>(){"name1","name 2"};
Here also you can workout your statement like this
string someChemicalName=Chemical.ChemicalNames[0]; // will be name1
Let's parse Chemical.ChemicalName[IndexNumber]:
IndexNumber is probably some value of one of integer types - let guess int IndexNumber. Other options could be enum or any type as you can use indexer with any arguments.
[IndexNumber] is indexing something. Since there is no
Static Indexers? in C# it means ChemicalName can't be class name of static class like following
namespace Chemical {
static class ChemicalName{}
}
so it means that ChemicalName is either property or field of Chemical.
Now for Chemical there are more options
it could be static class with ChemicalName as static property:
static class Chemical{
public static string[] ChemicalName = new[] {"Food", "Poison"};
}
it could be local variable of some type that has ChemicalName as instance property:
class ChemicalType{
public string[] ChemicalName = new[] {"Food", "Poison"};
}
...
void MyMethod()
{
// implicitly typed, same as `ChemicalType Chemical`
var Chemical = new ChemicalType();
int IndexNumber = 1;
Console.WriteLine(Chemical.ChemicalName[IndexNumber]);
}
it could be field or property of your class (with any accessibility as to get Checmical.ChemicalName syntax to work for property it need to be used inside a method of your class)
class MyClass
{
// one of any combination:
// private field
ChemicalType Chemical = new ChemicalType();
// or protected automatic property
protected ChemicalType Chemical {get;set;}
// or public property
ChemicalType _chemical;
public ChemicalType Chemical {get {return _chemical;}}
...
}
Finally let's see what ChemicalName could be: the only requirement is to allow indexer by some type. including int. This gives very broad set of types as many of built in types support indexing.
array is most common one string[] ChemicalName
just string - somewhat strange given name of variable, but possible - string ChemicalName. When indexing will give single char result
List<string>
dictionary, this option allows broader range of indexing - i.e. by strings Dictionary<string,string> ChemicalName.
custom type implementing similar to public string this[int i] (or any other return type).
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.
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"});