Is it possible to save an oxyplot into a list? - c#

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.

Related

Creating a List of a class within the same class?

I'm practicing C# and have only been learning for about a month. I have a question that maybe is a bit on the beginner side so I hope someone doesn't mind answering for me.
I have a class called yourCharacter. In this class sits all the information for someone's character. I want to give the user the ability to create a new character so I've created a method/function to do so. My question is, can I place a function within yourCharacter that creates a List of yourCharacter. Is this doable? Is it bad form to do it this way? Should I be creating this list in my Main class and then calling a function within the main class to do this?
I hope my question is clear enough, please let me know if you need further detail. The only reason I want to do this is because in my head it makes more sense to group my methods/functions with the class it is manipulating and or working with.
class yourCharacter{
//insert a bunch of variables here
public static void newChar(){
List newCharacter = new List<yourCharacter>();
}
}
What you have there is doable for sure.
A class can contain itself as a member. This might make sense in some scenario, but think about yours and see whether it applies. In your case it seems as if you're trying to have a mechanism to group characters - but it's not a good way to do it. Why? Read on...
Say you had a box. This box contained another box inside... that box could contain another box inside...
So in that case, if I made a class which represents this kind of box it might make sense to add that class into itself.
Making a class like that represents a system in reality where a box contains other boxes.
What system are you trying to abstract, so that it is useful that a character contains other characters?
I can think of something: let's imagine a character in your game can swallow other characters and they'll live inside his tummy...
If you want to group them - you'd likely want to use another class which represents the system under which your characters are grouped like this:
Say your game has an adventure party! The adventure party can invite other people to join and they go questing together. Let's say this adventure party has a leader which decides who can join and who can't. You still probably wouldn't make a character which contains other characters... you would rather create a class called "AdventureGroup" with a list of "ordinary" party members and a leader, you would assign a string which represents the "group nickname", and all other kinds of things which represent the group as an entity. Something like that... you get the jist.

C# custom file parsing with 2 delimiters and different record types

I have a (not quite valid) CSV file that contains rows of multiple types. Any record could be one of about 6 different types and each type has a different number of properties. The first part of any row contains the timestamp and the type of record, followed by a standard CSV of the data.
Example
1456057920 PERSON, Ted Danson, 123 Fake Street, 555-123-3214, blah
1476195120 PLACE, Detroit, Michigan, 12345
1440581532 THING, Bucket, Has holes, Not a good bucket
And to make matters more complex, I need to be able to do different things with the records depending on certain criteria. So a PERSON type can be automatically inserted into a DB without user input, but a THING type would be displayed on screen for the user to review and approve before adding to DB and continuing the parse, etc.
Normally, I would use a library like CsvHelper to map the records to a type, but in this case since the types could be different, and the first part uses a space instead of comma, I dont know how to do that with a standard CSV library. So currently how I am doing it each loop is:
String split based off comma.
Split the first array item by the space.
Use a switch statement to determine the type and create the object.
Put that object into a List of type object.
Get confused as to where to go now because i now have a list of various types and will have to use yet another switch or if to determine the next parts.
I don't really know for sure if I will actually need that List but I have a feeling the user will want the ability to manually flip through records in the file.
By this point, this is starting to make for very long, confusing code, and my gut feeling tells me there has to be a cleaner way to do this. I thought maybe using Type.GetType(string) would help simplify the code some, but this seems like it might be terribly inefficient in a loop with 10k+ records and might make things even more confusing. I then thought maybe making some interfaces might help, but I'm not the greatest at using interfaces in this context and I seem to end up in about this same situation.
So what would be a more manageable way to parse this file? Are there any C# parsing libraries out there that would be able to handle something like this?
You can implement an IRecord interface that has a Timestamp property and a Process method (perhaps others as well).
Then, implement concrete types for each type of record.
Use a switch statement to determine the type and create and populate the correct concrete type.
Place each object in a List
After that you can do whatever you need. Some examples:
Loop through each item and call Process() to handle it.
Use linq .OfType<{concrete type}> to segment the list. (Warning with 10k
records, this would be slow since it would traverse the entire list for each concrete type.)
Use an overridden ToString method to give a single text representation of the IRecord
If using WPF, you can define a datatype template for each concrete type, bind an ItemsControl derivative to a collection of IRecords and your "detail" display (e.g. ListItem or separate ContentControl) will automagically display the item using the correct DataTemplate
Continuing in my comment - well that depends. What u described is actually pretty good for starters, u can of course expand it to a series of factories one for each object type - so that you move from explicit switch into searching for first factory that can parse a line. Might prove useful if u are looking to adding more object types in the future - you just add then another factory for new kind of object. Up to you if these objects should share a common interface. Interface is used generally to define a a behavior, so it doesn't seem so. Maybe you should rather just a Dictionary? You need to ask urself if you actually need strongly typed objects here? Maybe what you need is a simple class with ObjectType property and Dictionary of properties with some helper methods for easy typed properties access like GetBool, GetInt or generic Get?

How to create the ability to apply a generic data source class

This is maybe something I know how to do or have already done it in the past. For some reason I am drawing a blank on how to wrap my head around it. This is more for learning as well as trying to implement something in my app.
I am using a set of third party controls. These controls offer a lot of functionality which is great. However, I want to be able to create a custom object that handle the logic/properties for the datasource of this control.
For example, there is a spreadsheet like object that I am using. You supply the spreadsheet like object some data and it pulls in your data. The problem here is that you need to set the columns, their data types, and other formatting/events as well as some logic to spit the data back to the user.
List<CustomClassWithProperties> dataSource
The custom class has some properties that will be translated to the columns. Like ProductName, Price, SalesDepartment, DatePurchased etc. This can be done by supplying the spreadsheet the columns and their data types each time. I want to be able to create a helper class that you just supply a list, a visible column list, and an editable column list and the data will fill in without any other issues.
Using the above list, I would imagine something similar to this:
DataHelperClass dtHlpr = new DataHelperClass(List<CustomClassWithProperties> data, List<string> visibleColumns, List<string> editableColumns)
This data helper class will take the data input list as the spreadsheet data source. It would then take the visibleColumns list and use that to set the visible rows, same for editableColumns.
Where I am running into a mental block (long week) is when I want to be able to reuse this. Let's say I have a List that has completely different properties. I would want my constructor for the data helper to be able to handle any List I send to it. Looking at whatever code I can get to for the third party controls, it appears that their data source is of type object.
Could someone point me in the right direction? I am thinking it has to do with generics and some interface implementation. I just honestly cannot think of where to start.
You can make the class itself generic:
public class DataHelperClass<T>
{
public DataHelperClass(List<T> data, ...) { ... }
}
DataHelperClass<CustomClassWithProperties> dtHlpr = new DataHelperClass<CustomClassWithProperties>(List<CustomClassWithProperties> data, List<string> visibleColumns, List<string> editableColumns)
You'd then perform your reflection against typeof(T).
I'd also be tempted to use IEnumerable<T> rather than List<T> if possible, but that's a matter of preference, more or less.
This is similar to using a simple List<object>, except that it enforces that all objects in the list inherit from the same type (which might well be object), so you get some more type-checking than you otherwise would.
You mentioned interfaces, I don't see any reason here to include that (from what you've told us, at least), but you can certainly make a generic interface via the same syntax.

Templated Lists in C#?

I know templates are common within c/c++ however I am currently trying to translate an old VB code our company uses up to a c# equivalent which brings me to my problem....
VB is an type unsafe language so we come across things like
Public Elements As New Collection
so given the line above I need to translate that to a List.. Given that a Collection can be anything from a List to a map I need to template this as efficiently as possible. I was thinking of for first draft ignoring the map option entirely and just making it a List however from my understanding Templates don't truly exist within C#... The below code is what I came up with so far and while it compiles (I have not gotten to a testing point in the rest of the code yet) I don't know if it would actually work...
Public List<ValueType> Elements = new List<ValueType>();
can anyone offer input on if this would work as a generic list where type is determined at input or if I need to change this so the constructor would look more like
Public List<ValueType> Elements = new List<typeof(foo)>();
if the above is confusing I am sorry it is for me as well, and I will try and clarify as questions come in.
this question is no longer relevant, I was able to go into the source of the calling code and determine what variable types that the lists need to support.

Inventory Class

I have created an Inventory Class but I don't know how to retrieve information from it. I know how to go over it in a foreach loop and display all items, but I don't know how to see if YoYo is in there and if it is, print the Name, Cost and Quantity to a label. Can anybody please help?
I hope you're going beyond an Inventory class and creating a Product class as well. Object-oriented languages are all about encapsulation and information hiding. Embed the output of the Product in a method for that object. Clients can simply call it that way instead of having to repeat it all over the place.
I'd also recommend a find method in your Inventory class that lets you search for a Product instance. You might want to search by name now, but what about cost, category, or manufacturer? Perhaps you'll want to extend it later.
I agree with Marc's dictionary recommendation, but I would advise that you made that part of Inventory's private implementation. You're adding an extra bit of abstraction that makes an Inventory something more than a mere dictionary.
I think strings and data structures are wonderful, but too often these primitives leak into code when hiding them in an object would be much better for clients. That's what I think good object-oriented design is all about.
Since you mention foreach, it sounds like it implements IEnumerable<T>. In which case, if you are using C# 3.0 / LINQ, something like:
var yoyo = inventory.FirstOrDefault(item => item.Name == "YoYo");
if(yoyo != null) { // found it
Console.WriteLine(yoyo.Cost); // etc
}
You might also look at dictionaries if you have a lot of data...

Categories

Resources