Adding multiple class level attributes c# [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 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;
}

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

How can I check for the data type of a dynamic property upon setting a value to it? [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 2 years ago.
Improve this question
Say I have this class:
public class Model {
public Type DataType { get; set; }
public dynamic DefaultValue { get; set; }
}
I would like to ensure that the data type of DefaultValue matches the assigned type to DataType as soon as a value gets assigned to the property. What would be a good way to do this?
Please use Generics for this:
public class Model<T>
{
public T DefaultValue { get; set; }
}
You could modify the DefaultValue to
private dynamic _defaultValue;
public dynamic DefaultValue
{
get => _defaultValue;
set
{
if (DataType == null) throw new Exception("Set type first");
if (value.GetType() != DataType) throw new Exception("Wrong type");
_defaultValue = value;
}
}
Although I imagine this could be better implemented with generics. Also what would happen if value is derived from DataType?

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?

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.).

How to create a new object using reflection in 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 9 years ago.
Improve this question
I would like to create a new object that is the instance of the following class.
How to make the object created by relfection equals the instance of the object represented by the class below with reflection c #?
public class cPerson
{
public String name { set; get; }
public String adress { set; get; }
public String phone { set; get; }
}
Maybe you're looking for something like Activator:
var person = Activator.CreateInstance(typeof(cPerson));
Of course, you'll probably be using it when the type is unknown at design time, to create object of the same type...
var newInstance = Activator.CreateInstance(p.GetType());

Categories