Question Assign value

kattungen

Member
Joined
Sep 8, 2019
Messages
8
Programming Experience
Beginner
Hello!

I am using A graphing tool for c# called Live Charts and would like to assign values to the y-axis based on an array I already have. So It would basically look like this:

C#:
  new LineSeries
                {
                    Title = "Species 1",
                    Values = new ChartValues<int>{1,2,3,4}
                }
Where Values should be assigned to an array called Info.

Also how do I make the x-axis be dependent to a variable. So that if I want it to be 100 units long I wont have to do it by hand?

C#:
 cartesianChart1.AxisX.Add(new Axis
            {
                Title = "Day",
Labels = new[] { "0","1", "2", "3", "4", "5", "6", "7", "8", "9", "10" }
            });

Thanks in advance!
 
The following:
C#:
var series = new LineSeries
{
    Title = "Species 1",
    Values = new ChartValues<int>{1,2,3,4}
}
is called Object Initialization and Collection Initialization.

It is just syntactic sugar that is equivalent to:
C#:
var values = new ChartValues();
values.Add(1);
values.Add(2);
values.Add(3);
values.Add(4);

var series = new LineSeries();
series.Title = "Species 1";
series.Values = value;

Just apply your C# knowledge and about how loops and arrays work and you could simply call [il]Add()[/il] multiple times.

The documentation for Live Charts seems to be a bit lacking in some areas, but from the source code looks like ChartValues has a constructor that takes an IEnumerable[/il][/URL]. Since arrays, are [icode]IEnumerable, you can simply pass it in: new ChartValues<int>(myIntArray).
 
Doesn't work :/


C#:
            new LineSeries
                {
                    Title = "Species 1",
                    var values = new ChartValues(); values.Add(1); values.Add(2); values.Add(3); values.Add(4); var series = new LineSeries(); series.Title = "Species 1"; series.Values = value;
                }
               
            
            };
 
You have to break it out as multiple lines like I had above. Object initializers only support assignments, not statements.
 
That was just bad typing on my part in the post. The real code looks like this:

C#:
 new LineSeries
{
Title = "Species 1",
var values = new ChartValues();
values.Add(1);
values.Add(2);
values.Add(3);
            values.Add(4);
            var series = new LineSeries();
series.Title = "Species 1";
series.Values = value;
        }
[code]
And generates a lot of errors as well. Such as: 

Error CS1003 Syntax error, ','

and

Error CS0116 A namespace cannot directly contain members such as fields or methods
 
Try:
C#:
var values = new ChartValues<int>();
values.Add(1);
values.Add(2);
values.Add(3);
values.Add(4);

new LineSeries
{
    Title = "Species 1",
    Values = values
};

OR

C#:
var values = new ChartValues();
values.Add(1);
values.Add(2);
values.Add(3);
values.Add(4);

var series = new LineSeries();
series.Title = "Species 1";
series.Values = values;
 
None of the above works

C#:
cartesianChart1.Series = new SeriesCollection
            {

var values = new ChartValues();
values.Add(1);
values.Add(2);
values.Add(3);
            values.Add(4);
            var series = new LineSeries();
series.Title = "Species 1";
            series.Values = values;

It generates many errors. One of them is: Using the generic type 'ChartValues<T>' required 1 type arguments.
 
You cannot put multiple statements inside object initializers. So lines 4-11 need to go before line 1.

Out of curiosity, what book or tutorial are you using to learn C#? It seems like you just jumped straight into the language without going through the learning curve of learning parts of the language, and how to read and interpret error messages.
 
That's because you haven't a clue what you're doing. Before you try moving forward, I recommend you read some of Jon Skeet's books : C# In Depth C# in Depth: 9781933988368: Computer Science Books @ Amazon.com - you can find a few other editions here : Jon Skeet

It is one of my biggest pet-hates to see someone skipping fundamental chapters, and then expecting anyone to help them. If anyone helps you from here, they will not actually be helping you, but carrying you. And while I am not trying to discourage support on the forums, as I would never do that on learning platforms such as these. But in my opinion, It is rather pointless for anyone to help you at this point without you firstly fully understanding some of the basic utilisation concepts of the language.
 
Alright but I still get an error message: Using the generic type 'ChartValues<T>' required 1 type arguments.
C#:
 var values = new ChartValues();
values.Add(1);
values.Add(2);
values.Add(3);
values.Add(4);
var series = new LineSeries();
series.Title = "Species 1";
series.Values = values;
cartesianChart1.Series = new SeriesCollection
            {
               


            };

I just want to get this working, please.
 
Try passing in the type int as the generic type parameter.

What is baffling to me is that the error message is telling you exactly what is wrong. Don't you read the error message and try to research what it means and how to fix it?
 
Help yourself by learning the basics. I've given you some good books to read up there. What use is it if we do it for you? You learn nothing.

And frankly if you can't understand what you're being shown above or what the error message is telling you, that's your fault for skipping the basics, in which case I will show know remorse or sympathy.

An argument is expected to be passed between ChartValues / ( ), so next ask yourself what is an object type, and what type<T> of parameters could it be looking for?

Logically look and think : You have a class called ChartValues which that class requires a type<T> which is meant to be a collection of that type. So where are you specifying the type in new ChartValues(); ?
 
Alright the code runs but the output is not what I was expecting.

C#:
   var values = new ChartValues<int>();
values.Add(1);
values.Add(2);
values.Add(3);
values.Add(4);
var series = new LineSeries();
series.Title = "Species 1";
series.Values = values;
cartesianChart1.Series = new SeriesCollection
            {

new LineSeries
                {
                }
            };
It doesnt add the points and the axis has very small numbers instead of integers.
And also the function should be called "Species 1".
 
That's because you did all that work on lines 1-8 to create and initialize a LineSeries, but decided to ignore it. You create yet another empty LineSeries on lines12-14.

Please take time to learn the language.
 
The compiler prefers when you do more on one line than having multiple lines essentially performing the same action for each line.
Switch:
   var values = new ChartValues<int>();
values.Add(1);
values.Add(2);
values.Add(3);
values.Add(4);
For This:
ChartValues<int> values = new ChartValues<int>() { 1, 2, 3, 4 };
When you create your LineSeries series; use the one you created. You clearly don't understand the use of the new keyword operator. Why aren't you using the LineSeries on line 6 instead of trying to instantiate a new LineSeries where you failed to declare the object type on line 12?

I don't mean to be patronising, you you really need to step back and pick up a book on C# basic programming 101.
 
Back
Top Bottom