public class myclass
{
public Details DetailsInfo { get; set; }
public string name{ get; set; }
public string Email { get; set; }
public int subjects{ get; set; }
}
public enum subjects{
maths,
english,
science,
}
Among these subjects is an enum. Even if I don't enter any value for subjects it takes 0 by default. This is the behavior of enum. Is there any way to check if I have chose any value for subject.
Note: I don't want to any value like undefined in the enum.
You could solve the issue in two ways.
First, define a None value (or whatever you want to name it) which represents 0 to indicate there is nothing chosen for an enum value:
public enum subjects{
None = 0, //define this
maths,
english,
science,
}
Alternatively, use Nullable<subjects> or subjects? in your class (myclass).
public class myclass
{
public Details DetailsInfo { get; set; }
public string name{ get; set; }
public string Email { get; set; }
public subjects? subj { get; set; } //note the question mark ?
}
Of the two methods, the first one is more common. Thus I would rather use the first one.
You can change the class to look like this:
public class myclass
{
public Details DetailsInfo { get; set; }
public string name{ get; set; }
public string Email { get; set; }
public subjects? subject { get; set; }
}
So that the subject is nullable. It means that when you have not set it. It will be null
Related
I've been trying to figure out how to do the following (although my research did not help): I have the these three classes:
public abstract class Classifier
{
public int ClassifierId { get; set; }
public string ClassifierName { get; set; }
public DateTime DateAdded { get; set; }
}
public class ManualClassifier : Classifier
{
public int ManualClassifierId { get; set; }
public string user_name { get; set; }
public string userName { get; set; }
public string firstName { get; set; }
public string lastName { get; set; }
public string email { get; set; }
public string password { get; set; }
}
public class ToolClassifier : Classifier
{
public int ToolId { get; set; }
public string ToolName { get; set; }
}
Both the ManualClassifier and ToolClassifer inherit from Classifier. I'm using EF Core to map this to a database but the question is the following: I've already searched a bit and I must make use of a descriminator which basically is an implicitly created column that will say the type of, in this case, classifier. So far so good. The issue arises when I have a property called ManualClassifierId as well as a ToolId. I want this two properties to map to the ClassifierId property. So in the table representing the entity Classifier, the ClassifierId property will either be the ManualClassifierId or the ToolId.
How can I achieve this mapping? Also, this solution would mean that both child classes would both have empty fileds in the tables (due to inheriting the three properties from the Classifier class). Is there a better solution? Perhaps just erase the Id's from both child classes a let them inherit the parent one?
Thank you in advance!
To use the same column name in both classes, you can add a Column attribute to both properties. Then they will both use that column name in the database. See ColumnAttribute(String).
Use it like this:
public class ManualClassifier : Classifier
{
[Column(Name="ClassifierId")]
public int ManualClassifierId { get; set; }
...........
}
Do the same with ToolId.
I'd like to construct a person class that has a property which the value may change over time but I want to keep the history of values. A person grows taller over time and their weight may change. You may imagine a "HockeyPlayer: Person" that plays over many seasons. Each season their height/weight may be different. I would then use EntityFramework to create tables. I think the main question I have is where do I put those properties? In their own class? In HockeyPlayer? In Person? Thank you in advance
pseudocode example:
public class Person
{
public int Id { get; set; }
public string FirstName { get; set; }
public string LastName { get; set; }
public System.DateTime DateOfBirth { get; set; }
}
public class Season
{
public int Id { get; set; }
public string season { get; set; }
}
public class HockeyPlayer : Person
{
public int PersonId {get; set; }
public int SeasonId { get; set; }
public int JerseyNumber { get; set; }
public int HeightInches { get; set; }
public int WeightPounds { get; set; }
}
I'm not sure your domain model here really models what you are looking for. Don't think of your player playing in a season as a player. A player participates in a season and gets a record for season specific information. So a player has a history of seasons played. a player is NOT a history of seasons.
I think I'd define something like this
public class Player
{
public Player() { Seasons = new List<PlayerSeason> { }; }
public int Id { get; set; }
public string FirstName { get; set; }
public string LastName { get; set; }
public System.DateTime DateOfBirth { get; set; }
public IEnumerable<PlayerSeason> Seasons { get; private set; }
public void AddSeason(PlayerSeason playerSeason)
{
//some code that adds a season. may check if already exists. whatever your business rules say to do
Seasons.Add(playerSeason);
}
}
public class Season
{
public int Id { get; set; }
public string season { get; set; }
}
public class PlayerSeason
{
public int PlayerId {get; set; }
public int SeasonId { get; set; }
public int JerseyNumber { get; set; }
public int HeightInches { get; set; }
public int WeightPounds { get; set; }
}
I'm not going to get into fancier stuff like collection encapsulation, there's enough here already to think about.
I'd take the entity classes you define here and then build your using ef-migrations.
I would recommend doing some research into database normalization.
I would create another entity called PersonMetrics with SeasonId, PersonId and Date attributes. You would then add whatever values you want to measure in that. Technically SeasonId and PersonId could form the composite primary key (in which case you would be better off putting Date on the Season entity), but for ease of use with EntityFramework, or if there will be multiple measurements per season you may choose to give it its own Id.
One way would be to create a separate table with height, weight and date columns with player Id as a secondary key. Call this table PhysicalStats for example. Your HockeyPlayer would have a list of PhysicalStats rather than just single height/ weight properties as they do currently.
I have a sqlite database which has some tables and columns like the following:
int Id
text Name
text Comment
...
And my object in my project looks like this:
Public Class Entry {
public int Id { get; set; }
public String Name { get; set; }
public String Comment { get; set; }
public String Additional { get; set; }
}
This can happen, because my programm need to handle different versions of the database.
EF Core now trys to access the Additional field of the database but returns an error that it cannot find the field. (Expected behaviour)
Now my question is, if there is a way to ignore this error and return a default value for the property?
I could bypass the error by making the properties nullable. But i don't want to check each property with .HasValue() before accessing it. Because the real database has 50+ columns in the table.
https://www.entityframeworktutorial.net/code-first/notmapped-dataannotations-attribute-in-code-first.aspx
Put NotMapped as an attribute on the Additional field:
using System.ComponentModel.DataAnnotations.Schema;
Public Class Entry {
public int Id { get; set; }
public String Name { get; set; }
public String Comment { get; set; }
[NotMapped]
public String Additional { get; set; }
}
This tells EF that the field is not a column in the database.
I would advise you to split your domain object from that persisted dto object. That way you can have different dtos with different mappings. Now you can instantiate your domain object with your dto and decide inside your domain object what values are the correct default values.
public class Entry
{
public int Id { get; set; }
public string Name { get; set; }
public string Comment { get; set; }
public string Additional { get; set; }
}
public class EntryDtoV1
{
public int Id { get; set; }
public string Name { get; set; }
public string Comment { get; set; }
}
public class EntryDtoV2
{
public int Id { get; set; }
public string Name { get; set; }
public string Comment { get; set; }
public string Additional { get; set; }
}
Now you only need to create some kind of factory that creates the correct repository depending on what database version you query.
I'm unsure how to go about implementing this particular idea of I have a class lets call it EnhancedUserInput that will have some variables that all of the input types will have and a particular subclass depending on the need during operation so some extra variables and a list so for example sub classes of it would be MultipleChoice which would have MinSelection, MaxSelection and a list of a type called option with their own variables ect and then another possible sub class called ExplicitAgreement which would have the variables inputLabel1, inputLabel2 and a list of type BinaryInput which would have their own variables.
So far from what I understand the best way going about this would be to have some type of generic variable? I'll show some code to try and help get what it is I need across but was just wondering is there an easy way of doing this that I am unaware of?
public class EnhancedCustomerInput
{
public string Title { get; set;}
public bool ResponseOptional { get; set;}
public string CancelLabel { get; set;}
public string SubmitLabel { get; set}
// this is where I am unsure of how to go about it
public object inputType
{
MultipleChoice
ExplicitAgreement
}
}
public class MultipleChoice
{
public List<MultipleChoiceOption> Options { get; set; }
public int MinSelected { get; set; }
public int MaxSelected { get; set; }
}
public class ExplicitAgreement
{
public List<BinaryInputOption> Buttons { get; set; }
public string InputLabel1 { get; set; }
public string InputLabel2 { get; set; }
}
what would be the best path for this solution I can think of some possible ways but they would be a bit figity and was wondering if there are any simple ways?
Seems to me that you may have this the wrong way around. Maybe what you want is to just use class inheritance?
public class EnhancedCustomerInput
{
public string Title { get; set;}
public bool ResponseOptional { get; set;}
public string CancelLabel { get; set;}
public string SubmitLabel { get; set}
}
public class MultipleChoice : EnhancedCustomerInput
{
public List<MultipleChoiceOption> Options { get; set; }
public int MinSelected { get; set; }
public int MaxSelected { get; set; }
}
public class ExplicitAgreement : EnhancedCustomerInput
{
public List<BinaryInputOption> Buttons { get; set; }
public string InputLabel1 { get; set; }
public string InputLabel2 { get; set; }
}
Steve Harris's inheritance suggestion is good. Your original option to use Composition can work just fine, too:
public class EnhancedCustomerInput
{
public string Title { get; set;}
public bool ResponseOptional { get; set;}
public string CancelLabel { get; set;}
public string SubmitLabel { get; set; }
public object InputData { get; set; }
}
The only problem is that consumers of your code need to know that InputData can be one of several different types, and you presumably need logic to switch on their type. You can add comments to the property to give people a hint, or you can use a library like LanguageExt, which provides an Either type:
public class EnhancedCustomerInput
{
public string Title { get; set;}
public bool ResponseOptional { get; set;}
public string CancelLabel { get; set;}
public string SubmitLabel { get; set; }
public Either<MultipleChoice, ExplicitAgreement> InputData { get; set; }
}
This makes it much more obvious which types InputData can be, but would get very unwieldy if you have more than two possibilities.
You could also declare an interface that InputData must implement, which would make it easier for developers to find all the types that are intended to be used there. But an empty interface is considered to be a code smell because it indicates you're using interfaces for something they weren't really intended for.
Another option I've found to work well is to define an enum type to help identify which different types of input data you can have:
public class EnhancedCustomerInput
{
public string Title { get; set;}
public bool ResponseOptional { get; set;}
public string CancelLabel { get; set;}
public string SubmitLabel { get; set; }
public InputType InputType { get; set; }
public object InputData { get; set; }
}
public enum InputType { MultipleChoice, ExplicitAgreement }
This gives your business logic a specific set of possible types that you can switch your logic on, and works particularly well when the class is going to be serialized and deserialized, because then you can tell the deserializer which specific type of object to deserialize InputData to.
There are lots of options, each with their advantages and disadvantages.
I would like to access the email of this class into another class that I am already instantiated on my render page.
Any help very much appreciated.
public class Access
{
public string Email { get; set; } //I want this value...
}
public class Types
{
public string Id { get; set; }
public string Sum { get; set; }
public string Addition { get; set; }
public string Email { get; set; } // ...to be this value inside my class TYPES
}
Well I think you can simply use a copy constructor for the Types class with the Access instance as parameter from which you want to copy the email value when creating a new Types instance.
From what you said it seems you don't care if its a particular instance (as your post is a bit confusing between class and instance)
public class Types
{
public string Id { get; set; }
public string Sum { get; set; }
public string Addition { get; set; }
public string Email { get; set; }
public Types(Access access)
{
Email = access.Email
}
}