Using a list method to populate a graph in C# - c#

I am trying to learn how to make graphs and populate them using a data list I cal from my class. I made a method to read all my Scale values then I call it to my form. After doing research I found one that looked promising so I modified it to work for me. Upon completion I received the following errors and after looking it up I'm still not sure why. Is there a better way of filling my chart with points using a list and or how can I salvage this one. Thank you in advance.
errors:
cannot convert from 'System.Windows.Forms.DataVisualization.Charting.DataPointCollection' to 'WindowsFormsApplication1.Class1'
The best overloaded method match for 'System.Collections.Generic.List.Add(WindowsFormsApplication1.Class1)' has some invalid arguments
Method I am using:
List<Class1> points = new List<Class1>();
for (int i = 0; i < 100; i++)
{
points.Add(new DataPoint(i, 1));
}
points.Clear();

Related

Change property of an object

im just started learning c# and i have this little thing.
i dont really know how to call it but here it goes:
there are like 5 textboxes, and i want to set the value of each textblock with a for loop.
they are called t_1, t_2, t_3 and so on.
for (int i=0; i<5; i++)`
{
("t_" + i).Text = i;`
}
this gives this error:
Error 1 'string' does not contain a definition for 'Text' and no
extension method 'Text' accepting a first argument of type 'string'
could be found (are you missing a using directive or an assembly
reference?)
while the textboxes do have .Text properties.. what am i missing?
(btw im using MS VSExpress 2012 for windowsphone)
thanks in advance :)
EDIT:
thank you all very much for all the effort! :D
you guys really helped me out, and im back on track learning! :D
I'm afraid that you can do that so directly. Create a list of your textboxes like
var list = new List<TextBox> { t_1, t_2, t_3, t_4, t_5 };
and then you will be able to do
for (int i=0; i<5; i++)
{
list[i].Text = i.ToString();
}
I also advise you to replace
for (int i=0; i<5; i++)
with
for (int i = 0; i < list.Count; i++)
so that you can just add new controls without having to change the loop.
EDIT:
Sudhakar answer is also a usable solution, but to clarify benefits of my, here are the facts:
using literals in code is a way to earn a nice bug ... imagine more devs working in the same solution and one of them changing a name of a component so that it better express its purpose
code readability - list[i].Text = i; versus reference boxing and Find("t_" + i,true)[0] ... a fairly significant difference in readability
The Textboxes are Properties of an overlying object, maybe a ASP.Net WebPage, XAML or WinForm.
To be able to itereate through them you have to look at that object first, and change its structure so that the Textboxes are in an enumerable list.
I would suggest a dictionary with string indexers, probably fits you most.
You can't call a variable like a value.
Use this:
for (int i=0; i<5; i++)
{
if (i==1) t_1.Text = i.tostring()
else if (i==2) t_2.Text = i.tostring()
... so on
}

Creating array of List storing class object?

May i ask if the following code i wrote is correct? Basically, i want to create an 2D array, and within each array, i want to have a list of vertices. So if tCount and pCount is 10, i would have 100 different lists storing vertices. The adding of the vertices will be done elsewhere after certain operations are done to determine which list the vertice should be added to.
List<Vertice>[,] lists = new List<Vertice>[tCount, pCount];
for (int i = 0; i < tCount; i++) {
for (int o = 0; o < pCount; o++) {
lists[i,o] = new List<Vertice>(); } }
Pardon me for posting such a simple question, because i have posted a similar qns in another forum and the replies i received kind of confused me. But thanks for reading!
Mitch Wheat- There was no error so far, but i am also not sure if the list is created properly..
Damith- I've seen that before but i do not really understand the code, so i was wondering if the code i typed was correct or not. I feel safer using a code that i understand, although i do understand the benefits of using a 1-liner.
Oberdan Nunes- I need it to be in a 2D array, as it is related to another 2D array of data which i have.

iText PdfTemplate duplication

I have created PdfTemplate with some contour. Now i want to duplicate it 4 times and place different shapes on each of them. What I am trying now is:
PdfTemplate[] oRTA = new PdfTemplate[4];
for(int i=0; i<4; i++)
{
oRTA[i] = (PdfTemplate)oTemplate.GetDuplicate(false);
}
Where oTemplate is my standard PdfTemplate object. Problem can be that I can't use overwritten GetDuplicate method, so i have to cast it (dunno why). I am trying this with simple points:
oRTA[iTemplateIndex].Circle(oCmp.iXLocation, oCmp.iYLocation, 0.1f);
oRTA[iTemplateIndex].Stroke();
In result I've got nothing printed. Attempt with:
oTemplate.Circle(oCmp.iXLocation, oCmp.iYLocation, 0.1f);
oTemplate.Stroke();
works.
Solution is not to duplicate your Template but to create it 4 times (4 different PdfTemplate objects) and then add what you need to every one of them. I know maybe it is now the best option but it work as it should. Solution given by Bruno Lowagie seems to be adequate too :)

NeuralNet in C# Single output converting to multiple output c# dummy

I'm currently learning neural nets and stumbled across different sources and different codes all good however i found one code which found interesting and want to adapt it for various things such as OCR and that stuff. I am relatively new to C sharp and i would like some help on what i can do to make this code to my use. Basically this code adopts one output and I want that i can add several output neurons. My main problem is that i managed to adapt it to work with multiple outputs however then i found it impossible for me to test the network i.e First im training giving the network with the inputs and expected outputs. Then i just give the network an input string . The problem is that the Class Pattern is only accepts 3 parameters. How can i tell it/ work so that i can train with 3 parameters but then test only giving the string which i need to identify after that generalization is done ?? This is parts of the code which i am using for training -- this code is not mine just that its clear i am just using it for testing..
So testing-- this is being called
Activate(new Pattern(values, _inputDims)));
and Pattern method is this one --
private double[] _inputs;
private double _output;
public Pattern(string value, int inputSize)
{
string[] line = value.Split(',');
if (line.Length - 1 != inputSize)
throw new Exception("Input does not match network configuration");
_inputs = new double[inputSize];
for (int i = 0; i < inputSize; i++)
{
_inputs[i] = double.Parse(line[i]);
}
_output = double.Parse(line[inputSize]);
}
so i want that the Pattern method accepts also outputSize however it must be able to do the testing when i just pass the value and input dimensions. Honestly i don't know what i can do I've already spent many time looking for all available options.
You can add an optional parameter to the pattern constructor.
Pattern ( string value, int inputSize, int outputSize = 1)

How do I retrieve the list from said pointer?

The MSDN document that I am trying to follow is located here. Basically I am trying to figure out in C# how to read that pointer into a list of the DHCP_OPTION_DATA structures.
I have the following code but I don't think that it is the proper way to do this.
DHCP_OPTION_ARRAY optionArray = (DHCP_OPTION_ARRAY)Marshal.PtrToStructure(options, typeof(DHCP_OPTION_ARRAY));
List<DHCP_OPTION> allOptions = new List<DHCP_OPTION>();
for (int i = 0; i < optionArray.NumElements; i++) {
DHCP_OPTION option = (DHCP_OPTION)Marshal.PtrToStructure(optionArray.Options, typeof(DHCP_OPTION));
allOptions.Add(option);
optionArray.Options = (IntPtr)((int)optionArray.Options + (int)Marshal.SizeOf(option));
}
Since I can't Marshal the pointer into a generic list collection I tried this way. My problem is that I am getting skewed results based on how much I increase the IntPtr to. Initially I was doing this.
optionArray.Options = (IntPtr)((int)optionArray.Options + (int)Marshal.SizeOf(typeof(DHCP_OPTION_DATA)));
However, I then realized that the next element would be located after the size of the actual option.
So the question still remains, how do I Marshal a Ptr to a list of structures?
EDIT 1
I posted the wrong article it is fixed now.
EDIT 2
Although both answers were great, I chose the answer to my problem because it addressed my lack of understanding of how the data was being handled on the back end of marshaling the information.
Is the first option object you get correct?
If so, the reason for the rest being skewed most likely is the alignment of the structure.
You could try to find the correct alignment, for example:
var offset = (int)Marshal.SizeOf(typeof(DHCP_OPTION_DATA));
var alignment = 4;
var remainder = offset % alignment;
if(remainder != 0)
offset += alignment - remainder;
optionArray.Options = (IntPtr)((int)optionArray.Options + offset);
Here is a paper Jason Rupard wrote using the DHCP_OPTION_ARRAY...
http://www.rupj.net/portfolio/docs/dws-writeup.pdf
Looks like he has everything you need and more... :)
Although looking at it you could define the structure a little differently and have it automatically turned into an array upon deserialization if you get the Pack attribute right.

Categories