I have a object class like so:
public class MyObject
{
[MaxLength(128)]
public string Name {get; set;}
}
However, when I make MyObject with a string for Name of more than 128 characters, I can set it and it works. This causes issues down the line because when I go to insert this object into the database, it exceptions due to the string being to long for that column in the table.
How would I go about making sure that a string that is too long gets truncated? And how can I detect when that happens so I can log it?
In the setter you can add some validation.
public class MyObject
{
private string name;
public string Name
{
get
{
return name;
}
set
{
if (string.IsNullOrEmpty(value) || value.Length <= 128)
{
name = value;
}
else
{
//log? do something or truncate
name = value.Substring(0, 127);
}
}
}
}
Alternatively I don't like it but I tried to make it work with an Attribute and made it easier to scale with a helper class.
public class MyObject
{
private string name;
[MaxLength(128, ErrorMessage = "String is longer than {1} characters and has been truncated.")]
public string Name
{
get { return name; }
set
{
name = value.Validate(GetType().GetProperty(MethodBase.GetCurrentMethod().Name.Substring(4)).GetCustomAttributes(false));
}
}
}
public static class Tools
{
public static string Validate(this string value, object[] attributes)
{
if (attributes.FirstOrDefault(x => x is MaxLengthAttribute) is MaxLengthAttribute maxLengthAttribute)
{
if (maxLengthAttribute.IsValid(value))
{
return value;
}
else
{
//LogMethod(maxLengthAttribute.FormatErrorMessage(maxLengthAttribute.MaximumLength.ToString()));
return value.Substring(0, maxLengthAttribute.Length - 1);
}
}
return value;
}
}
Related
I am trying to create a class that contains a header for my list-box. I have two classes that I will be inserting in the listbox. The first class runs fine, but the second one I am making so that the array is entirely of strings is telling me that a method must have a value return type. What does this mean exactly? The error at hand is "HeaderItems."
namespace RETAILITEMSBLAKE
{
class HeaderClass
{
string HeaderDescription;
string HeaderPrice;
string HeaderUnitsonHand;
public HeaderItems(string HeaderDescription, string HeaderUnitsonHand, string HeaderPrice)
{
this.HeaderDescription = HeaderDescription;
this.HeaderUnitsonHand = HeaderUnitsonHand;
this.HeaderPrice = HeaderPrice;
}
public string HeaderDescriptions
{
get
{
return HeaderDescription;
}
set
{
HeaderDescription = value;
}
}
public string HeaderUnits
{
get
{
return HeaderUnitsonHand;
}
set
{
HeaderUnitsonHand = value;
}
}
public string HeaderPrices
{
get
{
return HeaderPrice;
}
set
{
HeaderPrice = value;
}
}
}
Here is my first class that is working correctly:
namespace RETAILitemsBLAKE
{
class ItemizedClass
{
string description;
int unitsonhand;
double price;
public ItemizedClass(string description,int unitsonhand,double price)
{
this.description = description;
this.unitsonhand = unitsonhand;
this.price = price;
}
public string Description
{
get
{
return description;
}
set
{
description = value;
}
}
public double Price
{
get
{
return price;
}
set
{
price = value;
}
}
public int Quantity
{
get
{
return unitsonhand;
}
set
{
unitsonhand = value;
}
}
}
}
So, my goal is to have the HeaderClass so that I can place them as headers in my Listbox. Is there an alternate way to do such? I want to place it on top of the code here:
namespace RETAILitemsBLAKE
{
public partial class FrmItemList : Form
{
ItemizedClass[] items;
public FrmItemList()
{
InitializeComponent();
ItemizedArray();
}
private void ItemizedArray()
{
ItemizedClass jackets = new ItemizedClass("Jackets", 12, 59.95);
ItemizedClass jeans = new ItemizedClass("Jeans", 40, 34.95);
ItemizedClass shirts = new ItemizedClass("Shirts", 20, 24.95);
items = new ItemizedClass[] { jackets, jeans, shirts };
foreach (ItemizedClass RetailData in items)
{
lstRetailitems.Items.Add(RetailData.Description + "\t\t" + RetailData.Quantity + "\t" + "$" + RetailData.Price);
}
}
}
}
Would anyone be of assistance? Thank you!
You are using construct method which needs as same as class name and it didn't need to set return data type, so the method name needs to write HeaderClass in HeaderClass class otherwise it needs to set return data type to be a normal method.
class HeaderClass
{
string HeaderDescription;
string HeaderPrice;
string HeaderUnitsonHand;
public HeaderClass(string HeaderDescription, string HeaderUnitsonHand, string HeaderPrice)
I have a problem with a class that I wrote. When I try to call it I get an exception. See the code below for more clarity.
I have the class:
using System;
using System.Data;
namespace People
{
class Person
{
// Auto prop + field
public string Name
{
get { return this.Name; }
private set
{
if (string.IsNullOrEmpty(value))
{
throw new NoNullAllowedException("Name is mandatory");
}
else
{
this.Name = value;
}
}
}
// Auto prop + field
public int Age
{
get { return this.Age; }
private set
{
if (value <= 0 || value > 100)
{
throw new Exception("Age must be between 1 and 100");
}
else
{
this.Age = value;
}
}
}
// Auto prop + field
public string Email
{
get { return this.Email; }
private set { this.Email = value; }
}
// Constructor 1
public Person(string name, int age, string email)
{
this.Name = name;
this.Age = age;
this.Email = email;
}
// Constructor 2
public Person(string name, int age) : this(name,age,"")
{
}
// Write to console
public override string ToString()
{
return string.Format("Name: {0} \nAge: {1} \nEmail: {2}" ,Name,Age,Email);
}
}
}
And when I call this class:
Person someone = new Person("First Someone",51,"someone#gmail.com");
Console.WriteLine(someone);
I get:
Process is terminated due to StackOverflowException.
I can't see the problem.
Thank you in advance.
The problem is that when you try to get or set any of the properties, say, Name, there is a code path that calls the setter of the same property:
public string Name
{
get { return this.Name; } // <<<<====== HERE
private set
{
if (string.IsNullOrEmpty(value))
{
throw new NoNullAllowedException("Name is mandatory");
}
else
{
this.Name = value; // <<<<==== HERE
}
}
}
this.Name means "call getter/setter of Name with the value of value". This creates infinite recursion, causing stack overflow.
To implement properties with conditions like that you need to define fields that store the actual data. A common approach is to make these fields private, and name them with the same name as the property, except the first letter should not be capitalized:
private string name; // <<<<==== Add this
public string Name
{
get { return name; } // <<<<====== change
private set
{
if (string.IsNullOrEmpty(value))
{
throw new NoNullAllowedException("Name is mandatory");
}
else
{
name = value; // <<<<==== change
}
}
}
change
get { return this.Name; }
to
get;
the same goes for the Age property.
This is because this.Name is using the get method you are overriding, thus creating the glorious StackOverflowException! If you need a field for name and age you have to create one yourself like:
private string name;
public string Name
{
get { return this.name; }
private set
{
if (string.IsNullOrEmpty(value))
{
throw new NoNullAllowedException("Name is mandatory");
}
else
{
this.name = value;
}
}
}
Movie Class
public class Movie
{
#region Properties
public string Name { get { return _name; } set { _name = value; } }
public string Producer { get { return _producer; } set { _producer = value; } }
public int Rating { get { return _rating; } }
public Image Covor { get; set; }
public string Description { get { return _description; } }
public int ReleaseYear { get { return _releaseYear; } set { _releaseYear = value; }}
#endregion
#region Private Fields
private string _name;
private string _producer;
private int _rating;
private string _description;
private int _releaseYear;
#endregion
#region Constructors
public Movie()
{
}
public Movie(string name, int yearRelease)
{
this._name = name;
this._releaseYear = yearRelease;
}
public Movie(string name, int yearRelease, string producer)
{
this._name = name;
this._releaseYear = yearRelease;
this._producer = producer;
}
#endregion
}
My attempt
foreach (DataRow movieRow in MovieTable().AsEnumerable())
{
if (movieRow["Producer"] != DBNull.Value)
{
Movie movie = new Movie()
{
Name = (string)movieRow["Name"],
Producer = (string)movieRow["Producer"],
ReleaseYear = (int)movieRow["Release Year"]
};
movieList.Add(movie);
}
else
{
Movie movie = new Movie()
{
Name = (string)movieRow["Name"],
ReleaseYear = (int)movieRow["Release Year"]
};
movieList.Add(movie);
}
}
This is my code so far I'm trying to convert a Table to a List. The only problem is DBNull's.
I would like to update the entire table to a list, this works currently for 2 situations, but I need for the List to contain all the information if it exists. I could create elseif statements to handle every possible scenario but there has to be a way better way to figure out if the type is DBNull and if not set the property correctly.
If there's any confusion tell me what it is and I'll explain further.
One option is to incorporate the null-check into the set-statements wherever you need them, using shorthand code:
Name = (movieRow["Name"] == DBNull.Value) ?
(string)movieRow["Name"] :
string.Empty,
// Producer will be given a value if it exists, or null otherwise:
Producer = (movieRow["Producer"] == DBNull.Value) ?
(string) movieRow["Producer"] :
null,
...
You can replace string.Empty with null or vice versa of course, if that suits you better.
Edit: Just a very basic clarification, since you state you are new to programming: This shorthand notation means "if firstValue is true, return secondValue, otherwise, return thirdValue"
var result = firstValue ? secondValue : thirdValue;
One option: you can check IsNull(column):
Movie movie = new Movie()
{
Name = movieRow.IsNull("Name")
? (string)null : (string)movieRow["Name"],
Producer = movieRow.IsNull("Producer")
? (string)null : (string)movieRow["Producer"],
// etc..
};
movieList.Add(movie);
i was need to write 2 methods in my student class which do the following
hasPassed() Should return True if the student has a year mark >= 40 or
false if the marks is <40
toString() Should return a single string containing a summary of the
student details held within the class
e.g.
“12345 Basil Fawlty, 23/08/1946”
here's the code i have for the above to methods, is what i have correct for what its asking for the above?
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace CourseWork
{
public class Student
{
private static string firstname;
private string secondname;
private string dateofbirth;
private string course;
private int matricnumber;
private double yearmark;
public bool hasPassed()
{
if (yearmark >= 40)
return true;
else
return false;
}
public void toString()
{
firstname = "Basil";
secondname = "Fawlty";
dateofbirth = "23/08/1946";
course = "MA Hotel Management";
matricnumber = 12345;
yearmark = 55;
}
public Student()
{
}
public string FirstName
{
get { return firstname; }
set { firstname = value; }
}
public string SecondName
{
get { return secondname; }
set { secondname = value; }
}
public string DateOfBirth
{
get { return dateofbirth; }
set { dateofbirth = value; }
}
public string Course
{
get { return course; }
set { course = value; }
}
public int MatricNumber
{
get { return matricnumber; }
set
{
if (value <= 99999 && value >= 10000)
{
matricnumber = value;
}
else
{
Console.WriteLine("Invalid Matric Number: {0}", value);
}
matricnumber = value;
}
}
public double YearMark
{
set
{
if (value <= 100 && value >= 0)
{
yearmark = value;
}
else
{
Console.WriteLine("Invalid Year Mark: {0}", value);
}
yearmark = value;
}
}
}
i then need the above methods to be used in a get button that does the following
Get: Uses the values of the Student class methods to update the text boxes. The
Student.hasPassed() method should be used to update the pass/fail label. The
Student details summary should be updated by using Student.toString ().
but I'm having trouble coding it and i cant seam to call hasPassed() method or toString() method from my student class
so I've doing something wrong but cant see what it is
any ideas how to go about fixing this?
In order the methods to be visible, you need to create an instance of the class Student. ex,
Student _student = new Student();
bool _x = _student.hasPassed();
if you want the members to be access without instantiating, make the member static,
public static bool hasPassed()
{
if (yearmark >= 40)
return true;
else
return false;
}
but bear in mind that static members cannot see non-static members. In that case, it won;t compile because yearmark cannot be found.
Just found LinFu - looks very impressive, but I can't quite see how to do what I want to do - which is multiple inheritance by mixin (composition/delegation as I'd say in my VB5/6 days - when I had a tool to generate the tedious repetitive delegation code - it was whilst looking for a C# equivalent that I found LinFu).
FURTHER EDIT: TO clarify what I mean by composition/delegation and mixin.
public class Person : NEOtherBase, IName, IAge
{
public Person()
{
}
public Person(string name, int age)
{
Name = name;
Age = age;
}
//Name "Mixin" - you'd need this code in any object that wanted to
//use the NameObject to implement IName
private NameObject _nameObj = new NameObject();
public string Name
{
get { return _nameObj.Name; }
set { _nameObj.Name = value; }
}
//--------------------
//Age "Mixin" you'd need this code in any object that wanted to
//use the AgeObject to implement IAge
private AgeObject _ageObj = new AgeObject();
public int Age
{
get { return _ageObj.Age; }
set { _ageObj.Age = value; }
}
//------------------
}
public interface IName
{
string Name { get; set; }
}
public class NameObject : IName
{
public NameObject()
{}
public NameObject(string name)
{
_name = name;
}
private string _name;
public string Name { get { return _name; } set { _name = value; } }
}
public interface IAge
{
int Age { get; set; }
}
public class AgeObject : IAge
{
public AgeObject()
{}
public AgeObject(int age)
{
_age = age;
}
private int _age;
public int Age { get { return _age; } set { _age = value; } }
}
Imagine objects with many more properties, used in many more "subclasses" and you start to see the tedium. A code-gernation tool would actually be just fine...
So, LinFu....
The mixin example below is fine but I'd want to have an actual Person class (as above) - what's the LinFu-esque way of doing that? Or have I missed the whole point?
EDIT: I need to be able to do this with classes that are already subclassed.
DynamicObject dynamic = new DynamicObject();
IPerson person = null;
// This will return false
bool isPerson = dynamic.LooksLike<IPerson>();
// Implement IPerson
dynamic.MixWith(new HasAge(18));
dynamic.MixWith(new Nameable("Me"));
// Now that it’s implemented, this
// will be true
isPerson = dynamic.LooksLike<IPerson>();
if (isPerson)
person = dynamic.CreateDuck<IPerson>();
// This will return “Me”
string name = person.Name;
// This will return ‘18’
int age = person.Age;