Solved

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

Posted on 2013-05-26
7
469 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
3 Use Cases for Connected Systems

Our Dev teams are like yours. They’re continually cranking out code for new features/bugs fixes, testing, deploying, testing some more, responding to production monitoring events and more. It’s complex. So, we thought you’d like to see what’s working for us.

 
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

3 Use Cases for Connected Systems

Our Dev teams are like yours. They’re continually cranking out code for new features/bugs fixes, testing, deploying, testing some more, responding to production monitoring events and more. It’s complex. So, we thought you’d like to see what’s working for us.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Icons and Colors for Terms 3 24
Get id from json Data with NewtonSoft 3 34
Not showing page correctly 3 30
Handle KeyDown for ToolStripDropDownButton, ToolStripMenuItem 3 22
A long time ago (May 2011), I have written an article showing you how to create a DLL using Visual Studio 2005 to be hosted in SQL Server 2005. That was valid at that time and it is still valid if you are still using these versions. You can still re…
Whether you've completed a degree in computer sciences or you're a self-taught programmer, writing your first lines of code in the real world is always a challenge. Here are some of the most common pitfalls for new programmers.
With the power of JIRA, there's an unlimited number of ways you can customize it, use it and benefit from it. With that in mind, there's bound to be things that I wasn't able to cover in this course. With this summary we'll look at some places to go…
In this seventh video of the Xpdf series, we discuss and demonstrate the PDFfonts utility, which lists all the fonts used in a PDF file. It does this via a command line interface, making it suitable for use in programs, scripts, batch files — any pl…

920 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

Need Help in Real-Time?

Connect with top rated Experts

17 Experts available now in Live!

Get 1:1 Help Now