Solved

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

Posted on 2013-05-26
7
465 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
Comment Utility
what do u mean?
can u explain the question?
0
 
LVL 23

Accepted Solution

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

Michael
0
 

Author Comment

by:ocaccy
Comment Utility
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
Better Security Awareness With Threat Intelligence

See how one of the leading financial services organizations uses Recorded Future as part of a holistic threat intelligence program to promote security awareness and proactively and efficiently identify threats.

 
LVL 23

Assisted Solution

by:Michael74
Michael74 earned 375 total points
Comment Utility
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
Comment Utility
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
Comment Utility
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
Comment Utility
Thank you.
0

Featured Post

Do You Know the 4 Main Threat Actor Types?

Do you know the main threat actor types? Most attackers fall into one of four categories, each with their own favored tactics, techniques, and procedures.

Join & Write a Comment

Suggested Solutions

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…
Entity Framework is a powerful tool to help you interact with the DataBase but still doesn't help much when we have a Stored Procedure that returns more than one resultset. The solution takes some of out-of-the-box thinking; read on!
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 …
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 …

762 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

11 Experts available now in Live!

Get 1:1 Help Now