C# Linking element of List - c#

I'm thinking of a way to create a link or reference to a List of Strings. My situation is that I'm creating ARP table and I need to save IP (as String) of my interface that captured response msg. Interface's IP address is saved in List<String>.
ARP_Table_entry(System.Net.IPAddress host_ip_addr, System.Net.NetworkInformation.PhysicalAddress host_mac_addr, int position)
{
this.host_ip_addr = host_ip_addr;
this.host_mac_addr = host_mac_addr;
this.time = System.DateTime.Now.Ticks / TimeSpan.TicksPerMillisecond;
this.local_inter = ??;
}
What I don't want to do, is to assign local_inter smt like list.ElementAt(0), because when I change IP address on the interface (List gets updated w/ new one), value in entry won't change - and I would have to do foreach for every entry (not bad, but...)
Instead I'm looking for solution, that will "link" that specific List-element to local_inter parameter - so changing IP in List will result in automatic update in every entry that contained old one.

If you can control code for ARP_Table_entry just make local_inter property that returns value from that mysterious list:
class ARP_Table_entry
{
List<string> mysteriousList;
int pos;
public ARP_Table_entry(List<string> mysteriousList, int pos,...)
{
this.mysteriousList = mysteriousList;
this.pos = pos;
...
}
// TODO: add null check/position verification as needed
string local_inter => mysteriousList[pos];
// or {get { return mysteriousList[pos];} for C# 5 and below
...
You can also use Func<string> as type or local_inter if you want to use fields for some reason:
class ARP_Table_entry
{
public Func<string> local_inter;
...
public ARP_Table_entry(List<string> mysteriousList, int pos,...)
{
local_inter = () => mysteriousList[pos];
...
}
Note that either approach will not protect you from replacing list altogether with originalMysteriousList = new List<string>().
Another option is to have more complex type to store list of IPs that will notify about its changes (similar to ObesrvableCollection) and update fields on change in the collection.

Related

How to change items in cache

Hello i want to change and alter values inside the cache of my acumatica cache i would like to know how to do it
for example i want to change the Ext. Cost value pro grammatically of the first line or the second line or can i check if there is already a "Data Backup" on transaction Descr.
public delegate void PersistDelegate();
[PXOverride]
public void Persist(PersistDelegate baseMethod)
{
if (Globalvar.GlobalBoolean == true)
{
PXCache cache = Base.Transactions.Cache;
APTran red = new APTran();
red.BranchID = Base.Transactions.Current.BranchID;
red.InventoryID = 10045;
var curyl = Convert.ToDecimal(Globalvar.Globalred);
red.CuryLineAmt = curyl * -1;
cache.Insert(red);
}
else
{
}
baseMethod();
}
this code add a new line on persist but if it save again it add the same line agaub u wabt ti check if there is already a inventoryID =10045; in the cache
thank you for your help
You can access your cache instance by using a view name or cache type. Ex: (Where 'Base' is the graph instance)
Base.Transactions.Cache
or
Base.Caches<APTran>().Cache
Using the cache instance you can loop the cached values using Cached, Inserted, Updated, or Deleted depending on which type of record you are looking for. You can also use GetStatus() on an object to find out if its inserted, updated, etc. Alternatively calling PXSelect will find the results in cache (PXSelectReadOnly will not).
So you could loop your results like so:
foreach (MyDac row in Base.Caches<MyDac>().Cache.Cached)
{
// logic
}
If you know the key values of the cache object you are looking for you can use Locate to find by key fields:
var row = (MyDac)Base.Transactions.Cache.Locate(new MyDac
{
MyKey1 = "",
MyKey2 = ""
// etc... must include each key field
});
As Mentioned before you can also just use a PXSelect statement to get the values.
Once you have the row to update the values you set the object properties and then call your cache Update(row) before the base persist and you are good to go. Similar if needing to Insert(row) or Delete(row).
So in your case you might end up with something like this in your persist:
foreach (APTran row in Base.Transactions.Cache.Cached)
{
if (Globalvar.GlobalBoolean != true || row.TranDesc == null || !row.TranDesc.Contains("Data Backup"))
{
continue;
}
//Found my row
var curyl = Convert.ToDecimal(Globalvar.Globalred);
row.CuryLineAmt = curyl * -1;
Base.Transactions.Update(row);
}

AspectGetter to get object from list

I have a list of items that I would like to bind to my ObjectListView and I think AspectGetter needs to be used to achieve this. How would I go about doing this?
I have tried this to generate additional columns but I am still unable to bind the data to show list items
int count = 0;
foreach (var disk in vmObject.DisksList)
{
// create column with vhd+count
OLVColumn diskColumn = new OLVColumn("Attached VHD " + count, disk.Path);
// this lets you handle the model object directly
diskColumn.AspectGetter = delegate(object rowObject)
{
// check if that is the expected model type
if (rowObject is Model.HyperVTools.VMInfo)
{
// return the value of disklist
return ((Model.HyperVTools.VMInfo)rowObject).DisksList;
}
else
{
return "";
}
};
columnsList.Add(diskColumn);
count++;
}
objectListView2.Columns.AddRange(columnsList.Cast<System.Windows.Forms.ColumnHeader>().ToArray());
objectListView2.AddObject(vmObject);
The foreach loop is unnecessary. You only need to create a column once. The same goes for the AspectGetter.
As soon as you add objects to the OLV, it will call the AspectGetter delegates to get the values automatically and create the corresponding rows. I suggest you take another look at the tutorial / examples.

Arrays/Array Lists

I am fairly new to C#
I am trying to retrieve some information from an external data source and store it in array, once it is in an array I wish to sort it by time.
I know how to do this for just one column in a row, however the information I require has multiple columns.
For example:
foreach (Appointment Appoint in fapts)
{
// Store Appoint.Subject, Appoint.Start, Appoint.Organiser.Name.ToString(), Appoint.Location in an array
}
// Sort my array by Appoint.Start
foreach ( item in myNewArray )
{
//print out Appoint.Subject - Appoint.Start, Appoint.Organiser.Name.ToString() and Appoint.location
}
Many thanks for your help.
EDIT:
I have multiple data sources which pull in this:
foreach (Appointment Appoint in fapts)
{
// Store Appoint.Subject, Appoint.Start, Appoint.Organiser.Name.ToString(), Appoint.Location in an array
}
Hence the need to sort the items in a new array, I know this isn't very efficent but there is no way of getting the information I need in any other way.
You can sort a list using the LINQ sorting operators OrderBy and ThenBy, as shown below.
using System.Linq;
and then...
var appointments = new List<Appointment>();
var sortedAppointments = list.OrderBy(l => l.Subject).ThenBy(l => l.Name).ToList();
This will create a new list of appointments, sorted by subject and then by name.
It's unclear what your final aim is but:
Use a generic List instead of an array:
See this SO question for more information as to why using a List is prefered.
List<Appointment> appointments = new List<Appointment>();
foreach (Appointment Appoint in fapts)
{
appointments.Add(Appoint);
}
foreach (var item in appointments)
{
Console.WriteLine(item.Subject);
Console.WriteLine(item.Foo);
// Here you could override ToString() on Appointment to print eveything in one Console.WriteLine
}
If the aim of your code is to order by time, try the following:
var sortedAppointments = fapts.OrderBy(a => a.Start); // assuming Start is a DateTime property of `Appointment`.
Consider a Dictionary Object instead of an array if the data is conceptually one row multiple columns.
foreach(KeyValuePair<string, string> entry in MyDic)
{
// do something with entry.Value or entry.Key
}
You already have a list of objects in fpts, sort that list itself:
fpts.OrderBy(x => x.Subject).ThenBy(x => x.Location).ToList();
LINQ is your friend here.
fapts appears to already be a collection so you could just operate on it.
var myNewArray = fapts.OrderBy(Appoint => Appoint.Start).ToArray()
I've used the ToArray() call to force immediate evaluation and means that myNewArray is already sorted so that if you use it more than once you don't have to re-evaluate the sort.
Alternatively if you are only using this once you can just as easily miss the ToArray() portion out and then execution of the sort will be deferred until you try and enumerate through myNewArray.
This solution puts the source objects into the array, but if you are just wanting to store the specific fields you mention then you will need to use a select. You have two choices for the array item type, you can either use an anonymous class which provides difficulties if you are returning this array from a function or define a class.
For anonymous:
var myNewArray = fapts.OrderBy(Appoint => Appoint.Start)
.Select(Appoint => new {
Start = Appoint.Start,
Organiser = Appoint.Organiser.Name.ToString(),
Location = Appoint.Location
}).ToArray();
For named class assuming class is MyClass:
var myNewArray = fapts.OrderBy(Appoint => Appoint.Start)
.Select(Appoint => new MyClass {
Start = Appoint.Start,
Organiser = Appoint.Organiser.Name.ToString(),
Location = Appoint.Location
}).ToArray();
You have a wide range of options. The 2 most common are:
1) Create a class, then define an array or list of that class, and populate that
2) Create a structure that matches the data format and create an array or list of that
Of course, you could put the data into an XML format or dataset, but that's probably more work than you need.
public List<foo> appointments = new List<foo>();
public struct foo
{
public string subject ;
public DateTime start ;
public string name ;
public string location ;
}
public void foo1()
{
// parse the file
while (!File.eof())
{
// Read the next line...
var myRecord = new foo() ;
myRecord.subject = data.subject ;
myRecord.start = data.Start ;
myRecord.name = data.Name ;
//...
appointments.Add(myRecord);
}
}
Enjoy
(Since I can't comment and reply to the comment - it wasn't clear if he had a class, etc. or was just showing us what he wanted to do. I assumed it was just for demonstration purposes since there wasn't any info as to how the data was being read. If he could already put it into a class, than the first answer applied anyway. I just tossed the last 2 in there because they were options for getting the data first.)

How to check for the duplicates in an ObservableCollection?

I have a collection of Addresses, which has different address items. Each address item has AddressType, City, Zipcode and other columns. I am writing a validation that if some one is adding a new addressType and the collection of Addressses has already an AddressType listed then give a warning that it has already been listed. How can I do that. I have attached some code. Right now its only checking it for the "Job Address". I have three types of Addresss.
if (Addresses.Any
(a => a.AddressType =="Job Address"))
{
DialogManager.ShowMessageBox(("The type has already been listed "),
MessageBoxButton.OKCancel);
}
If you are checking this after the fact, you can use the size of a HashSet<string>:
var types = new HashSet<string>(Addresses.Select(aa => aa.AddressType));
if (types.Count < Addresses.Count)
{
// You have a duplicate...
// ...not necessarily easy to know WHO is the duplicate
}
The above works by assigning each of the AddressType instances to a set. A set is a collection which only contains the unique items added. Therefore, if you have duplicates in your input sequence, the set will contain less items than your input sequence. You can illustrate this behavior like so:
// And an ISet<T> of existing items
var types = new HashSet<string>();
foreach (string typeToAdd in Addresses.Select(aa => aa.AddressType))
{
// you can test if typeToAdd is really a new item
// through the return value of ISet<T>.Add:
if (!types.Add(typeToAdd))
{
// ISet<T>.Add returned false, typeToAdd already exists
}
}
A better way would be beforehand, perhaps through CanExecute of a command if you have it implemented in a similar fashion:
this.AddCommand = new DelegateCommand<Address>(
aa => this.Addresses.Add(aa),
aa => !this.Addresses.Any(xx => xx.AddressType == aa.AddressType));
Create a new AddressComparer class implementing IEqualityComparer interface
Now you use Contains method
if(Addresses.Contains(Address,new AddressComparer()))
{
//your code
}

Gantt Chart Predecessors using JSGrid control

I am trying to create a Gantt Chart generator, using the Share point control:
<Sharepoint:JsGrid>
I followed this tutorial: How to: Create a Gantt Chart Using the JS Grid Control
I also linked my Sharepoint TaskList as the data Source.
I developped a system of filters using some XML.
But I now want to manage predecessors and represent dependencies by an arrow.
To manage them, I used the last parameter of the EnableGantt function (ganttDependentsColumnName), which one just need the name of the column which contains the dependency information.
What I have to put in this column ?
What I tried is to fill it with the ID of the task, the lane of the DataRow containing predecessors, and I tried to put an object of the class Dependency :
class Dependency : IJsonSerializable
{
public object Key {get; set;} // RecordKey
public LinkType{get; set;} //LinkType
public string ToJson(Serializer s)
{
return JsonUtility.SerializeToJsonFromProperties(s,this);
}
}
(This code is from the answers in the tutorial)
In the Key, what do I have to put? If someone did it or know how to do it, It could be nice.
Not sure if you are still facing this issue. But this is what we did for the Predecessors column and this is as far as I understand this:
1] Change the Dependency class a bit to add a constructor as shown (otherwise it errors out).
2] Then, you basically need to pass a Dependency array to the Predecessors column which means that the JSGrid control needs to know the starting point/row and the ending point/row of the dependency (thus an array is required). JSON Serialization is taken care of already because of the inheritance and the ToJson methods so no worries there.
Code:
1] Dependency Class:
public class Dependency : IJsonSerializable
{
public object Key { get; set; } // recordKey
public LinkType Type { get; set; } // SP.JsGrid.LinkType
public Dependency() {
Key = DBNull.Value;
Type = LinkType.FinishStart;
}
public string ToJson(Serializer s)
{
return JsonUtility.SerializeToJsonFromProperties(s, this);
}
}
2] Add column if not targeting a SharePoint Task List (where data is an object of DataTable):
data.Columns.Add(new DataColumn("Predecessors",typeof(Dependency[])));
3] Set the right object array to the Predecessors column:
if (<<A predecessor exists>>){
Dependency[] dep = new Dependency[2];
dep[0] = new Dependency();
try{
/*
// Unique Identifier for your row based on what you are
// passing to the GridSerializer while initializing it
// (as a third parameter which is called keyColumnName)
// In my case I had to get it by doing some coding as
// shown. The first object in the array represents the
// previous row and so the unique identifier should
// point to the previous row
*/
dep[0].Key = (
data.Select(
"ID=" +
data.Rows[s]["PredecessorsID"].ToString()
)
[0]["Key"]
);
}catch (Exception ex){
dep[0].Key = DBNull.Value;
}
dep[0].Type = LinkType.FinishStart;
/*
// Unique Identifier for your row based on what you are
// passing to the GridSerializer while initializing it
// (as a third parameter which is called keyColumnName)
// In my case I had to get it by doing some coding as
// shown. The second object in the array represents the
// current row and so the unique identifier should
// point to the current row
*/
dep[1] = new Dependency();
try{
dep[1].Key = data.Rows[s]["Key"];
}catch (Exception ex){
dep[0].Key = DBNull.Value;
}
dep[1].Type = LinkType.StartFinish;
data.Rows[s]["Predecessors"] = dep;
}
Finally, pass the Predecessors column while calling the EnableGantt() function:
gds.EnableGantt(
Convert.ToDateTime(
dr["start Date"]
),
Convert.ToDateTime(
dr["Due Date"]
),
GanttUtilities.GetStyleInfo(),
"Predecessors"
);
Make sure that your StartFinish and FinishStart link types matches the correct rows and that your tasks are listed correctly with correct task start dates and task end dates and predecessor keys.

Categories