Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people, just like you, are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
Solved

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

Posted on 2013-05-26
7
474 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
MIM Survival Guide for Service Desk Managers

Major incidents can send mastered service desk processes into disorder. Systems and tools produce the data needed to resolve these incidents, but your challenge is getting that information to the right people fast. Check out the Survival Guide and begin bringing order to chaos.

 
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: SSL Checker

Scans your site and returns information about your SSL implementation and certificate. Helpful for debugging and validating your SSL configuration.

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

Whether you’re a college noob or a soon-to-be pro, these tips are sure to help you in your journey to becoming a programming ninja and stand out from the crowd.
Computer science students often experience many of the same frustrations when going through their engineering courses. This article presents seven tips I found useful when completing a bachelors and masters degree in computing which I believe may he…
Viewers will learn how to properly install Eclipse with the necessary JDK, and will take a look at an introductory Java program. Download Eclipse installation zip file: Extract files from zip file: Download and install JDK 8: Open Eclipse and …

856 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