I'm looking at some code and found this kind of pattern:
private string text { get; set; }
public string Text
{
get
{
return text;
}
set
{
text= value;
RaisePropertyChanged("Text");
}
}
I normally just back my public properties with private fields.
Is there any reason a property should be backed by a private property like this? My instinct is to say that it shouldn't, and this should be backed by a field instead, is that right? Any technical reasons I can use to back this up?
Typical case is when you have a raw data (data as it is without any transformation) and the same data, but friendly represented:
private String m_RawText;
// Text as it's obtained from, say, database
private string rawText {
get {
if (null == m_RawText)
m_RawText = ReadValueFromDataBase();
return m_RawText;
}
set {
if (m_RawText != value) {
UpdateValueInDataBase(value);
m_RawText = value;
}
}
}
// Friendly encoded text, for say UI
public string Text {
get {
return EncondeText(rawTex);
}
set {
rawText = DecodeText(value);
RaisePropertyChanged("Text");
}
}
// Here we want rawText
public void PerformSomething() {
String text = rawText; // we want raw text...
...
}
// And here we prefer Text
public override String ToString() {
return String.Fromat("Text = {0} ", Text, ...)
}
Related
i am working with a .net application where i have a web service that returns values in array form and now this array values i want to pass to a class and also as a reference to a private object. But since i am fresh new in programming i do not know how where an with what logic to start.
This is the private obj i created and i want to pass those references where CT is the array type and clsIn is the info that comes from another class but i have no idea how to pass neither of them.
private object TotInfo(clsIn In, CT ct)
{
TotInfo objFromCD = new TotInfo();
return objFromCD;
}
And here is the new class i have created that where i want to pass all the values from clsIn and CT:
public class TotInfo
{
// Object properties
private string LAST_OFFER;
private string LAST_OFFER_DATE;
private string CLOSING_REASON;
private string _NO;
private string _STATUS;
#region "GET/SET Property"
public string NO
{
get { return _NO; }
set { _NO = value; }
}
public string LAST_OFFER
{
get { return _LAST_OFFER; }
set { _LAST_OFFER = value; }
}
public string LAST_OFFER_DATE
{
get { return _LAST_OFFER_DATE; }
set { _LAST_OFFER_DATE = value; }
}
public string CLOSING_REASON
{
get { return _CLOSING_REASON; }
set { _CLOSING_REASON = value; }
}
public string STATUS
{
get { return _STATUS; }
set { _STATUS = value; }
}
#endregion
#region "Costruttori"
public CardsTotInfo() { }
public CardsTotInfo(string No, string lastOffer, string lastOfferDate, string closingReason, string status)
{
this.NO = No;
this.LAST_OFFER = lastOffer.ToUpper();
this.LAST_OFFER_DATE = lastOfferDate.ToUpper();
this.CLOSING_REASON = closingReason.ToUpper();
this.STATUS = status.ToUpper();
}
}
I have passed, or better say i think i have passed in the correct way the values of clsIn but i do not know how to pass the properties of the array type CT[].
I really need help.
Thank you in advance.
If CT is an object array and the data you get from the web service always comes in the same order, for instance using an arbitrary example:
object[] CT = { 1, DateTime.Now, "foo", true }
If you know that each property data inside the array will always be at the same index (you will always have a int in index 0 representing an Id, and a DateTime on index 1 representing the last offer day and so on)
I would say you need to set each property "manually":
private object TotInfo(clsIn In, CT ct)
{
TotInfo objFromCD = new TotInfo();
//get data from DB
//set the data from the array into the class properties
objFromCD.Id = (int)ct[0];
objFromCD.LastOfferDate = (DateTime)ct[1];
objFromCD.ClosingReason = (string)ct[2];
objFromCD.Available = (bool)ct[3];
return objFromCD;
}
I'm trying to deserialize xml data using xmlreader into a list object but I am getting a null back from my call. Here is a sample of my xml data...
<ExceptionLog>
<ExceptionLogData MessageCount="1" SourceDateTime="2016-02-08T09:32:41.713" MinSourceDateTime="2016-02-08T09:32:41.713" DataId="610029" MaxExceptionLogID="610029" MessageText="INVALID_SESSION_ID: Invalid Session ID found in SessionHeader: Illegal Session. Session not found, missing session hash: hX7K7LONeTilw5RfGT432g==
This is expected, it can happen if the session has expired and swept away, or if the user logs out, or if its just someone trying to hack in. " MachineName="VERTEXDPORTSQL1" AppDomainName="VTMS.Windows.SalesforceServicingAgent.exe" ProcessName="VTMS.Windows.SalesforceServicingAgent" />
<ExceptionLogData MessageCount="1" SourceDateTime="2016-02-08T09:22:39.340" MinSourceDateTime="2016-02-08T09:22:39.340" DataId="610028" MaxExceptionLogID="610028" MessageText="INVALID_SESSION_ID: Invalid Session ID found in SessionHeader: Illegal Session. Session not found, missing session hash: rtZTrLk2f99iVttLoz31tg==
This is expected, it can happen if the session has expired and swept away, or if the user logs out, or if its just someone trying to hack in. " MachineName="VERTEXDPORTSQL1" AppDomainName="VTMS.Windows.SalesforceServicingAgent.exe" ProcessName="VTMS.Windows.SalesforceServicingAgent" />
</ExceptionLog>
This is the object class code that I am trying to create...
public class ExceptionLog {
public ExceptionLog() {
ExceptionLogData = new List<ExceptionLogExceptionLogData>();
}
public List<ExceptionLogExceptionLogData> ExceptionLogData { get; set; }
}
public class ExceptionLogExceptionLogData {
private DateTime _sourceDateTimeField;
private DateTime _minSourceDateTimeField;
private uint dataIdField;
private uint _maxExceptionLogIdField;
private string _messageTextField;
private string _machineNameField;
private string _appDomainNameField;
private string _processNameField;
public byte MessageCount { get; set; }
public DateTime SourceDateTime {
get {
return _sourceDateTimeField;
}
set {
_sourceDateTimeField = value;
}
}
public DateTime MinSourceDateTime {
get {
return _minSourceDateTimeField;
}
set {
_minSourceDateTimeField = value;
}
}
public uint DataId {
get {
return dataIdField;
}
set {
dataIdField = value;
}
}
public uint MaxExceptionLogID {
get {
return _maxExceptionLogIdField;
}
set {
_maxExceptionLogIdField = value;
}
}
public string MessageText {
get {
return _messageTextField;
}
set {
_messageTextField = value;
}
}
public string MachineName {
get {
return _machineNameField;
}
set {
_machineNameField = value;
}
}
public string AppDomainName {
get {
return _appDomainNameField;
}
set {
_appDomainNameField = value;
}
}
public string ProcessName {
get {
return _processNameField;
}
set {
_processNameField = value;
}
}
}
And finally here is how I am trying to deserialize the data...
using (var dataReader = sqlCommand.ExecuteXmlReader())
{
var serializer = new XmlSerializer(typeof(ExceptionLog));
var returnDataList = serializer.Deserialize(dataReader) as List<ExceptionLogExceptionLogData>;
return returnDataList;
}
What have I missed or what am I doing wrong?
I have another approach that I can use until I figure this out and that is the old fashioned way of creating my object list and programmatically populating it with my objects on the fly - not very graceful but for the time being it works.
TIA
XmlSerializer is defined of type ExceptionLog but you're then casting the result to List
var serializer = new XmlSerializer(typeof(ExceptionLog));
var returnDataList = serializer.Deserialize(dataReader) as List<ExceptionLogExceptionLogData>;
The casting should be to the type of serializer:
var returnDataList = serializer.Deserialize(dataReader) as ExceptionLog;
I didn't check all the elements but you should also mark ExceptionLogData with XmlElement attribute.
[XmlElement]
public List<ExceptionLogExceptionLogData> ExceptionLogData { get; set; }
There might be some issues with the other properties but this should address the problem in the question
I am using JSON.NET to serialize some c# objects into JSON (and then write to a file).
My two main classes are:
public class Reservoir {
private Well[] mWells;
public Well[] wells {
get { return mWells; }
set { mWells = value; }
}
}
and
public Well() {
private string mWellName;
private double mY;
private double mX;
public string wellName {
get { return mWellName; }
set { mWellName = value; }
}
public double y {
get { return mY; }
set { mY = value; }
}
public double x {
get { return mX; }
set { mX = value; }
}
private Well[] mWellCorrelations;
}
The problem is that the output looks like:
'{"wells":[{"wellName":"B-B10","y":217.04646503367468,"x":469.5776343820333,"wellCorrelations":[{"wellName":"B-B12","y":152.71005958395972,"x":459.02158140110026,"wellCorrelations":[{"wellName":"B-B13","y":475.0,"x":495.14804408905263,"wellCorrelations":[{"wellName":"B-B11","y":25.0,"x":50.0,"wellCorrelations":[]}
i.e. the associated wells of each well object are expanded as objects themselves and this becomes a serious problem of space and time when there lots of associated objects.
I suppose I would have preferred something like:
'{"wells":[{"wellName":"B-B10","y":217.04646503367468,"x":469.5776343820333,"wellCorrelations":[{"wellName":"B-B12"}], {"wellName":"B-B11","y":217.04646503367468,"x":469.5776343820333,"wellCorrelations":[{"wellName":"B-B13"}
i.e maintaining only the well name as the link (assume its unique).
Is there a way to do this with JSON.NET?
You have set
serializer.ReferenceLoopHandling = ReferenceLoopHandling.Ignore;
but it doesn't make any difference.
You could add a new readonly property called WellCorrelations that only got the names of the well correlations, and slap a JsonIngore attribute on your mWellCorrelations, like so:
[JsonIgnore]
private Well[] mWellCorrelations;
public string[] WellCorrelations
{
get { return mWellCorrelations.Select(w => w.wellName).ToArray(); }
}
http://james.newtonking.com/projects/json/help/html/ReducingSerializedJSONSize.htm
That way, the serializer will only serialize the names of the correlated wells.
I have the following class:
public class VendorClass {
public int VendorID { get; set; }
public string VendorName { get; set; }
}
The fields above match fields in the database table.
In the case of say VendorName, how do I give it a field width ?
VendorName maps to a field in the database which is varchar(15)
You can't limit the length of the string but you can use properties with backing fields to achieve the desired result :
public class VendorClass
{
public int VendorID { get; set; }
private string _vendorName;
public string VendorName
{
get { return _vendorName; }
set
{
if (value.Length > 15)
{
_vendorName = value.Substring(0,15);
} else {
_vendorName = value;
}
}
}
}
Strings in C# have almost-arbitrary length.
When loading from your database, it will automatically accommodate the actual string length. When saving to the database, your business logic, data layer or ORM (as appropriate) will need to ensure the proper maximum length.
A string can't have a set length in C#. You will have to handle the db length through some other mechanism like validation. Can't really tell you more without more details.
I would question why you would do this in c# code. However this link has a couple of ways around this. I suppose either truncation or taking a subsring is the best option. You could also make sure that the UI (or the model-view) takes care of details such as this.
I am not sure exactly what you are asking, but if you want to know the maximum length of a string, this question can help you.
If you want to limit the number of characters entered, I would suggest that you use server-side validation and/or client-side validation.
I just met a problem like what you described and found a way to create a limited length's string. Maybe a little inflexible but concise when there are only finite varchar length definitions in database.
Firstly introduce some basic classes:
public class Length16
{
public static int StringLength { get => 16; }
}
public class Length8
{
public static int StringLength { get => 8; }
}
public class Length15
{
public static int StringLength { get => 15; }
}
public class LimitedLengthString<T>
{
private string _sValue;
public LimitedLengthString(string sNewValue)
{
_sValue = sNewValue;
}
public static implicit operator LimitedLengthString<T>(string sNewValue)
{
var prop = typeof(T).GetProperty("StringLength");
int iLength = (int)prop.GetValue(null);
if (sNewValue.Length > iLength)
{
throw new Exception($"New string is too long! Allowed length {iLength}.");
}
return new LimitedLengthString<T>(sNewValue);
}
public static implicit operator string(LimitedLengthString<T> sSource)
{
return sSource.ToString();
}
public override string ToString()
{
return _sValue;
}
}
public class AutoTruncatedString<T>
{
private string _sValue;
public AutoTruncatedString(string sNewValue)
{
_sValue = sNewValue;
}
public static implicit operator AutoTruncatedString<T>(string sNewValue)
{
var prop = typeof(T).GetProperty("StringLength");
int iLength = (int)prop.GetValue(null);
return new AutoTruncatedString<T>(sNewValue.Substring(0, iLength));
}
public static implicit operator string(AutoTruncatedString<T> sSource)
{
return sSource.ToString();
}
public override string ToString()
{
return _sValue;
}
}
Use them like this:
LimitedLengthString<Length8> sLimitedLength8;
sLimitedLength8 = "asdfgasdfg"; // will error out
AutoTruncatedString<Length8> sAutoTruncated8;
sAutoTruncated8 = "asdfgasdfg"; // will be truncated
sLimitedLength8 will throw an error if you try to assign a string longer than 8 and sAutoTruncated8 will truncate the string you assign to it.
For you, you can define the VendorName this way:
public LimitedLengthString<Length15> VendorName { get; set; }
Hope this could help you.
I am creating a dataGridView which gets its values from an arrayList. To retrieve the values, I have:
public DataSet(String totalCM, String totPM, String hkCM, String hkPM, String sherCM, String sherPM, String hinsCM, String hinsPM, String kassCM, String kassPM, String belleCM, String bellePM)
{
_totalCM = totalCM;
_totalPM = totPM;
_hiddenKnollsCM = hkCM;
_hiddenKnollsPM = hkPM;
_sherCM = sherCM;
_sherPM = sherPM;
_hinsdaleCM = hinsCM;
_hinsdalePM = hinsPM;
_kassonCM = kassCM;
_kassonPM = kassPM;
_belleCM = belleCM;
_bellePM = bellePM;
}
public String TotalCurrentMonth
{
get { return _totalCM; }
}
public String TotalPreviousMonth
{
get { return _totalPM; }
}
public String HiddenKnollsCurrentMonth
{
get { return _hiddenKnollsCM; }
}
public String HiddenKnollsPreviousMonth
{
get { return _hiddenKnollsPM; }
}
public String SherwoodCurrentMonth
{
get { return _sherCM; }
}
public String SherwoodPreviousMonth
{
get { return _sherPM; }
}
public String HinsdaleCurrentMonth
{
get { return _hinsdaleCM; }
}
public String HinsdalePreviousMonth
{
get { return _hinsdalePM; }
}
public String KassonCurrentMonth
{
get { return _kassonCM; }
}
public String KassonPreviousMonth
{
get { return _kassonPM; }
}
public String BelleIsleCurrentMonth
{
get { return _belleCM; ; }
}
public String BelleIslePreviousMonth
{
get { return _bellePM; }
}
}
How do I go about creating custom column headers for this, as they need to have a space? Probably a really dumb question, but could not find much information about this specific issue. I know that you can modify the properties in the collection of column names, but it just seems to create new column header names based off of the accessor methods in the code. Thanks in advance.
Well, if you're using .NET 2+ you can use the DisplayNameAttribute Class.
Use in on your properties to specify the string used in the column headers of your DGV; otherwise the property name will be used (as you see).
And if you're using .NET 2+ you shouldn't be using an ArrayList, use a List<T> instread.