List of objects in C# / Unity and yearly check - c#

I've been working on this app about trees and have found myself stuck. What I have is a Tree class with some attributes to it, like ID, type and others. I need to be able to make a List or some other structure of a user-entered number of trees and have each tree be separately modified (in terms of its attributes). Now a problem I ran into is the fact that I need to have separate attributes for each tree each year (2017, 2018 and so on) since the installation of the app. I seem to not be able to think of a viable solution for this to work. How would I set separate attributes for each year? Hell, how would I even check when a new year is and update the list accordingly? I need the yearly attributes because I need to label a tree that hasn't had problems the past five years as "strong".
Here's the code for my simple Tree class:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
public class Tree : MonoBehaviour {
public string leafType { get { return leafType; } set { } }
public string leafGender { get { return leafGender; } set { } }
public string fruitType { get { return fruitType; } set { } }
public string fruitProblems { get { return fruitProblems; } set { } }
public string bloomingStart { get { return bloomingStart; } set { } }
public string bloomingEnd { get { return bloomingEnd; } set { } }
public string kgHazelnutPerYear { get { return kgHazelnutPerYear; } set { } }
public string problemsInLeaf { get { return problemsInLeaf; } set { } }
public string pests { get { return pests; } set { } }
public string usedPesticides { get { return usedPesticides; } set { } }
public string altitude { get { return altitude; } set { } }
public string region { get { return region; } set { } }
}
Thanks in advance!

you can use Dictionary to manipulate it like bellow
void addYear(string year, Dictionary<string, List<YourTreeClass>> data)
{
if (!data.ContainsKey(year)) data.Add(year, new List<YourTreeClass>());
}
void addTree(string year,YourTreeClass tree, Dictionary<string, List<YourTreeClass>> data)
{
data[year].Add(tree);
}
Dictionary<string, List<YourTreeClass>> data = new Dictionary<string, List<YourTreeClass>>();
addYear("2017", data);
addTree("2017", tree1, data);
addTree("2017", tree2, data);
addYear("2018", data);
addTree("2018", tree3, data);

Related

How to send action with AsterNET, C#?

I'm working with AsterNET and C#, I need to get the status of all the extensions, specifically the result of Action: ExtensionStateList but the library doesn't have this action, I'm trying to create it but I can't get it. I hope someone can guide me. Attached is the code.
ResponseEvents re;
try
{
re = manager.SendEventGeneratingAction(new ExtensionStateListAction());
}
catch (EventTimeoutException e)
{
re = e.PartialResult;
}
foreach (ManagerEvent e in re.Events)
{
foreach (KeyValuePair<string, string> d in e.Attributes)
{
Console.WriteLine(e);
}
}
using System;
using AsterNET.Manager.Event;
namespace AsterNET.Manager.Action
{
public class ExtensionStateListAction : ManagerActionEvent
{
public override string Action
{
get { return "ExtensionStateList"; }
}
public override Type ActionCompleteEventClass()
{
return typeof (ExtensionStateListCompleteEvent);
}
}
}
namespace AsterNET.Manager.Event
{
public class ExtensionStateListCompleteEvent : ResponseEvent
{
private int listItems;
public int ListItems
{
get { return this.listItems; }
set { this.listItems = value; }
}
public ExtensionStateListCompleteEvent(ManagerConnection source)
: base(source)
{
}
}
}
Result of this command will come in set of events "ExtensionState" and final "ExtensionStateCompleate"
It will be asyncronous.
You should setup event listener and parse that.

How to add edges by unwind using neo4jclient?

The nodes have existed.
I tried to add edges by unwind,but my function importBuyConnectionIntoNeo4j didn't work,
Is there any one can help me?
the data structure:
class Connection
{
private string type;
public string Type
{
get { return type; }
set { type = value; }
}
private string source;
public string Source
{
get { return source; }
set { source = value; }
}
private string target;
public string Target
{
get { return target; }
set { target = value; }
}
}
class BuyConnection:Connection
{
}
myFunction:
public void importBuyConnectionIntoNeo4j(List<BuyConnection> connectionList)
{
GraphClient client = createConnectionToNeo4j();
client.Cypher
.Unwind(connectionList, "connection")
.Match("(source:Person),(target:Vegetable)")
.Where("source.Name=connection.Source AND target.Type=connection.Target")
.Create("(source)-[:Buy]->(target)")
.ExecuteWithoutResults();
}
I think the issue is with your .where text:
.Where("source.Name=connection.Source AND target.Type=connection.Target")
is the Source and Target the right way around?

How can I quickly display complex data on a asp.net page

How should one display complex data objects in a asp.net page? Actually the below class is read only, the data needs to be displayed on screen so that agent's can tell customers how much they have to pay. I don't want to write lot of code to normalize it and show it on the page. This data is returned by web service method call.
public class FeeInfo {
private string countryInfoTextField;
public string CountryInfoTextField
{
get{return this.countryInfoTextField;}
set{this.countryInfoTextField= value;}
}
private stringfeeInfoTextField;
public string FeeInfoTextField
{
get{return this.feeInfoTextField;}
set{this.feeInfoTextField= value;}
}
private string taxInfoTextField;
public string TaxInfoTextField
{
get{return this.taxInfoTextField;}
set{this.taxInfoTextField = value;}
}
private string additionalInfoTextField;
public string additionalInfoText
{
get{return this.additionalInfoTextField;}
set{this.additionalInfoTextField = value;}
}
private PromotionInfo[] promotionInfoField; //Class
private SendAmountInfo sendAmountsField; //Class
private EstimatedReceiveAmountInfo receiveAmountsField; //Class
/// <remarks/>
[System.Xml.Serialization.XmlElementAttribute("promotionInfo")]
public PromotionInfo[] promotionInfo {
get {
return this.promotionInfoField;
}
set {
this.promotionInfoField = value;
}
}
/// <remarks/>
public SendAmountInfo sendAmounts {
get {
return this.sendAmountsField;
}
set {
this.sendAmountsField = value;
}
}
/// <remarks/>
public EstimatedReceiveAmountInfo receiveAmounts {
get {
return this.receiveAmountsField;
}
set {
this.receiveAmountsField = value;
}
}
}
I converted the above into a list and bound it to gridview, but all I see is the 4 fields, not the last 3 complex properties. What could be the fast and quick way to show this data on screen?
add a toList method to FeeInfo.
public List<FeeInfo> toList(){
var retList;
var props = this.GetType().GetProperties();
foreach(var prop in props) {
if(prop.propertyType.IsArray)
for(int i=0;i<prop.length;i++)
retList.add(new GridView(prop.GetValue[i]);
else if(prop.propertyType.isClass)
retList.Add(new Gridview(prop.GetValue()));
else retList.Add(prop.GetValue());
}
return retList;
}
dont have VS to test atm unfortunatly so there's probably some adjustments to make :)

Using { get set } Accessors for Multiple Values in MVC

EDIT: Question Reconstructed.
OK, I have revisited my get and set methods, but I am still very unclear on how it all works.
What I want to achieve is the Model is populated by the Controller, from the values that it takes form the form. This is then sent to the Db_Facade, which compares the uName and uPwd, and if they are equal returns the ACCESS, which will be set for the entire scope of the program.
I don't know if the get and set declarations are done correctly, or if they can be bunched together (If this is possible it would be great because I will be using this for much larger collections of data), and I'm pretty sure I'm implementing them wrong as well.
If you can help, my knowledge of Accessors is incredibly limited.
Here is my Compare Login method in my Controller:
public static void Compare_Login(User_Login_View Login_View)
{
User_Model getACCESS = new User_Model(); // Creates a new oject of User_Model
getACCESS.Name = Login_View.txtUsername.Text; //Populates the Model from the Login View
getACCESS.Pwd = Login_View.txtPassword.Text;
if (getACCESS.ACCESSLEVEL > 0)
{
Login_View.Close();
}
else
{
Login_View.lblError.Visible = true;
}
Login_View.Menu.SetMenuView();
}
Here is my Model:
public class User_Model
{
public string Name
{
get
{
return Db_Facade.uName;
}
set
{
Db_Facade.uName = value;
}
}
public string Pwd
{
get
{
return Db_Facade.uPwd;
}
set
{
Db_Facade.uPwd = value;
}
}
public int ACCESSLEVEL
{
get
{
return Db_Facade.ACCESS;
}
set
{
Db_Facade.ACCESS = value;
}
}
}
Here is the dummy database comparison:
class Db_Facade
{
public static string uName;
public static string uPwd;
public static string cPwd;
public static int ACCESS;
public static void getLoginACCESS()
{
uName = "paul";
uPwd = "pwd";
ACCESS = 1;
/* I get a "getACCESS does not exist" error here
if (uName == getACCESS.Name && uPwd == getACCESS.Pwd)
{
getACCESS.ACCESSLEVEL = ACCESS;
}
else
{
getACCESS.ACCESSLEVEL = 0;
}
*/
}
}
I don't know if it's needed, but here is my View
public partial class User_Login_View : Form
{
public Menu_View Menu { get; set; }
public User_Login_View()
{
InitializeComponent();
}
private void btnLogin_Click(object sender, EventArgs e)
{
User_Controller.Compare_Login(this);
}
}
2 Questions / Hints
1.) Where do you call your getLoginACCESS() ?
2.) Why do you think Db_Facade is able to access getACCESSfrom your class User_Controller?
a solution would be to modyfie your getLoginACCESS() to getLoginACCESS(User_Model getACCESS) and than call it in your Compare_Login(User_Login_View Login_View) befor your if like Db_Facade.etLoginACCESS(getACCESS);

Is it worthwhile to strongly-type datarows?

I have a datarow that I pass around and do things with, and would like to strongly type it, but don't need to strongly-type the table itself.
Is there a tool that will autogenerate a strongly-typed row with isnull methods and such?
In my opinion is is worthwhile to create a strongly-typed class for ADO data types. There are two ways you can do this:
Hand-code a subclass of DataRow or whatever that encapsulates the behavior you want.
Write an XSD file of your data and let Visual Studio construct strongly-typed classes.
The advantage of the first method is that it provides a custom API exposing exactly what you want. The second method is often faster.
It is worthwhile since you could get compile time checking with strongly typed DataRow/DataSet.
The code below (just a sample) shows how I do it. I have a tool that generates all of the classes from Database information, in particular the output of stored procedures. So I have a stored procedure that returns a result set whose fields map to the properties of the PostDtw class below.
The tool (with source) is available on my blog. It also generates DataReaders wrappers in a similar manner. You can get the tool from here
Data Access Layer CodeGen
The "Main" method below shows how you'd use the class. Notice how in the foreach loop you're accessing properties of a class but behind the scenes the property getter is using a DataRow.
The methods "GetPosts1" and "GetPosts2" shows how you'd basically use a DataTable but "convert" it to an
IEnumerable<PostDtw>
. GetPosts1 essentially uses the same instance, while GetPosts2 creates a new instance for each row.
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data;
namespace ConsoleApplication5
{
class Program
{
static void Main(string[] args)
{
var posts = GetPosts1();
foreach (var post in posts)
{
Console.WriteLine(post.PostId);
Console.WriteLine(post.PostTitle);
Console.WriteLine(post.PostSlug);
Console.WriteLine(post.PostDate);
}
}
static IEnumerable<PostDtw> GetPosts1()
{
DataTable postsDt = GetPostsDataTable();
PostDtw postDtw = new PostDtw();
foreach(DataRow row in postsDt.Rows)
{
postDtw.DataRow = row;
yield return postDtw;
}
}
static IEnumerable<PostDtw> GetPosts2()
{
DataTable postsDt = GetPostsDataTable();
foreach (DataRow row in postsDt.Rows)
yield return new PostDtw(row);
}
static DataTable GetPostsDataTable()
{
throw new NotImplementedException();
}
}
/// <summary>
///This is the Base Class for all DataTable Wrappers
/// </summary>
public class BaseDataTableWrapper
{
public DataRow DataRow { get; set; }
public BaseDataTableWrapper()
{
}
public BaseDataTableWrapper(DataRow row)
: this()
{
DataRow = row;
}
}
#region [GetPost]
/// <summary>
///This class is a wrapper around a DataTable,
///Associated with the stored procedure - GetPost
///This class provides a strongly typed interface to access data from the DataTable
///containing the result of the given stored procedure.
/// </summary>
public sealed class PostDtw : BaseDataTableWrapper
{
public Int32 PostId { get { return (Int32)DataRow[0]; } set { DataRow[0] = value; } }
public DateTime PostDate { get { return (DateTime)DataRow[1]; } set { DataRow[1] = value; } }
public String PostSlug { get { return (String)DataRow[2]; } set { DataRow[2] = value; } }
public Int32 UserId { get { return (Int32)DataRow[3]; } set { DataRow[3] = value; } }
public String PostTitle { get { return (String)DataRow[4]; } set { DataRow[4] = value; } }
public String PostText { get { return (String)DataRow[5]; } set { DataRow[5] = value; } }
public Boolean PostIsPublished { get { return (Boolean)DataRow[6]; } set { DataRow[6] = value; } }
public Boolean PostIsPublic { get { return (Boolean)DataRow[7]; } set { DataRow[7] = value; } }
public String PostTitleImg { get { if (DataRow[8] != DBNull.Value) return (String)DataRow[8]; else return default(String); } set { DataRow[8] = value; } }
public PostDtw()
: base()
{
}
public PostDtw(DataRow row)
: base(row)
{
}
}
#endregion [GetPost]
}

Categories