How to add new objects [closed] - c#

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 6 years ago.
Improve this question
Today i am trying to add another info into a class but i am unable to add. I have tough of adding another class but i am sure that there is a way to modify the class to add more Info.
The info i want to add is
Mr Chan, 1200, 18 Pioneer Rd and
Mr Lee, 600, Blk 21 #21-21 Yishun Rd
class PersonalInfo
{
private string name;
private float salary;
private string address;
public PersonalInfo(string nameVar, float salaryVar, string addressVar)
{
name = nameVar;
salary = salaryVar;
address = addressVar;
}
public void PrintPersonalInfo(TextBox txtPersonalInfo)
{
txtPersonalInfo.Text += name + Environment.NewLine + salary + Environment.NewLine + address + Environment.NewLine + Environment.NewLine;
}
}
That is my code for the class Personal info.
private void btnRun_Click(object sender, EventArgs e)
{
PersonalInfo obj = new PersonalInfo("Mr Tan", 3000, "Blk 123, #12-003 Kepple Rd");
obj.PrintPersonalInfo(txtPersonalinfo);
That is my code for the form.cs So far i can only think of adding new class to add more Info. Now i would like to know how to modify PersonalInfo.cs to add more Info.
Thanks for all you help and have a nice day ahead :)

Perhaps you could consider encapsulating your common properties into a new object, say BasicInfo:
public class BasicInfo
{
public string Name {get; set;}
public float Salary {get; set;}
public string Address {get; set;}
// add more properties when necessary
// e.g. Gender
// public string Gender {get; set;}
}
With that, in the constructor of PersonalInfo class, you can just pass in a BasicInfo object so you don't have to worry about all those properties in the PersonalInfo constructor.
public class PersonalInfo
{
BasicInfo basicInfo;
public PersonalInfo(BasicInfo basicInfo)
{
this.basicInfo = basicInfo;
}
}
You can then keep adding new properties into your BasicInfo class without needing to change PersonalInfo constructor. Hopefully it helps and that's what you are looking for.

Related

What does "_id" mean in this line of code? [closed]

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 1 year ago.
The community is reviewing whether to reopen this question as of 1 year ago.
Improve this question
So when i read a tutorial i met that piece of code, and i didn't understand why does " T id" and why author after then write in class "id = _id"?
class Account<T>
{
public T id { get; set; }
public int Sum { get; set; }
public Account(T _id)
{
id = _id;
}
}
He is just naming the variables differently for ease of reading / understanding. What he does is, declaring a property of type T (generic type) with name id and accepting a value for that in the constructor using the variable _id also of type T. and assigning it to the id proprty. this can also be writter as below but for clarity of reading the above approach may be good.
class Account<T>
{
public T id { get; set; }
public int Sum { get; set; }
public Account(T id)
{
this.id = id;
}
}
So the Constructor is theroeticly (practicly) a Method, it takes Parameters.
So you can new T(5).
The Class ITSELF has a Member which is "id", a variable which holds information AS LONG as the Object of this Class is not disposed.
Parameters of a Method are only Viable inside the Method, so _id is only Aviable in the Constructor.
If u want to Create a new Car with ID 5. The Object has to know that its ID 5.
So you Programmer, Create a Car with ID 5
T lambo = new T(5)
Now the Constructor Method has the _id = 5.
In the Constructor while constructing u Mark on the Car that its which it has been given
id = _id
now id = 5
and _id is gone after the constructor

C# - Add node to serialized class [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 4 years ago.
Improve this question
I'm very new to C# and please excuse me if i'm asking you stupid question.
I don't know how to add node to serialized class.
Here is my code:
namespace DuloGames.UI
{
[Serializable]
public class UISpellInfo
{
public int ID;
public string Name;
public Sprite Icon;
public string Description;
public float Range;
public float Cooldown;
public float CastTime;
public float PowerCost;
public float test;
[BitMask(typeof(UISpellInfo_Flags))]
public UISpellInfo_Flags Flags;
}
}
Here is how i try add new node to the serialized class above from another class:
using DuloGames.UI;
public class Character : MonoBehaviour
{
private void AddToNode()
{
UISpellInfo serializedNode = new UISpellInfo();
serializedNode.Add(new UISpellInfo()
{
ID = 1,
Name = "test",
Description = "description"
});
}
}
This is how i try to add new node to the serialized class but it seems i'm not doing it correctly. Can you please help me out ?
I'm not sure what your are trying to do but you UISpellInfo class is just a POCO that has no Add method right?
What are you trying to do.. I think you need to have a List of UISpellInfo object before you can add something to it?
List<UISpellInfo> nodes = new List<UISpellInfo>();
nodes.Add(new UISpellInfo{ID = 1, Name = "test", Description = "description"});
This should work i think.
But then again what will you do with serializedNode, i don't see you are doing anything with it?

Adding multiple class level attributes c# [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 7 years ago.
Improve this question
I want to add a property to the Node which is optional for the user to set.
How do I add another class level attribute aside from the one which already exists ?
VirtualName - this would give users the ability to name the node whatever they want.
I already implemented the ClassName. I will eventually want to add other class object level attributes like color and icon.
namespace NodeDemo
{
[NodeAttribute("DemoNode")]
public class DemoNode
{
[InputPinAttribute("Radius")]
public int Radius { get; set; }
[InputPinAttribute("Width Segments")]
public int WidthSegs { get; set; }
[InputPinAttribute("Height Segments")]
public int HeightSegs { get; set; }
}
}
Just add as many as you want:
namespace NodeDemo
{
public class ColorAttribute : Attribute
{
public string Color {get;set;}
public ColorAttribute(string color)
{
Color = color;
}
}
[NodeAttribute("DemoNode")]
[ColorAttribute("Red")]
public class DemoNode
{
...
}
}
You can latter check attribute using Type.Attributes property
string color = null;
Type myType = typeof(DemoNode);
ColorAttribute cAttribute = (ColorAttribute)Attribute.GetCustomAttribute(myType, typeof(ColorAttribute));
if (cAttribute != null)
{
color = cAttribute.Color;
}

array of pointers to objects in another class [closed]

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 8 years ago.
Improve this question
I have to create class like DataBase that will contain menu, adding users, editing them, deleting etc.
Users are from class User.
It looks like:
class base
{
protected int mAccounts=0;
protected const int mMaxAccs=10;
osoba[] list = new osoba[mMaxAccs];
public void menu(){...}
public void add()
{
user user1("Jurand","ze Spychowa",15231512,"1410-10-26","hue#x.com");
mAccounts++;
}
... useless code there
}
then there is User class
class user
{
private string mName;
private string mSurname;
private int mPesel;
private DateTime mBDate;
private string mEmail;
osoba(string mName2, string mSurname2, string mPesel2, string mBDate2, string mEmail2)
{
mName = mName2;
mSurname = mSurname2;
mPesel = Convert.ToInt32(mPesel2);
mBDate = Convert.ToDateTime(mBDate2);
mEmail = mEmail2;
}
The problem is adding new accounts. I totally don't know how to make it working
So the users will be stored in base class and you will have access to edit and add them etc.
Anyone know how to make it working (Creating objects properly)?
I suggest adding properties to User class:
class User
{
public string mName { get; private set; }
public string mSurname { get; private set; }
public int mPesel { get; private set; }
public DateTime mBDate { get; private set; }
public string mEmail { get; private set; }
//constructor for User class
public User(string mName2, string mSurname2, string mPesel2, string mBDate2, string mEmail2)
{
mName = mName2;
mSurname = mSurname2;
mPesel = Convert.ToInt32(mPesel2);
mBDate = Convert.ToDateTime(mBDate2);
mEmail = mEmail2;
}
}
and modify your add method (also changed object where you store data from array to List):
class MyDB
{
List<User> list = new List<User>();
public void add()
{
//use constructor to create new object
User person = new User("Jurand", "ze Spychowa","15231512","1410-10-26","hue#dupa.com");
//add object to list
list.Add(person);
}
}
Its not good idea to mix different languages in code so try avoiding naming objects/methods like "osoba".
You will probably benefit from reading following articles (in Polish):
C# Constructors
C# Properties
I belive that only thing that is missing in your add() method is:
osoba[mAccounts] = user1;
before mAccounts++ line.
Make fields public only if your User class is going to be stupid data object which only store data and contain no methods (maybe except formatting, converting etc.).

C# - Lists inside lists [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 9 years ago.
Improve this question
I need some help.
I'm trying to store information inside of lists.
For example, the user types in number, age, address and hair color...
And I want to make lists inside of lists to store the information, like this:
[0] Number
----[0] Age
----------[0] Address
----------[1] Adrress
--------------[0] Hair Color
If there is a better way to do this please help me!
With lists inside of list I will be very confused, but if it is the better solution, I've no problem.
Storing logically-grouped data as arrays of primitive values isn't really the way to go with this. Instead, replace the array with an object and just store the list of objects. In your example, the object could be as simple as:
class Person
{
public int Number { get; set; } // it's not clear what this value means
public int Age { get; set; }
public string Address { get; set; }
public string HairColor { get; set; }
}
You can add more properties, logic, validation, strongly-typed enumerations (for things like HairColor), etc. to this object. Maintaining a collection of this is easy:
var people = new List<Person>();
One of the key benefits you'd immediately notice with this approach is that the structure of the Person can change without having to change any of the collections which store Persons. You can add/alter/remove properties and it's still just a List<Person>. You'd only have to change code which uses the components that you're changing.
Another key benefit is that you can encapsulate all of the logic which defines a Person within this class, so other code doesn't need to worry about it. So instead of having to re-write the same validation logic for example (always making sure Age is a valid positive number, things like that), you can just write it on the object and it's applied everywhere. As Eric Raymond once said, "Smart data structures and dumb code work a lot better than the other way around."
You need a class structure take a look at this example:
public enum AddressType{
Work,
Home,
Other
}
class Address{
public AddressType AddressType{get;set;}
public AddressLine1 {get;set;}
public AddressLine2 {get;set;}
public string City{get;set;}
public string State{get;set;}
}
class User
{
public int Age{get;set;}
public string FirstName{get;set;}
public string LastName{get;set;}
public string HairColor{get;set;}
public List<Address> AddressList{get;set;}
public User(){
AddressList = new List<Address>();
}
}
class UserList:List<User>{
}
Then you can make use of this code like this:
var users = new UserList();
var usr = new User{FirstName = "Steve", LastName="Ruben", Age = 32, HairColor = "Brown"};
usr.AddressList.Add(new AddressList{AddressLine1 = "Address ln 1", AddressLine2 = "Address ln 2", City = "Some Place", State = "WI", AddressType = AddressType.Home});
users.Add(usr);

Categories