'Random' does not contain a definition for 'Next'

S

ssims

I've got this code for a random GDI+ chart generator, and the demo
works, but when I try to run it on my local machine I get a compiler
error: Compiler Error Message: CS0117: 'Random' does not contain a
definition for 'Next'

Here's the code:
using System;
using System.Collections;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Drawing.Drawing2D;
using System.Drawing.Imaging;
using System.Drawing.Text;
using System.Web;
using System.Web.SessionState;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.HtmlControls;

public partial class Random : System.Web.UI.Page
{
Graphics gfx;
Bitmap bmp;
protected void Page_Load(object sender, EventArgs e)
{
bmp = new Bitmap(500, 320);
gfx = Graphics.FromImage(bmp);
gfx.Clear(Color.White);
gfx.SmoothingMode = SmoothingMode.AntiAlias;
// Define Points
//int[] p = new int[] { 10, 12, 48, 102, 290, 15, 100, 25, 300,
200, 150, 200, 60, 40, 250 };
int[] p = generateRandomValues();
DrawChart(p, maxValue(p), minValue(p));
}
private int maxValue(int[] value)
{
int max = Int32.MinValue;
for (int i = 0; i < value.Length; i++)
{
if (value > max)
{
max = value;
}
}
return max;
}
private int minValue(int[] value)
{
int min = Int32.MaxValue;
for (int i = 0; i < value.Length; i++)
{
if (value < min)
{
min = value;
}
}
return min;
}
private void DrawChart(int[] points, int maxValue, int minValue)
{
int verticalOffset = 50;
int horizontalOffset = 100;
// Taking care of some bookwork (declaring/initializing
variables)
int maxDataPoints = points.Length;
int chartHeight = bmp.Height;
int chartWidth = bmp.Width;
double adjustedMax = maxValue * .10 + maxValue;
double adjustedMin = minValue - .50 * minValue;
double adjustVerticalRatio = (chartHeight - verticalOffset) /
adjustedMax;
double adjustHorizontalRatio = ((chartWidth - horizontalOffset)
/ (maxDataPoints - 1));
int space = (int)bmp.Width / points.Length;
Pen chartPen = new Pen(Color.Orange, 3);
Pen gridLine = new Pen(Color.LightGray, 1);
int minYpos = 0;
int maxYpos = 0;
for (int i = 0; i < maxDataPoints - 1; i++)
{
int xPos = Convert.ToInt32(i * adjustHorizontalRatio) +
horizontalOffset;
int xPos2 = Convert.ToInt32((i + 1) *
adjustHorizontalRatio) + horizontalOffset;
int yPos = Convert.ToInt32(chartHeight -
adjustVerticalRatio * points) - verticalOffset;
int yPos2 = Convert.ToInt32(chartHeight -
adjustVerticalRatio * points[i + 1]) - verticalOffset;
if (points == minValue)
{
minYpos = yPos;
}
if (points == maxValue)
{
maxYpos = yPos;
}
gfx.DrawLine(gridLine, new Point(xPos2, 0), new
Point(xPos2, chartHeight - verticalOffset));
gfx.DrawLine(chartPen, new Point(xPos, yPos), new
Point(xPos2, yPos2));
gfx.DrawString(i.ToString(), new Font("Arial", 8), new
SolidBrush(Color.Gray), new Point(xPos - 4, chartHeight -
verticalOffset + 10));
}
//Draw Border Lines
Pen borderLine = new Pen(Color.DarkGray, 2);
gfx.DrawLine(borderLine, new Point(horizontalOffset,
chartHeight - verticalOffset), new Point(horizontalOffset, 0));
gfx.DrawLine(borderLine, new Point(horizontalOffset,
chartHeight - verticalOffset), new Point(chartWidth, chartHeight -
verticalOffset));
gfx.DrawLine(borderLine, new Point(chartWidth - 1, chartHeight
- verticalOffset), new Point(chartWidth - 1, 0));
gfx.DrawLine(borderLine, new Point(horizontalOffset, 1), new
Point(chartWidth, 1));
//Drawing Vertical Min/Max Values
gfx.DrawString(maxValue.ToString(), new Font("Arial", 8), new
SolidBrush(Color.Gray), new Point(horizontalOffset - 25, maxYpos));
gfx.DrawString(minValue.ToString(), new Font("Arial", 8), new
SolidBrush(Color.Gray), new Point(horizontalOffset - 25, minYpos));
gfx.DrawLine(gridLine, new Point(horizontalOffset - 25,
minYpos), new Point(chartWidth, minYpos));
gfx.DrawLine(gridLine, new Point(horizontalOffset - 25,
maxYpos), new Point(chartWidth, maxYpos));
//Finalizing and Cleaning Up
Response.ContentType = "image/jpeg";
bmp.Save(Response.OutputStream, ImageFormat.Jpeg);
bmp.Dispose();
gfx.Dispose();
Response.End();
}
private int[] generateRandomValues()
{
Random rand = new Random();
int numValues = rand.Next(10, 20);
int[] newArray = new int[numValues];
for (int i = 0; i < numValues; i++)
{
newArray = rand.Next(100, 1000);
}
return newArray;
}
}
 
R

Rob MacFadyen

ssims,

The problem is that you've called the page "Random" and are trying to use
the system class "Random". Without qualification the compiler sees the
following code:

private int[] generateRandomValues()
{
Random rand = new Random();
int numValues = rand.Next(10, 20);
int[] newArray = new int[numValues];
for (int i = 0; i < numValues; i++)
{
newArray = rand.Next(100, 1000);
}
return newArray;
}

as dealing with your page's class itself.

Try these changes:
private int[] generateRandomValues()
{
System.Random rand = new System.Random();
int numValues = rand.Next(10, 20);
int[] newArray = new int[numValues];
for (int i = 0; i < numValues; i++)
{
newArray = rand.Next(100, 1000);
}
return newArray;
}


Regards,

Rob


ssims said:
I've got this code for a random GDI+ chart generator, and the demo
works, but when I try to run it on my local machine I get a compiler
error: Compiler Error Message: CS0117: 'Random' does not contain a
definition for 'Next'

Here's the code:
using System;
using System.Collections;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Drawing.Drawing2D;
using System.Drawing.Imaging;
using System.Drawing.Text;
using System.Web;
using System.Web.SessionState;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.HtmlControls;

public partial class Random : System.Web.UI.Page
{
Graphics gfx;
Bitmap bmp;
protected void Page_Load(object sender, EventArgs e)
{
bmp = new Bitmap(500, 320);
gfx = Graphics.FromImage(bmp);
gfx.Clear(Color.White);
gfx.SmoothingMode = SmoothingMode.AntiAlias;
// Define Points
//int[] p = new int[] { 10, 12, 48, 102, 290, 15, 100, 25, 300,
200, 150, 200, 60, 40, 250 };
int[] p = generateRandomValues();
DrawChart(p, maxValue(p), minValue(p));
}
private int maxValue(int[] value)
{
int max = Int32.MinValue;
for (int i = 0; i < value.Length; i++)
{
if (value > max)
{
max = value;
}
}
return max;
}
private int minValue(int[] value)
{
int min = Int32.MaxValue;
for (int i = 0; i < value.Length; i++)
{
if (value < min)
{
min = value;
}
}
return min;
}
private void DrawChart(int[] points, int maxValue, int minValue)
{
int verticalOffset = 50;
int horizontalOffset = 100;
// Taking care of some bookwork (declaring/initializing
variables)
int maxDataPoints = points.Length;
int chartHeight = bmp.Height;
int chartWidth = bmp.Width;
double adjustedMax = maxValue * .10 + maxValue;
double adjustedMin = minValue - .50 * minValue;
double adjustVerticalRatio = (chartHeight - verticalOffset) /
adjustedMax;
double adjustHorizontalRatio = ((chartWidth - horizontalOffset)
/ (maxDataPoints - 1));
int space = (int)bmp.Width / points.Length;
Pen chartPen = new Pen(Color.Orange, 3);
Pen gridLine = new Pen(Color.LightGray, 1);
int minYpos = 0;
int maxYpos = 0;
for (int i = 0; i < maxDataPoints - 1; i++)
{
int xPos = Convert.ToInt32(i * adjustHorizontalRatio) +
horizontalOffset;
int xPos2 = Convert.ToInt32((i + 1) *
adjustHorizontalRatio) + horizontalOffset;
int yPos = Convert.ToInt32(chartHeight -
adjustVerticalRatio * points) - verticalOffset;
int yPos2 = Convert.ToInt32(chartHeight -
adjustVerticalRatio * points[i + 1]) - verticalOffset;
if (points == minValue)
{
minYpos = yPos;
}
if (points == maxValue)
{
maxYpos = yPos;
}
gfx.DrawLine(gridLine, new Point(xPos2, 0), new
Point(xPos2, chartHeight - verticalOffset));
gfx.DrawLine(chartPen, new Point(xPos, yPos), new
Point(xPos2, yPos2));
gfx.DrawString(i.ToString(), new Font("Arial", 8), new
SolidBrush(Color.Gray), new Point(xPos - 4, chartHeight -
verticalOffset + 10));
}
//Draw Border Lines
Pen borderLine = new Pen(Color.DarkGray, 2);
gfx.DrawLine(borderLine, new Point(horizontalOffset,
chartHeight - verticalOffset), new Point(horizontalOffset, 0));
gfx.DrawLine(borderLine, new Point(horizontalOffset,
chartHeight - verticalOffset), new Point(chartWidth, chartHeight -
verticalOffset));
gfx.DrawLine(borderLine, new Point(chartWidth - 1, chartHeight
- verticalOffset), new Point(chartWidth - 1, 0));
gfx.DrawLine(borderLine, new Point(horizontalOffset, 1), new
Point(chartWidth, 1));
//Drawing Vertical Min/Max Values
gfx.DrawString(maxValue.ToString(), new Font("Arial", 8), new
SolidBrush(Color.Gray), new Point(horizontalOffset - 25, maxYpos));
gfx.DrawString(minValue.ToString(), new Font("Arial", 8), new
SolidBrush(Color.Gray), new Point(horizontalOffset - 25, minYpos));
gfx.DrawLine(gridLine, new Point(horizontalOffset - 25,
minYpos), new Point(chartWidth, minYpos));
gfx.DrawLine(gridLine, new Point(horizontalOffset - 25,
maxYpos), new Point(chartWidth, maxYpos));
//Finalizing and Cleaning Up
Response.ContentType = "image/jpeg";
bmp.Save(Response.OutputStream, ImageFormat.Jpeg);
bmp.Dispose();
gfx.Dispose();
Response.End();
}
private int[] generateRandomValues()
{
Random rand = new Random();
int numValues = rand.Next(10, 20);
int[] newArray = new int[numValues];
for (int i = 0; i < numValues; i++)
{
newArray = rand.Next(100, 1000);
}
return newArray;
}
}
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,766
Messages
2,569,569
Members
45,042
Latest member
icassiem

Latest Threads

Top