Thanks so much, your help is really appreciated. Its made realise how much I have to learn. Its the direction that I want to set and get to have adapted your examples to reflect this. However, its now asking for ; at the end of my static void Main.... even though I have never added this before, its asking to declare the body extern, abstract. Its also not letting me set the links whilst in Main, I am using the following syntax gh.setName("Great Hall"); gh.setN(dr); in order to set the north link between the great hall and the drawing room. Any idea?
private string RoomName;
private Room N = null;
private Room E = null;
private Room S = null;
private Room W = null;
public Room X { get; private set; }
public void setName(string N)
{
RoomName = N;
}
public void setN(Room X)
{
N = X;
}
public void setE(Room X)
{
E = X;
}
public void setW(Room X)
{
W = X;
}
public void setS(Room x)
{
S = X;
}
public Room getN()
{
return N;
}
public Room getE()
{
return E;
}
public Room getS()
{
return S;
}
public Room getW()
{
return W;
}
static void Main(string[] args)
Room gh = new Room();
Room kit = new Room();
Room Pan = new Room();
Room Dun = new Room();
Room dr = new Room();
Room lib = new Room();
Room din = new Room();
Room sr = new Room();
Room weap = new Room();
Room tow = new Room();
Room gal = new Room();
Room tr = new Room();
gh.setName("Great Hall");
gh.setN(dr);
You can handle properties explicitly like this:
private string _roomName;
public string roomName {
get {
return _roomName;
}
set {
_roomName = value;
}
}
In C# you can use simple way of dealing with properties:
public string RoomName {get; set;}
Will create a default property with GET and SET. It will also create a field for you, that you don't have to bother about that.
If you need to do something more you can use fields:
private string _roomName;
public string RoomName {
get {
//do something before returning
return _roomName;
}
set {
//do something while value is being set
_roomName = value;
}
}
If you are using new C# (6.0) you can use that syntax as well:
private string _roomName;
public string RoomName => _roomName;
When you are building property in code, that what's probably happens underneath:
Compiler translates property into set of two methods looks like that:
public string Get_RoomName() {
return _roomName;
}
public void Set_RoomName(string value) {
_roomName = value;
}
In the first place, according to your error:
SetN must declare a body as extern, abstract or partial and this comes up for my both get and set methods.
Please close the } at the end of your class Room!
and remove the ; after public void setN(Room X);!
In C# you can use properties to get the functionality of get and set methods:
this is a short Variant:
public string Name{ get; set; }
and this is the explicit version:
private Room myN;
public Room N
{
get { return myN; }
set { myN = value; }
}
In the extended Version you have a private field, which is inaccessible from outside, and you have a public property. It has a set { myVar = value; }
section which is the equivalent to your set-method.
EDIT: as suggested by#Jens: value is a default parameter that contains the value being passed from the outside to the set method. It is always called value and not expliictly declared in C#
Calling convention when setting would be to use it like a class variable:
Room AnyRoom = new Room();
Room X = new Room();
AnyRoom.N = X;
Related
I have a stucture of classes and subclasses as follows:
public class Regions
{
public const string UNITEDSTATES = "United States";
public static string[] members = { UNITEDSTATES};
public static class UnitedStatesTypes
{
public const string STEEL = "steel";
public const string CONCRETE = "concrete";
public static string[] members = { STEEL, CONCRETE };
public static class SteelStandards
{
public const string A36 = "ASTM A36";
public static string[] members = { A36 };
public static class A36Grades
{
public const string GRADE_36 = "Grade 36";
public static string[] members = { GRADE_36 };
}
public static class ConcreteStandards
{
...
}
There are more values under each one of the classes, but this is just a small sample so you can get the idea of what it looks like. I am trying to create a UI to select each one of these. There are 4 dropdown menus, each menu is populated by the value of the higher menu. So if the standards dropdown is on SteelStandards, the next dropdown is populated with A36, if it was on ConcreteStandards the next would be populated with the data under ConcreteStandards. Is there a way that I can access a subclass using a string variable?
For example, the first dropdown will select United States. The next dropdown needs to piece together "UnitedStatesTypes" and then access Regions.UnitedStatesTypes.members. I have tried using braces
Regions["UnitedStatesTypes"].members
but this did not work. Is there a way to make this happen? Or is there a better way to organize my data?
You could do this with just dictionaries, albeit it gets a bit unwieldy as you go down the tree:
var regions = new Dictionary<string,Dictionary<string,Dictionary<string,Dictionary<string,List<string>>>>>();
// populate it. Yes I know how ugly that will look!
var usSteelStandards = regions["United States"]["Steel"]["Standards"];
A better way might be to refactor your code as a set of class instances, instead of trying to use static classes/members all the way. It is a typical tree structure
public class Node : IEnumerable<Node>
{
public Node(string text)
{
this.Text = text;
this.Children = new List<Node>();
}
public string Text {get; private set;}
public List<Node> Children { get; private set;}
public Node this[string childText]
{
get{ return this.Children.FirstOrDefault(x => x.Text == childText); }
}
public void Add(string text, params Node[] childNodes)
{
var node = new Node(text);
node.Children.AddRange(childNodes);
this.Children.Add(node);
}
public IEnumerator<Node> GetEnumerator()
{
return Children.GetEnumerator();
}
IEnumerator IEnumerable.GetEnumerator()
{
return this.GetEnumerator();
}
}
This can then be setup and used much easier
var node = new Node("root")
{
{
"United States",
new Node("Steel")
{
{
"ASTM A36",
new Node("Grade 36")
}
},
new Node("Concrete")
{
}
}
};
Console.WriteLine(node["United States"].Children.Count);
Console.WriteLine(node["United States"]["Steel"]["ASTM A36"].Children[0].Text);
Live example: https://rextester.com/QVGN99585
Now my question is how can I make so when I make new object type Trip,
arrayOfPeople will be size of value numberOfRooms?
class Trip
{
private Person[] arrayOfPeople;
public Person[] arrayOfPeople get { return arrayOfPeople; }
set { arrayOfPeople = value; }
}
class Ship
{
private int numberOfRooms;
public int NumberOfRooms
{
get { return numberOfRooms; }
set { numberOfRooms = value; }
}
}
I was thinking of making numberOfRooms static and then in Trip constructor just setting arrayOfPeople = new Person[Ship.NumberOfRooms] but I am not sure if that is right aproach.
Feel free to correct me if I am wrong.
The comments in the code help to answer your question, so check it out :)
public class Trip
{
// Define a constructor for trip that takes a number of rooms
// Which will be provided by Ship.
public Trip(int numberOfRooms)
{
this.ArrayOfPeople = new Person[numberOfRooms];
}
// I removed the field arrayOfPeople becuase if you are just
// going to set and return the array without manipulating it
// you don't need a private field backing, just the property.
private Person[] ArrayOfPeople { get; set; }
}
public class Ship
{
// Define a constructor that takes a number of rooms for Ship
// so Ship knows it's room count.
public Ship(int numberOfRooms)
{
this.NumberOfRooms = numberOfRooms;
}
// I removed the numberOfRooms field for the same reason
// as above for arrayOfPeople
public int NumberOfRooms { get; set; }
}
public class MyShipProgram
{
public static void Main(string[] args)
{
// Create your ship with x number of rooms
var ship = new Ship(100);
// Now you can create your trip using Ship's number of rooms
var trip = new Trip(ship.NumberOfRooms);
}
}
Create a constructor for Trip that takes an integer parameter public Trip(int numberOfPeople) and inside that new up the array like you mentioned arrayOfPeople = new Person[numberOfPeople]()
There are semi answer to this question which I have read through thoroughly, as well as all things MSDN about generic classes but I am still having trouble when a generic class inherits from another class: where T: ClassName
For example, here is my generic list class
public class MyGenericList2<T> where T : Person
{
private T[] list;
public MyGenericList2(int size)
{
list = new T[size];
}
public T getItem(int index)
{
T temp = default(T);
temp = list[index];
return temp;
}
public void setItem(int index, T value)
{
list[index] = value;
}
public void DisplayList()
{
for (int i = 0; i < list.Length; i++)
{
Console.Out.WriteLine(list[i]);
}
}
}
It inherits from the person class:
NOTE: It is shortened for clarity sake
public abstract class Person
{
protected string firstName;
// Getters
public string getFirstName()
{
return this.firstName;
}
public void setFirstName(string fname)
{
this.firstName = fname;
}
}
When I try to call it I get an error about trying to convert a string to a {namespace}.Person which I sort of get, in that I am trying to put a string into a 'Person' box, but how does one call the class using this mechanism?
Here is the main method
public static void Main(string[] args)
{
MyGenericList2<Person> studentGeneric = new MyGenericList2<Person>(3);
Student st1 = new Student();
st1.setFirstName("Thor");
studentGeneric.setItem(0, st1); //This does not work
studentGeneric.setItem(1, Person.setFirstName("Odin"); // Does not work
studentGeneric.setItem(2, st1.setFirstName("Slepnir"); // Does not work
studentGeneric.DisplayList();
Console.ReadLine();
}
If I cut out the Where T : Person and use GenericList2<string> it works fine, which makes sense since it is string to string.
Any help would be appreciated
quick clarification Student inherits from Person:
public class Student : Person
{
// Student 1
private string studentID01 = "001";
public string getStudentID01()
{
return this.studentID01;
}
}
First of all I would recommend using public properties for your classes, for example:
public abstract class Person
{
public string FirstName { get; set; }
}
public class Student : Person
{
public string StudentId { get; set; }
}
This means your list code would work like this:
Student st1 = new Student();
st1.FirstName = "Thor";
studentGeneric.setItem(0, st1);
And you can even use this syntax:
studentGeneric.setItem(1, new Student
{
FirstName = "Odin"
});
Additionally, the .Net Framework already provides a really nice set of generic collection classes you can use so you don't really need your MyGenericList2<T> class. For example, the most commonly used class is System.Collections.Generic.List:
var people = new System.Collections.Generic.List<Person>();
people.Add(new Student
{
FirstName = "Odin"
});
Or even using the collection initialiser syntax:
var people = new System.Collections.Generic.List<Person>
{
new Student
{
FirstName = "Odin"
}
});
Finally, the problem you are having with outputting your values to the console is because C# doesn't know what to do with your class so by default outputs the value of student.ToString(). And becaue you haven't told your class what to do with it, it just outputs the name of the type. You can either override ToString or, much simpler just call the getFirstName() method:
Console.WriteLine(list[i].getFirstName());
You are using setItem incorrectly. This method can be used to set the value of elements in the list array in an instance of MyGenericList2 class.
To use the setFirstName method on an instance of the Student class, first use getItem to return the object instance. For example:
public void Main(string[] args)
{
MyGenericList2<Person> studentGeneric = new MyGenericList2<Person>(3);
Student st1 = new Student();
st1.setFirstName("Thor");
studentGeneric.setItem(0, st1);
Student st2 = new Student();
studentGeneric.setItem(1, st2);
studentGeneric.getItem(1).setFirstName("Odin");
Student st3 = new Student();
studentGeneric.setItem(2, st3);
studentGeneric.getItem(2).setFirstName("Slepnir");
studentGeneric.DisplayList();
Console.ReadLine();
}
To display the list contents correctly, replace your DisplayList() method with:
public void DisplayList()
{
for (int i = 0; i < list.Length; i++)
{
if(list[i] != null){
Console.Out.WriteLine("{0}: {1}", i, list[i].getFirstName());
}
else
{
Console.Out.WriteLine("{0}: [NULL]", i);
}
}
}
I have a class baseClass, and a list of objects of the baseClass. What i want to achieve is that i have to dynamically assign the instance number to each object in the list. for that what am doing is that use a constructor to do this.
Following is the class definition:
public class baseClass
{
private int _InstanceNumber;
private int _MyIntVal;
private string _MyString;
public string MyString
{
get { return _MyString; }
set { _MyString = value; }
}
public int MyIntVal
{
get { return _MyIntVal; }
set { _MyIntVal = value; }
}
public int MyProperty
{
get { return _InstanceNumber; }
}
public baseClass(int instance)
{
_InstanceNumber = instance;
}
}
The creation of the List of objects is as follows:
int instanceNumber = 0;
List<baseClass> classList = new List<baseClass>();
classList.Add(new baseClass(instanceNumber++) { MyString = "sample1", MyIntVal = 10 });
classList.Add(new baseClass(instanceNumber++) { MyString = "sample2", MyIntVal = 11 });
I know it is not the actual way for creating this. it does not give the index number actually. how can i calculate the instance number?
Consider the following scenario, that am creating another list of objects then it hard to maintain the instance number. or if i create another object(this also be an instance) external to the list.
int instanceNumber = 0;
List<baseClass> anotherClassList = new List<baseClass>();
classList.Add(new baseClass(instanceNumber++) { MyString = "sample1", MyIntVal = 10 });
classList.Add(new baseClass(instanceNumber++) { MyString = "sample2", MyIntVal = 11 });
Updates:
This is my temporary solution for this. i need proper way/ method to maintain instance number
If you want to find the index of item in the list, you should ask it from the list, not the item like:
var index = list.IndexOf(item);
But it seems that you expect the item to be aware of its position in the list. In order to do this, you should pass the list to the item so it can use it to find its own place in it:
public class Item
{
private List<Item> _containerList;
public Item(List<Item> containerList)
{
_containerList = containerList;
}
public int InstanceNumber
{
get { return _containerList.IndexOf(this); }
}
}
and change your code to:
List<Item> classList = new List<Item>();
classList.Add(new Item(classList ) { ... });
classList.Add(new Item(classList ) { ... });
I saw an example on MSDN where it would let you specify the default value if nothing is returned. See below:
List<int> months = new List<int> { };
int firstMonth2 = months.DefaultIfEmpty(1).First();
Is it possible to use this functionality with an object? Example:
class object
{
int id;
string name;
}
code:
List<myObjec> objs = new List<myObjec> {};
string defaultName = objs.DefaultIfEmpty(/*something to define object in here*/).name;
UPDATE:
I was thinking I could do something like this:
List<myObjec> objs = new List<myObjec> {};
string defaultName = objs.DefaultIfEmpty(new myObjec(-1,"test")).name;
But haven't been able to. It should be noted that I am actually trying to use this method on an object defined in my DBML using LINQ-To-SQL. Not sure if that makes a difference in this case or not.
You need to pass an instantiated class as a parameter of the DefaultIfEmpty.
class Program
{
static void Main(string[] args)
{
var lTest = new List<Test>();
var s = lTest.DefaultIfEmpty(new Test() { i = 1, name = "testing" }).First().name;
Console.WriteLine(s);
Console.ReadLine();
}
}
public class Test
{
public int i { get; set; }
public string name { get; set; }
}
To add to it and make it a bit more elegant (IMO) add a default constructor:
class Program
{
static void Main(string[] args)
{
var lTest = new List<Test>();
var s = lTest.DefaultIfEmpty(new Test()).First().name;
Console.WriteLine(s);
Console.ReadLine();
}
}
public class Test
{
public int i { get; set; }
public string name { get; set; }
public Test() { i = 2; name = "testing2"; }
}
As per the MSDN page on this Extension Method you can do what you want:
http://msdn.microsoft.com/en-us/library/bb355419.aspx
Check the sample on this page for an example on how to use this with an object.
i must admit i am not too sure i understand your question, but i'll try to suggest using double question mark if the returned object might be null. Like so:
myList.FirstOrDefault() ?? new myObject();
You can create a default Object Like this:
Object o_Obj_Default = new Object();
o_Obj_Default.id = 3;
o_Obj_Default.name = "C";
And add it to your default value :
string defaultName = objs.DefaultIfEmpty(o_Obj_Default).First().name;
If your list "objs" is empty, the result will be "C"