Easiest way to access a class list - c#

First of all, it's my first post here so sorry if it's wrong or anything just warn me it will be better next time.
Second I speak french x).
Ok it's not a problem but I would like to have a better way to acces my data who are stored in a mother class. I know i'm not clear let me show you maybe you will understand.
Main.cs :
namespace Xahor
{
class Program
{
static void Main(string[] args)
{
TProduct test = new TProduct();
test.FillData(1, "Apple", "4857", "A tasty apple.", "Green", false, true);
test.PrintData();
}
}
}
TListNF.cs :
namespace Xahor
{
public class TListNF
{
public TListNF(int iProductID = 0, string sProductName = "", string sProductIDNum = "", string sProductDesc = "", string sColor = "", bool bMake = false, bool bCustom = false)
{
this.iProductID = iProductID;
this.sProductName = sProductName;
this.sProductIDNum = sProductIDNum;
this.sProductDesc = sProductDesc;
this.sColor = sColor;
this.bMake = bMake;
this.bCustom = bCustom;
}
public int iProductID { get; set; }
public string sProductName { get; set; }
public string sProductIDNum { get; set; }
public string sProductDesc { get; set; }
public string sColor { get; set; }
public bool bMake { get; set; }
public bool bCustom { get; set; }
protected List<TListNF> ItemList = new List<TListNF>();
}
}
TProduct.cs :
namespace Xahor
{
class TProduct : TListNF
{
public void FillData(int iProductID, string sProductName, string sProductIDNum, string sProductDesc, string sColor, bool bMake, bool bCustom)
{
ItemList.Add(new TListNF(iProductID, sProductName, sProductIDNum, sProductDesc, sColor, bMake, bCustom));
}
public void PrintData()
{
foreach (TListNF t in ItemList)
{
//Here where * is each of the accessor
Console.WriteLine(t.*.ToString());
Console.WriteLine(t.*.ToString());
...
}
}
}
}
So, basicly what I don't know how to do is to get an easier acces to the getter what it would normally be a foreach so each time we enter the loop the var t get the value
Resolved
#Nair
Thank you I've figured out with the post
How to loop through all the properties of a class?
But your answer help by the way if anyone else need somethings like this I,ve used
foreach (PropertyInfo p in list.GetType().GetProperties())
{
Console.WriteLine(p.Name + " : " + p.GetValue(list));
}
//Where list is the List<ClassName_With_Accesor> list;

You can achieve it through reflecting individual properties in the type. You may need to add more constructive logic as the intention is to show the idea.
foreach (TListNF t in ItemList)
{
foreach (PropertyInfo proInfo in t.GetType().GetProperties())
{
Console.WriteLine(proInfo.GetGetMethod().Invoke(t,null).ToString());
}
}

Of course the answer #Nair is correct. But usually it's bad practice to use reflection to for easy purposes. This is because of access rights of your application and so on. Here two other options you might try.
Option 1: Yield the properties in an own method (see TListNF.GetItemInfo). Disadvantage: You have to add another yield it in the implementation of GetItemInfo everytime you add another property to your TListNF class. Advantage: No use of reflection at all.
Option 2: Use an own attribute (see MyInfoAttribute) to mark the properties that are of interest to you. Usually classes do have several more attribute that you don't want to print. Advantage: Only marked attributes are printed: Disadvantage: Use of reflection.
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Reflection;
namespace ConsoleApplication1
{
class Program
{
[AttributeUsage(AttributeTargets.Property)]
public class MyInfoAttribute : Attribute
{
}
public class TListNF
{
public TListNF(int iProductID = 0, string sProductName = "", string sProductIDNum = "", string sProductDesc = "", string sColor = "", bool bMake = false, bool bCustom = false)
{
this.iProductID = iProductID;
this.sProductName = sProductName;
this.sProductIDNum = sProductIDNum;
this.sProductDesc = sProductDesc;
this.sColor = sColor;
this.bMake = bMake;
this.bCustom = bCustom;
}
[MyInfo]
public int iProductID { get; set; }
[MyInfo]
public string sProductName { get; set; }
[MyInfo]
public string sProductIDNum { get; set; }
[MyInfo]
public string sProductDesc { get; set; }
[MyInfo]
public string sColor { get; set; }
[MyInfo]
public bool bMake { get; set; }
[MyInfo]
public bool bCustom { get; set; }
protected List<TListNF> ItemList = new List<TListNF>();
public IEnumerable<string> GetItemInfo()
{
yield return iProductID.ToString();
yield return sProductName;
yield return sProductIDNum;
yield return sProductDesc;
yield return sColor;
yield return bMake.ToString();
yield return bCustom.ToString();
}
}
class TProduct : TListNF
{
public void FillData(int iProductID, string sProductName, string sProductIDNum, string sProductDesc, string sColor, bool bMake, bool bCustom)
{
ItemList.Add(new TListNF(iProductID, sProductName, sProductIDNum, sProductDesc, sColor, bMake, bCustom));
}
public void PrintData()
{
foreach (TListNF item in ItemList)
{
foreach (string info in item.GetItemInfo())
Console.WriteLine(info);
}
}
public void PrintDataReflection()
{
Type type = typeof(MyInfoAttribute);
foreach (TListNF item in ItemList)
{
foreach (PropertyInfo proInfo in item.GetType().GetProperties().Where(p => p.GetCustomAttributes(type, true).Length > 0))
{
Console.WriteLine(proInfo.GetGetMethod().Invoke(item, null).ToString());
}
}
}
}
static void Main(string[] args)
{
var test = new TProduct();
test.FillData(1, "Apple", "4857", "A tasty apple.", "Green", false, true);
test.PrintData();
test.PrintDataReflection();
Console.ReadKey();
}
}
}

Related

Type Casting Exception With Inheritance

Here is my code below. It gives me Casting exception problem at selIngs.Add(da). tried with the 2nd way. it still give me the same exception. I wonder where I am doing wrong? Once I implement the interface or inherit the base class it should be ok to treat child class as the same. Any idea please?
//1st way
public interface IngredientInterface
{
double Concentration { get; set; }
string DateCreated { get; set; }
string DevCode { get; set; }
}
public class IngredientData : INotifyPropertyChanged, IngredientInterface
{
public string GroupCode
{
get { return groupCode; }
set
{
groupCode = value;
}
}
public double Setpoint { get; set; }
public bool IsHighlighted { get; set; }
public double PPT { get; set; }
}
public class FormulaUploadViewModelData: IngredientData
{
//.....
}
public class FormulaUploadViewModel :INotifyPropertyChanged
{
public FormulaUploadViewModel()
{
selIngs = new List<FormulaUploadViewModelData>();
}
private void IngsUp()
{
List<IngredientData> someIngData = new List<IngredientData>();
foreach (FormulaUploadViewModelData da in someIngData)
{
selIngs.Add(da); //here gives me casting exception
}
}
}
//2nd way
public class FormulaUploadViewModelData: IngredientInterface
{
//.....
}
public class FormulaUploadViewModel :INotifyPropertyChanged
{
public FormulaUploadViewModel()
{
selIngs = new List<FormulaUploadViewModelData>();
}
private void IngsUp()
{
List<IngredientInterface> someIngData = new List<IngredientInterface>();
foreach (FormulaUploadViewModelData da in someIngData)
{
selIngs.Add(da); //here gives me casting exception
}
}
}
All FormulaUploadViewModelData are IngredientInterface. So this will work:
var ingredients = new List<IngredientInterface>();
ingredients.Add(new FormulaUploadViewModelData());
But the opposite does not work because not all IngredientInterface are FormulaUploadViewModelData which is what should follow from allowing:
var formulas = new
List<FormulaUploadViewModelData>();
formulas(someIngredientInterface);
Solution? Make sure the da you are adding is in fact a FormulaUploadViewModelData. There is quite a few ways to do it, to name a couple:
Pattern matching
foreach (var da in someInData)
if (da is FormulaUploadViewModelData formula)
selIngs.Add(formula)
Use Enumerable.OfType<> extension method
foreach (var formula in
someInData.OfType<FormulaUploadViewModelData>())
selIngs.Add(formula)
Etc.

C# Accessing custom attribute of owner object

How can I access the custom attribute of the parent or owner object.
Look at the FieldInfo property of the SQLFieldInfo struct
Here's a more detailed program that will compile and run that shows what I need.
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
Employee myclass = new Employee();
// Load from sql server...
myclass.Name = "Alain";
myclass.Age = 51;
//----
MessageBox.Show(myclass.Name.ToString()); // Should return Alain
MessageBox.Show(myclass.Age.FieldInfo.Type.ToString()); // Should output "int"
}
}
// This next class is generated by a helper exe that reads SQL table design and create the class from it
[SQLTableAttribute(DatabaseName = "Employees", Schema = "dbo", TableName = "Employees")]
public class Employee
{
[SQLFieldAttribute(FieldName = "ID", Type = SqlDbType.Int)]
public SQLFieldInfo<int> ID { get; set; }
[SQLFieldAttribute(FieldName = "Name", Type = SqlDbType.NVarChar, Size = 200)]
public SQLFieldInfo<String> Name { get; set; }
[SQLFieldAttribute(FieldName = "Age", Type = SqlDbType.Int)]
public SQLFieldInfo<int> Age { get; set; }
}
public struct SQLFieldInfo<T>
{
private readonly T value;
public SQLFieldInfo(T Value)
{
this.value = Value;
}
public static implicit operator SQLFieldInfo<T>(T Value)
{
return new SQLFieldInfo<T>(Value);
}
public T Value
{
get
{
return this.value;
}
}
public override string ToString()
{
return this.value.ToString();
}
public SQLFieldAttribute FieldInfo
{
get
{
// Need to retreive the attribute class of the parent or declaring member
return null;
}
}
}
// Holds the sql field information
public class SQLFieldAttribute : Attribute
{
public string FieldName { get; set; }
public SqlDbType Type { get; set; }
public bool AllowNull { get; set; }
public int Size { get; set; }
}
// Holds the sql table information
public class SQLTableAttribute : Attribute
{
public string DatabaseName { get; set; }
public string Schema { get; set; } = "dbo";
public string TableName { get; set; }
}
Thank you!
Alain
My data class is as follows (should be fairly translatable to A above):
public class Foo
{
[Argument(Help = "Name", AssignmentDelimiter = "=")]
public string Name
{
get;
set;
}
}
A helper class is responsible of reading attribute values of objects:
static public string GetCommandLineDelimiter<T>(Expression<Func<T>> property)
{
if(property != null)
{
var memberExpression = (MemberExpression)property.Body;
string propertyName = memberExpression.Member.Name;
PropertyInfo prop = typeof(Arguments).GetProperty(propertyName);
if(prop != null)
{
object[] dbFieldAtts = prop.GetCustomAttributes(typeof(ArgumentAttribute), true);
if(dbFieldAtts.Length > 0)
{
return ((ArgumentAttribute)dbFieldAtts[0]).AssignmentDelimiter;
}
}
}
return null;
}
To use it, simply:
string delimiter = GetCommandLineDelimiter(() => myObject.Name);
That will get the attribute value of AssignmentDelimiter on property Name, i.e. "=".
First, MSDN is your friend.
Then, if you want to get the attributes for ancestors just specify true in the inherit flag of the method:
var attribute = typeof(A).GetProperty("myprop").GetCustomAttributes(true)
.OfType<MycustomAttrib>().FirstOrDefault();
This works. I am doing a lazy initialization of a reference to the custom attribute by using reflection to look at all the properties of all the types.
public class MycustomAttribAttribute : Attribute
{
public MycustomAttribAttribute(string name)
{
this.Name=name;
}
public string Name { get; private set; }
}
class A
{
public A() { MyProp=new B(); }
[MycustomAttrib(name: "OK")]
public B MyProp { get; set; }
}
class B
{
private static Lazy<MycustomAttribAttribute> att = new Lazy<MycustomAttribAttribute>(() =>
{
var types = System.Reflection.Assembly.GetExecutingAssembly().DefinedTypes;
foreach(var item in types)
{
foreach(var prop in item.DeclaredProperties)
{
var attr = prop.GetCustomAttributes(typeof(MycustomAttribAttribute), false);
if(attr.Length>0)
{
return attr[0] as MycustomAttribAttribute;
}
}
}
return null;
});
public string MyProp2
{
get
{
return att.Value.Name;
}
}
}
class Program
{
static void Main(string[] args)
{
// Finds the attribute reference and returns "OK"
string name = (new A()).MyProp.MyProp2;
// Uses the stored attribute reference to return "OK"
string name2 = (new A()).MyProp.MyProp2;
}
}

How do I get the type of my ReportItems

This is a continuation of another post. I'm trying to create an interface that will let me walk through a collection of objects, and access the name of the properties of the object.
A Report object will have ReportSections. A ReportSection will have an ICollection of items which will change depending on usage.
Here's how I'm trying to define it now.
public interface IReport
{
string ReportName { get; set; }
ICollection<IReportSection> ReportSections { get; }
}
public interface IReportSection
{
string ReportSectionName { get; set; }
ICollection ReportItems { get; }
}
public abstract class ReportBase : IReport
{
virtual public string ReportType { get; set; }
virtual public string ReportName { get; set; }
virtual public ICollection<IReportSection> ReportSections { get; set; }
}
public abstract class ReportSectionBase : IReportSection
{
public string ReportSectionName { get; set; }
public ICollection ReportItems { get; set; }
}
In my code, I would do this:
public class BookAffiliates : ReportSectionBase
{
public override string ReportSectionName { get { return "Book Affiliates"; } }
public override ICollection ReportItems { get; set; }
}
public class SomeClass
{
public ICollection<AuthorsViewModel> Authors { get; set; }
public ICollection<ProjectSubmissionViewModel> Submissions { get; set; }
public string ProcessAuthorsReport()
{
var report = new ContribAuthorsReport{ ReportType = "CSV" };
var authorAffil = new BookAffiliates {ReportItems = Authors };
report.ReportSections.Add(chapAffil);
var submissionAffil = new BookAffiliates {ReportItems = Submissions};
report.ReportSections.Add(submissionAffil );
return RenderReport(report)
}
}
In RenderReport I would like to walk through the collections and use the PropertyNames:
private string RenderReport(ReportBase currentReport)
{
var reportBody = new StringBuilder();
reportBody.Append(currentReport.ReportName + Environment.NewLine + Environment.NewLine);
foreach (var thisSection in currentReport.ReportSections)
{
reportBody.Append(thisSection.ReportSectionName + Environment.NewLine);
/// ---- Here! Here! I don't know what type, I want the
/// code to get the type based on ReportSectionBase<T>
var firstItem = thisSection.ReportItems.OfType<???Type???>().FirstOrDefault();
// I would actually like to go through each property of
// the ReportItem type and list it here.
foreach(var prop in firstItem.GetType().GetProperties())
{
reportBody.AppendLine(string.Format("{0}:{1}" prop.Name, prop.Value));
}
}
return reportBody.ToString();
}
I'm not sure how to best define this. I'm pretty sure I've done it before, but it's not coming to me.
You would use Reflection to do it.
foreach(var prop in thisItem.GetType().GetProperties())
{
reportBody.AppendLine(string.Format("{0}:{1}" prop.Name, prop.Value));
}
Took a while, a lot of questions, and figuring out what I really wanted to ask. I came up with this.
Here are my interfaces and base classes:
public class ReportBase
{
public ReportBase()
{
ReportSections = new List<IReportSection>();
}
public string ReportType { get; set; }
public string ReportName { get; set; }
public ICollection<IReportSection> ReportSections { get; set; }
}
public interface IReportSection
{
string ReportSectionName { get; }
ICollection ReportItems { get; set; }
}
public class ReportSection<T> : IReportSection
{
public string ReportSectionName { get; set; }
public ICollection<T> ReportItems { get; set; }
ICollection IReportSection.ReportItems
{
get { return ReportItems as ICollection; }
set { ReportItems = value as ICollection<T>; }
}
}
I create my objects like this:
public ReportBase GetContribAuthorsReport
(
ICollection<ProjectAffiliateViewModel> projectAffiliates,
ICollection<SubmissionAffiliateViewModel> submissionAffiliates
)
{
//var caReport = new ContributingAffiliates("CSV", projectAffiliates, submissionAffiliates);
var caReport = new ReportBase { ReportType = "CSV", ReportName = "Reviewers' Contact Information" };
caReport.ReportSections.Add(new ReportSection<ProjectAffiliateViewModel> { ReportItems = projectAffiliates });
caReport.ReportSections.Add(new ReportSection<SubmissionAffiliateViewModel> { ReportItems = submissionAffiliates });
return caReport;//.Report;
}
Then I iterate through the objects like this:
public class DownloadCsvActionResult : ActionResult
{
public ReportBase Report { get; set; }
public string fileName { get; set; }
private string ReportData { get; set; }
public DownloadCsvActionResult(ReportBase report, string pFileName)
{
Report = report;
fileName = pFileName;
ReportData = RenderReport();
}
private string RenderReport()
{
var reportBody = new StringBuilder();
reportBody.AppendLine(Report.ReportName);
reportBody.Append(Environment.NewLine + Environment.NewLine);
foreach (var thisSection in Report.ReportSections)
{
reportBody.Append(thisSection.ReportSectionName + Environment.NewLine);
if (thisSection.ReportItems != null)
{
var itemType = thisSection.ReportItems.GetType().GetGenericArguments().Single();
var first = true;
foreach (var prop in itemType.GetProperties())
{
if (!first) reportBody.Append(",");
DisplayAttribute attribute = prop.GetCustomAttributes(typeof(DisplayAttribute), false)
.Cast<DisplayAttribute>()
.SingleOrDefault();
string displayName = (attribute != null) ? attribute.Name : prop.Name;
reportBody.Append(displayName);
first = false;
}
reportBody.Append(Environment.NewLine);
foreach (var thisItem in thisSection.ReportItems)
{
var firstData = true;
foreach (var prop in itemType.GetProperties())
{
if (!firstData) reportBody.Append(",");
reportBody.Append(prop.GetValue(thisItem,null));
firstData = false;
}
reportBody.Append(Environment.NewLine);
}
}
reportBody.Append(Environment.NewLine);
}
return reportBody.ToString();
}
public override void ExecuteResult(ControllerContext context)
{
//Create a response stream to create and write the Excel file
HttpContext curContext = HttpContext.Current;
curContext.Response.Clear();
curContext.Response.AddHeader("content-disposition", "attachment;filename=" + fileName);
curContext.Response.Charset = "";
curContext.Response.Cache.SetCacheability(HttpCacheability.NoCache);
curContext.Response.ContentType = "application/vnd.ms-excel";
//Write the stream back to the response
curContext.Response.Write(ReportData);
curContext.Response.End();
}
}
This gives me what I need for now. Sorry I wasn't as clear in the first place, and thank you for all your help.

What is NZEC error in C# and where it is in following code snippet

This is the first time I am getting NZEC error. I don't understand this error, I just found that it stands for "Non Zero Exit Code". But what does this means. When we get this error. I am getting this error on one of online coding challenge websites and not in Visual Studio.
I am real need of help. Please check following code snippet where I am getting this error and please suggest what I am doing wrong.
using System;
using System.Collections.Generic;
using System.Linq;
namespace PractiseConsoleApplication
{
internal class Program
{
private static void Main(string[] args)
{
//Get input
var stringInput = Console.ReadLine();
var numberOfRooms = Convert.ToInt32(Console.ReadLine());
var endOfInput = Convert.ToInt32(Console.ReadLine());
var customers = stringInput.ToArray();
var hotel = new Hotel(numberOfRooms);
foreach (var customerName in customers)
{
hotel.CheckInCheckoutCustomer(customerName);
}
Console.WriteLine(Convert.ToString(hotel.CustomersLeftWithoutStayingInHotel.Count));
}
}
internal class Hotel
{
public bool IsRoomAvailable { get; private set; }
public List<HotelRoom> Rooms { get; private set; }
public List<char> CustomersLeftWithoutStayingInHotel { get; private set; }
private Hotel()
{
Rooms = new List<HotelRoom>();
CustomersLeftWithoutStayingInHotel = new List<char>();
}
public Hotel(int numberOfRooms)
: this()
{
for (int i = 1; i <= numberOfRooms; i++)
{
Rooms.Add(new HotelRoom(i));
}
}
public void CheckInCheckoutCustomer(char customer)
{
if (CustomersLeftWithoutStayingInHotel.Any(f => f == customer))
{
return;
}
var existingCustomer = Rooms.FirstOrDefault(f => f.IsRoomAllocatedToCustomer && f.CustomerName == customer);
//Already room allocated to this customer
if (existingCustomer != null)
{
//checkout him
existingCustomer.CustomerCheckout();
}
else
{
//Get empty rooms
var emptyRoom = Rooms.FirstOrDefault(f => f.IsRoomAllocatedToCustomer == false);
if (emptyRoom != null)
{
emptyRoom.AllocateRoomToCustomer(customer);
}
else
{
CustomersLeftWithoutStayingInHotel.Add(customer);
}
}
}
}
internal class HotelRoom
{
public int RoomNumber { get; private set; }
public char? CustomerName { get; private set; }
public HotelRoom(int roomNumber)
{
RoomNumber = roomNumber;
CustomerName = null;
}
public bool IsRoomAllocatedToCustomer
{
get
{
return CustomerName.HasValue;
}
}
public void AllocateRoomToCustomer(char customerDetails)
{
CustomerName = customerDetails;
}
public void CustomerCheckout()
{
CustomerName = null;
}
}
internal class Customer
{
public char CustomerName { get; private set; }
public Customer(char name)
{
CustomerName = name;
}
}
}
as Tony Hopkinson says in the comments, main should return an int.
place this statement in the end of main method:
return 0;
this will exit with zero code(thus the Non Zero Exit Code error)

Parsing JSON into Object

Trying to get the result from a webservice call to return a Model. I eep getting the error:
Cannot deserialize the current JSON array (e.g. [1,2,3]) into type 'CI.Models.Schedule' because the type requires a JSON object (e.g. {"name":"value"}) to deserialize correctly.
public Schedule getCourseSchedule()
{
var obj = new
{
States = new[] { new { State = "MX" } },
Zip = "",
Miles = "",
PaginationStart = 1,
PaginationLimit = 3
};
using (var client = new WebClient())
{
client.Headers[HttpRequestHeader.ContentType] = "apoplication/json";
var url = "http://192.168.1.198:15014/ShoppingCart2/CourseSchedule";
var json = JsonConvert.SerializeObject(obj);
byte[] data = Encoding.UTF8.GetBytes(json);
byte[] result = client.UploadData(url, data);
string returnjson = Encoding.UTF8.GetString(result);
Schedule sched = JsonConvert.DeserializeObject<Schedule>(returnjson);
return sched;
}
}
Schedule Model:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Globalization;
namespace CI.Models
{
public class Schedule
{
public IEnumerable<Course> Courses { get; set; }
}
public class Course
{
/*
JSON Data returned from web service:
{
"ProgramGroup":"MR",
"ProgramCode":"RM",
"EventCode":"20160901MXMR",
"FormalDate":"September 1-2, 2016",
"StartDate":"2016\/09\/01",
"Price":5,
"LocName":"WB Hotel",
"LocAddress":"Av. Speedy Gonzales 220",
"LocCity":"Monterrey",
"LocState":"MX",
"LocZipCode":null,
"LicenseeURL":null,
"AgendaURL":"NA",
"SeatsAreAvailable":"2",
"GeneralInfoHTML":"General Info goes here.",
"GateKeeperHTML":null,
"EventType":"SS",
"TotalCourses":3
}
*/
public string ProgramGroup { get; set; }
public string ProgramCode { get; set; }
public string EventCode { get; set; }
public string FormalDate { get { return FormalDate; } set { FormalDate = convertFormalDateToSpanish(value); } }
public string StartDate { get; set; }
public double Price { get; set; }
public string LocName { get; set; }
public string LocAddress { get; set; }
public string LocCity { get ; set; }
public string LocState { get; set; }
public string LocZipCode { get; set; }
public string LicenseeURL { get; set; }
public string AgendaURL { get { return AgendaURL; } set { AgendaURL = buildAgendaLink(value); } }
public string SeatsAreAvailable { get; set; }
public string GeneralInfoHTML { get; set; }
public string GateKeeperHTML { get; set; }
public string EventType { get; set; }
public int TotalCourses { get; set; }
public string convertFormalDateToSpanish(string val)
{
DateTime TheDate = DateTime.Parse(StartDate);
string[] FormalDate = val.Split(" ".ToCharArray());
CultureInfo ci = new CultureInfo("es-ES");
string _Date = FormalDate[1].Replace("-", " al ").Replace(",", "");
string _Month = ci.TextInfo.ToTitleCase(TheDate.ToString("MMMM", ci));
val = string.Concat(_Date, " ", _Month);
return val;
}
private string buildAgendaLink(string val)
{
if (val.Trim() != "")
{
val = string.Concat("Agenda");
}
else
{
val = "Agenda";
}
return val;
}
}
}
Your server returns an array. Just try
Course[] courses = JsonConvert.DeserializeObject<Course[]>(returnjson);
Note that this is not an answer to your original problem, but I added it like an answer in order to explain my comment above with some actual code.
First problem with your code is that FormalDate and AgendaUrl properties simply won't work. Accessing them will result in a StackOverflowException, because you basically defined them recursively.
A property is merely syntax sugar for two separate getter/setter methods, so by writing this:
public class Course
{
public string FormalDate
{
get { return FormalDate; }
}
}
You are basically writing this:
public class Course
{
public string GetFormalDate()
{
// recursive call, with no terminating condition,
// will infinitely call itself until there is no
// more stack to store context data (and CLR
// will then throw an exception)
return GetFormalDate();
}
}
To fix that, you need to add an actual backing field, e.g.:
public class Course
{
private string _formalDate; // <-- this is a backing field;
// and this property uses the backing field to read/store data
public string FormalDate
{
get { return _formalDate; }
set { _formalDate = convertFormalDateToSpanish(value); }
}
}
Additionally, it's unusual for a property getter to return a different value than the one set through a setter. In other words, I would never expect this from a class:
var course = new Course();
course.StartDate = "2016/09/01";
course.FormalDate = "September 1-2, 2016";
Console.WriteLine(course.FormalDate); // prints "1 al 2 Septiembre" ?
I would rather move this functionality into a different class, or at least create different properties which return these values:
public class CourseInfo
{
// this is now a "dumb" auto-implemented property
// (no need for a backing field anymore)
public string FormalDate { get; set; }
// this read-only property returns the converted value
public string LocalizedFormalDate
{
get
{
return convertFormalDateToSpanish(FormalDate);
}
}
}

Categories