Accessing a property value 'nested' inside other property/variable - c#

I am trying to access a property value deep inside another received variable. I don't know the terminolgy so apologies but cant describe it properly which the image below.
I am using a Library called CmdMessenger, to send/recevie data over serial port.
I receive data back from the serial port using the libray, no problem. But There is some data I need to extract which is 'nested' inside the variable as shown in the Locals screenshot below.
I receive the 'arugument' variable at the top, and can read its immediate 'Arguments' property fine, but I wish to extract the data from the other highlighted 'arguments' property at the bottom of the list, which currently has 'PBC' as its value. See screenshot:
public static void SendFirmwareVersion(ReceivedCommand arguments)
{
//read value ok from root of that property.
string[] x = arguments.Arguments;
//Then need to read value from other property deep inside that?
}
This is not soemthing i've had to do before, so quite frankly wouldnt know where to start, but it seems possible as there data is there?
PS: Feel free to edit the title that describes what I am trying to acheive.
Thanks.

Related

Is it possible to save an oxyplot into a list?

I'm trying to save some oxyplots into a list where I can then later generate a pdf or png file for printing to a printer. Is this something that is possible?
I have two classes; one is a plotUsercontrol and the other being a Baseplot.
Also, I did not implement the oxyplots feature of the app so I'm not fully aware of the differences between PlotModel and PlotView.
In my snippet PlotUserControl. I have the following function which I created to grab all the plots from the BasePlot. I declared it as void because I was not sure what type to declare the Data type for the list and function as.
public void getPlots()
{
foreach(var plot in plots)
{
Console.WriteLine(" ");
Console.WriteLine(plot.getImage());
}
}
Bellow is just a fucntion from my baseplot that gets the PlotModel. Also I wasn't sure whether to return the plotModel or the plotView.
public PlotModel getImage()
{
return this.plotModel;
}
I hope this make sense. New to c#.
Do you want to safe your images or the plots into a list? If you want to save plots into a list, that's already done (plots).
However if you want to save images into a list that's a bit different but very easy. I suggest you take a look at List<T>. I can supply you with method that will do all the work but it is so easy that i think you can manage to set up the list on your own.
Here is a quick rundown tho: List<T> will create a list which allows you to save objects of the datatype T inside of it. T is a so-called generic type which means you can hand it every data type that can be instantiated.
Hint: T would be PlotModel in your example.
If you need any more help, feel free to contact me but I think you can manage creating said list on your own.
This is the link to the Microsoft Docs of List<T>, but imo they are not the best docs for beginners. Just look for some tutorials on Google.
Another tip tho, rename your functions. In C# first letters of words in method names are uppercase (e.g: GetPlots(), GetImage()). This is just a common naming style although you can stick to lowercase.

Why can't Json.Decode handle arrays in C#?

Could someone help shed light on what is going on here?
If I do this in a C# program:
dynamic data = Json.Decode("{\"myObjects\": [ { \"id\": 1 }, { \"id\": 2 } ] }");
int id = data.myObjects[0].id;
I am able to access id and it is set to 1... everything is fine up until this point.
What I don't understand is why, when I'm debugging the program, I cannot view the contents of datain the Locals inspector. Instead it tells me this, as if it doesn't know how to process the array.
Error No further information on this object could be discovered
'data' is declared as dynamic type and thats why you cannot mouse over and see the value. You could still see the value if you add 'data' to Watch.
There seems to be a bug/limitation of Visual Studio (at least the 2013 version I use). If you put a watch on data, you can click on Dynamic View and see that it has stuff, but if you click on myObject (that is a DynamicJsonArray) you only get its "dynamic" part, and not its "fixed" properties (like the Length) and if you try to click on its Dynamic View you receive No further information on this object could be discovered... but if you create a watch for data.myObject, then you'll still have the unusable Dynamic View, but you can look at the "fixed" properties of the DynamicJsonArray (like the Length) and if you click on the Result View you can see the items of the array. See image:
The other answers did not work for me, but I found an alternative way to view the array contents in Visual Studio.
I have found that we can see the contents of the DynamicJsonArray in the Watch window (or IntelliSense), by looking inside the Non-Public members of the top level object. Inside that, we see the private _values member and inside that, we see the values. Upon opening the relevant value, we see the Key and Value members inside.
We can then proceed to open the Value node and then, the Results View, which will show the items in the array. We can then finally access the individual items in the collection

What to use instead ini in C#

There must be a program that should generate WORD document (but that's not the point). It generates a document by data that the user writes in the program. And sometimes there is need to close application and do some other work and user don't want to lose all progress. And here we must keep all the "changes" that user have entered. Previously, I have saved all .ini file (under the instruction http://www.codeproject.com/Articles/1966/An-INI-file-handling-class-using-C) it was enough for me for test, but in real cases ini was not enough, because if I save the textbox with multiline so record to inin file goes wrong, and when reading ini file - readed was only the first line and then only the first 255 characters.
Here is an example of what should be saved:
information about all checked checkboxes radiobuttons etc. (in
INI-expample I show it in CHECKOUTS and COMPARING sections)
the data from all textboxes on the main form (MAIN section)
most importantly, objects of 'implementation' and 'screens' class.
Now about class 'implementation': Each implementation may have a name, description, status, and a list of links to screenshots. To do this, I created a listbox with list of screenshots for each implementation (ScreenList) and a separate listbox for all implementations. Ie when I fill the data I store object in listbox, and then if I need I can just get access to it. And when I save a list of all implementations into ini I first of all write the number of all implementations to be able get access in loops (for, while etc) to each object (all in INI and CODE examples: Section IMPLEMENTATIONS, IMPLEMENTATION_n ...)
And the question is: how to save these Data? As I know Microsoft has abandoned ini and use the xml, but I can't google it correctly. Someone suggested I use serialization of data in xml, but as far as I could google - Serialization is used only for one object of class, and I have a lot of these objects are, othervide I still have and values ​​of all the checkboxes and more. Ie I need to save all the values ​​of controls , all the objects of classes implement and screen, and then read these values ​​and write them right back to where they were taken . How to do that?
Code examples:
//........save object to listbox
Implement imp = new Implement(impName.Text, impDescr.Text, impStatus.Text, ScreenList);
listBox1.Items.Add(imp);
//......
//in implement class, Screens is list of screenshots that is get from another listbox
private List<string> _Screens = new List<string>();
public Implement(string Name, string Description, string Status, ListBox Screen)
{
_Name = Name;
_Description = Description;
_Status = Status;
for(int i=0;i<Screen.Items.Count;i++)
{
_Screens.Add(Screen.Items[i].ToString());
}
}
//....getting access to implementation
Implement imp = (Implement)listBox1.SelectedItem;
....
Ini example:
[MAIN]
Languages=Polish
Comment=Comment lalarar larl alrlalrl
Status=Correct
[CHECKOUTS]
Enable=True
SLDoc=False
SLDocTab=True
SaveDoc=True
LoadDoc=False
SendDoc=False
Correctly=True
CorrNum=50
[COMPARING]
Enable=True
NoDif=False
Declar=True
UnDecl=False
UnDeclDESCR=
[IMPLEMENTATIONS]
COUNT=2
[IMPLEMENTATION_0]
Name=Implement 1 CORRECT
Descr=text text test text
Status=Correct
ScreenCount=2
Screen_0=C:\1.png
Screen_1=C:\2.png
[IMPLEMENTATION_1]
Name=IMPLEMENT 2 INCORRECT
Descr=lala
Status=Incorrect
ScreenCount=2
Screen_0=C:\2.png
Screen_1=C:\3.jpg
[SCREENS]
COUNT=2
[SCREEN_0]
Descr=Screen 1
Screen=C:\1.png
[SCREEN_1]
Descr=Screen 2
Screen=C:\1.png
If I understood you correctly you should take a look at settings mechanism in c#.
I hope that this chapter of MSDN will be useful.
What you have to do is create some kind of Settings object. This should be a class that either has a specific property (or class for nested values) for each setting you like (e.g. public string Language {get; set; }) or you take a more generic way by using two dictionaries Dictionary<string, string> (for simple values), Dictionary<string, Settings> (a recursive structure for complex types).
Then your form should be able to create such a settings object an fill it with the desired data it needs to recreate the current state and it should be able to take such a settings object and change its current state to this settings object by setting all inner variable to the values within this object.
The next step would be to make this object serializable by either using XML serialization or Data Contract or whatever you like. Then you can write/load this object to/from disk and afterwards push it into the class that needs this state.

Updating a property using the Content Service api in Umbraco 6.x

I've created a custom user control for the back-end of my Umbraco site that allows administrators to quickly update certain fields on nodes without having to navigate through the content tree.
So far my code is working as expected: I can update simple true/false properties without a problem. However now I'm trying to update a property that's of a custom data type and I'm running into difficulties.
The data type itself is just a simple drop down that lists a series of availability statuses ie. Available, Unavailable, Sold and Reserved. The datatype is storing the text values.
Here's the code I have that allows me to update my true/false properties:
public void ChangeInteractiveStatus(string nodeId, bool chkValue)
{
var cs = ApplicationContext.Current.Services.ContentService;
var apartment = cs.GetById(Convert.ToInt32(nodeId));
apartment.SetValue("displayOnInteractive", chkValue);
cs.SaveAndPublish(apartment);
}
This works absolutely fine as the data type of this property is a regular true/false data type.
Here's the code I'm using to change the value of my custom dropdownlist data type:
public void ChangeAvailabilityStatus(string nodeId, string status)
{
var cs = ApplicationContext.Current.Services.ContentService;
var apartment = cs.GetById(Convert.ToInt32(nodeId));
apartment.SetValue("status", status);
cs.SaveAndPublish(apartment);
}
As you can see there's very little difference and yet this code isn't working.
In order to check what was happening when I was updating the properties with the above code, I checked the umbraco.config file only to find that the property in question was displaying as follows:
<status><![CDATA[]]></status>
However when I change the value in the content tree (without using my admin control) the value gets saved properly as:
<status><![CDATA[Sold]]></status>
So for whatever reason, when I try to update the value it's being rejected and I can't work out why.
FYI I tried entering the value as:
"<![CDATA[" + status + "]]>"
Yet that made no difference.
Does anyone know how I can fix this? How can I get the property to update correctly?
Thanks
Okay I've figured out what the problem was. It seems the values were being stored as name-value pairs, so the actual value getting stored in the database was an integer. Once I updated the code to insert the integer id it all worked as expected! Hooray.

How to grab value of selected objects in ObjectListView

I have an objectlistview which displays filename and its path in a column. I would like to run a function on selected items. Is there any way I can grab the value of the filename and loop through every file that is selected on the objectlistview? The column aspect name is Filename.
My function is as follows: sampleFunction(string inputFile, string outputFile);
so far I've tried this, but couldn't work, I know I'm missing reference to the column itself but I don;t know how to add it:
for(var i=0; i<=objectListView1.SelectedObjects.Count; i++)
{
encClass.sampleFunction(objectListView1.SelectedObjects[i], "output here");
}
edit:
I also tried append ToString() method to objectListView1.SelectedObjects[i].ToString(). It shows no error but the function couldn't run perfectly because I have 3 columns and I only wanted to use the first column's value in the function as the inputFile value.
I seems that you do not understand the concept of the OLV correctly. I suggest you read the tutorial (again).
The OLV allows you to work with the underlying model objects directly, so just cast the selected object(s) to the original type and access its properties. You can even use a TypedObjectListView<>, which simplifies access to the models.
Judging from you post, what you want to do is probably something like this:
foreach (var selectedObject in objectListView1.SelectedObjects) {
encClass.sampleFunction(((MyType)selectedObject).Filename, "output here");
}
Obviously, replace "MyType" with your model object type.

Categories

Resources