Solved

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

Posted on 2013-05-26
7
473 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
DevOps Toolchain Recommendations

Read this Gartner Research Note and discover how your IT organization can automate and optimize DevOps processes using a toolchain architecture.

 
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

DevOps Toolchain Recommendations

Read this Gartner Research Note and discover how your IT organization can automate and optimize DevOps processes using a toolchain architecture.

Question has a verified solution.

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

This is about my first experience with programming Arduino.
Performance in games development is paramount: every microsecond counts to be able to do everything in less than 33ms (aiming at 16ms). C# foreach statement is one of the worst performance killers, and here I explain why.
In this fifth video of the Xpdf series, we discuss and demonstrate the PDFdetach utility, which is able to list and, more importantly, extract attachments that are embedded in PDF files. It does this via a command line interface, making it suitable …

832 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