Solved

C# - Using var in Chart Control with values. Concatenates string with integers in var.

Posted on 2013-05-26
7
475 Views
Last Modified: 2013-05-30
Hi everyone.
How to use var for the model below?
Thanks in advance, ocaccy
var ch1=this.chart1.ChartAreas[0];
	
	switch(x)
	{
	case 1:
		var chInt=7200D;
		ch1.AxisX.LabelStyle.Interval=chInt;
		ch1.AxisX.MajorGrid.Interval=chInt;
		ch1.AxisX.MajorTickMark.Interval=chInt;
		break;

	case 2:
		var chInt=14400D;	
		ch1.AxisX.LabelStyle.Interval=chInt;
		ch1.AxisX.MajorGrid.Interval=chInt;
		ch1.AxisX.MajorTickMark.Interval=chInt;
		break;

	case 3:
		var chInt=21600D;	
		ch1.AxisX.LabelStyle.Interval=chInt;
		ch1.AxisX.MajorGrid.Interval=chInt;
		ch1.AxisX.MajorTickMark.Interval=chInt;
		break;
	}

	
	var ch1=this.chart1.ChartAreas[0];
	int i_ch1Y=7200;

	switch(x)
	{
	case 1:
		i_chInt=x*i_ch1Y;
		var chInt=i_chInt+"D"; 
		// This is my idea, but I don't know how to concatenate
		// int value to String D to turn into 7200D.
		ch1.AxisX.LabelStyle.Interval=chInt;
		ch1.AxisX.MajorGrid.Interval=chInt;
		ch1.AxisX.MajorTickMark.Interval=chInt;
		break;

	case 2:
		i_chInt=x*i_ch1Y;
		var chInt=i_chInt+"D"; 
		ch1.AxisX.LabelStyle.Interval=chInt;
		ch1.AxisX.MajorGrid.Interval=chInt;
		ch1.AxisX.MajorTickMark.Interval=chInt;
		break;

	case 3:
		i_chInt=x*i_ch1Y;
		var chInt=i_chInt+"D";
		ch1.AxisX.LabelStyle.Interval=chInt;
		ch1.AxisX.MajorGrid.Interval=chInt;
		ch1.AxisX.MajorTickMark.Interval=chInt;
		break;
	}

Open in new window

0
Comment
Question by:ocaccy
  • 3
  • 3
7 Comments
 
LVL 42

Assisted Solution

by:sedgwick
sedgwick earned 125 total points
ID: 39198570
what do u mean?
can u explain the question?
0
 
LVL 23

Accepted Solution

by:
Michael74 earned 375 total points
ID: 39198601
One suggestion is not to use the var keyword use string for chInt and ChartArea for ch1

Michael
0
 

Author Comment

by:ocaccy
ID: 39198602
ok.

int aa;
int xx=2;
int yy=7200;
aa=xx*yy;
var chInt=aa+"D";  // How to concatenate this ( aa+"D" ) to turn it ( 14400D ) ?

sample:
ch1.AxisX.LabelStyle.Interval=chInt; // chInt==14400D;
0
Space-Age Communications Transitions to DevOps

ViaSat, a global provider of satellite and wireless communications, securely connects businesses, governments, and organizations to the Internet. Learn how ViaSat’s Network Solutions Engineer, drove the transition from a traditional network support to a DevOps-centric model.

 
LVL 23

Assisted Solution

by:Michael74
Michael74 earned 375 total points
ID: 39198620
No need to do anything special

string chInt = aa+ "D";

Open in new window


should work.

If you are getting errors you could use

string chInt = aa.ToString() + "D";

Open in new window


Michael
0
 

Author Comment

by:ocaccy
ID: 39198643
Does not work, because it is expected to double.
chart1.ChartAreas[0].AxisX.LabelStyle.Interval=7200D;
chart1.ChartAreas[0].AxisX.MajorGrid.Interval=7200D;
chart1.ChartAreas[0].AxisX.MajorTickMark.Interval=7200D;

If use this:

var chInt = 7200D;
chart1.ChartAreas[0].AxisX.MajorTickMark.Interval=chInt;

works.
0
 
LVL 23

Assisted Solution

by:Michael74
Michael74 earned 375 total points
ID: 39209749
Sorry for delay, busy week.

The interval value is a double. When you assign 7200D to this value the suffix "D" merely tells the compiler that 7200 is a double value.

If you replace
var chInt = 7200D;

Open in new window

with
Double chInt = 7200;

Open in new window

you would get the same result. You only needed the "D" in the first example because when using the var keyword the compiler would set chInt as an integer value without it.

The problem you encountered in your original example is that when you add a string value to a numeric value like
var chInt=i_chInt+"D";

Open in new window

the result of the addition is a string.

This issue can be resolved in a number of ways
1/ Declare chInt as a double and leave off the "D" ie
Double chInt = i_chInt

Open in new window

2/ Cast the result to a double eg
var chInt=Convert.ToDouble(i_chInt+"D");

Open in new window

I personally recommend not using the var keyword unless it is required. If you had used
Double chInt=i_chInt+"D";

Open in new window

You would have gotten meaningful error. Plus your code would be easier to read as it is clear what chInt is being declared as

Michael
0
 

Author Closing Comment

by:ocaccy
ID: 39209918
Thank you.
0

Featured Post

Free Tool: Port Scanner

Check which ports are open to the outside world. Helps make sure that your firewall rules are working as intended.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
need to convert function to c# 5 40
batch file or script 4 47
itextsharp with c# 3 18
Crashing when deleting value with no child in Binary Tree 4 24
Calculating holidays and working days is a function that is often needed yet it is not one found within the Framework. This article presents one approach to building a working-day calculator for use in .NET.
In this post we will learn different types of Android Layout and some basics of an Android App.
An introduction to basic programming syntax in Java by creating a simple program. Viewers can follow the tutorial as they create their first class in Java. Definitions and explanations about each element are given to help prepare viewers for future …

685 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question