Im new to c sharp so am creating simple things to learn.
i know using global variables bad and have been led towards using a static class? attached is all the code. I get an error saying The type or namespace name 'Ball1' could not be found (are you missing a using directive or an assembly reference?) so im guessing im using this wrong but I cant see how. i've spent a couple hours looking at tutorials and the code seems right so i can only guess i've put something in the wrong place?
Any help would be greatly apreciated
using System; using System.Drawing; using System.Collections; using System.ComponentModel; using System.Windows.Forms; using System.Data; class Ball { public static int xpos; public static int ypos; public static int xdir; public static int ydir; public static int speed; } namespace Balls { public class Form1 : System.Windows.Forms.Form { private System.Windows.Forms.Timer TheTicker; private System.Windows.Forms.Button btnBall; private System.Windows.Forms.Label varDirection; private System.Windows.Forms.Label varDirectionY; private System.ComponentModel.IContainer components; public Form1() { InitializeComponent(); } /// /// Clean up any resources being used. /// protected override void Dispose( bool disposing ) { if( disposing ) { if (components != null) { components.Dispose(); } } base.Dispose( disposing ); } #region Windows Form Designer generated code /// /// Required method for Designer support - do not modify /// the contents of this method with the code editor. /// private void InitializeComponent() { this.components = new System.ComponentModel.Container(); this.TheTicker = new System.Windows.Forms.Timer(this.components); this.btnBall = new System.Windows.Forms.Button(); this.varDirection = new System.Windows.Forms.Label(); this.varDirectionY = new System.Windows.Forms.Label(); this.SuspendLayout(); // // TheTicker // this.TheTicker.Enabled = true; this.TheTicker.Interval = 1; this.TheTicker.Tick += new System.EventHandler(this.TheTicker_Tick); // // btnBall // this.btnBall.Location = new System.Drawing.Point(288, 192); this.btnBall.Name = "btnBall"; this.btnBall.Size = new System.Drawing.Size(50, 50); this.btnBall.TabIndex = 0; this.btnBall.Text = "Press Me"; this.btnBall.Click += new System.EventHandler(this.btnBall_Click); // // varDirection // this.varDirection.Location = new System.Drawing.Point(616, 0); this.varDirection.Name = "varDirection"; this.varDirection.Size = new System.Drawing.Size(8, 16); this.varDirection.TabIndex = 1; this.varDirection.Text = "label1"; // // varDirectionY // this.varDirectionY.Location = new System.Drawing.Point(624, 0); this.varDirectionY.Name = "varDirectionY"; this.varDirectionY.Size = new System.Drawing.Size(8, 16); this.varDirectionY.TabIndex = 2; this.varDirectionY.Text = "label1"; // // Form1 // this.AutoScaleBaseSize = new System.Drawing.Size(5, 13); this.ClientSize = new System.Drawing.Size(632, 446); this.Controls.Add(this.varDirectionY); this.Controls.Add(this.varDirection); this.Controls.Add(this.btnBall); this.Name = "Form1"; this.Text = "Form1"; this.ResumeLayout(false); } #endregion [STAThread] static void Main() { Application.Run(new Form1()); Ball Ball1 = new Ball(); Ball1.xpos = 300; Ball1.ypos = 200; Ball1.speed = 5; Ball1.xdir = 1; Ball1.xdir = 1; } private void TheTicker_Tick(object sender, System.EventArgs e) { int BallSpeed = 5; int xpos = btnBall.Location.X; int ypos = btnBall.Location.Y; #region checkball #region checkX
//check x if (Ball1.xdir == 1) { //check to see if edge of screen if (Ball1.xpos + 50 > this.Width) { Ball1.xpos -= Ball1.speed; Ball1.xdir = 0; } else { Ball1.xpos += Ball1.speed; } } else { //check to see if edge of screen if (xpos < 0) { xpos += BallSpeed; varDirection.Text = "+"; } else { xpos -= BallSpeed; } } #endregion #region checky //checky if (varDirectionY.Text == "+") { //check to see if edge of screen if (ypos + 50 >= this.Height) { ypos -= BallSpeed; varDirectionY.Text = "-"; } else { ypos += BallSpeed; } } else { //check to see if edge of screen if (ypos <= 0) { ypos += BallSpeed; varDirectionY.Text = "+"; } else { ypos -= BallSpeed; } } #endregion #endregion btnBall.Location = new Point(xpos, ypos); } private void btnBall_Click(object sender, System.EventArgs e) {
} } }
|
Krishna GaradPosted Jan 27, 2011, 7:14 AM
Sam HobbsPosted Jan 27, 2011, 4:40 PM
Note that the line that says "Application.Run(new Form1())" runs the form and the code that does "new Ball()" does not execute until after the form is closed. So Ball1 does not even exist until after the form is closed. If you put the "Application.Run(new Form1())" after the "new Ball()" then it still won't work since the form would need a way to find the Ball. Since I don't know what you are trying to do, I cannot tell you what to do to fix it, but my guess is that you need to put the "new Ball()" in the form's constructor or in the form load event or maybe in a button's click event or maybe in a menu item's event handler.
BoxPosted Jan 27, 2011, 8:20 AM