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

Discussion in 'ASP .Net' started by ssims, Sep 13, 2006.

  1. ssims

    ssims Guest

    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;
    }
    }
     
    ssims, Sep 13, 2006
    #1
    1. Advertising

  2. 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" <> wrote in message
    news:...
    > 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;
    > }
    > }
    >
     
    Rob MacFadyen, Sep 14, 2006
    #2
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Ashish Kanoongo
    Replies:
    1
    Views:
    619
    Vjekoslav Babic
    Feb 3, 2004
  2. Columbia Pike
    Replies:
    5
    Views:
    27,094
    Columbia Pike
    Aug 9, 2004
  3. Justin Dutoit
    Replies:
    3
    Views:
    3,515
    =?Utf-8?B?dG9tIHdpc25vd3NraQ==?=
    Oct 7, 2004
  4. Lars-Erik Aabech
    Replies:
    8
    Views:
    845
    Lars-Erik Aabech
    Apr 28, 2005
  5. Jason Carlton
    Replies:
    11
    Views:
    248
    Dr J R Stockton
    Dec 8, 2009
Loading...

Share This Page