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
C#
using System;
using System.Drawing;
using System.Collections;
using System.ComponentModel;
using System.Windows.Forms;
using System.Data;
classBall
{
publicstaticint xpos;
publicstaticint ypos;
publicstaticint xdir;
publicstaticint ydir;
publicstaticint speed;
}
namespaceBalls
{
publicclassForm1 : 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;
publicForm1()
{
InitializeComponent();
}
////// Clean up any resources being used.///protectedoverridevoidDispose(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.///privatevoidInitializeComponent()
{
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]
staticvoidMain()
{
Application.Run(new Form1());
Ball Ball1 = new Ball();
Ball1.xpos = 300;
Ball1.ypos = 200;
Ball1.speed = 5;
Ball1.xdir = 1;
Ball1.xdir = 1;
}
privatevoidTheTicker_Tick(object sender, System.EventArgs e)
{
int BallSpeed = 5;
int xpos = btnBall.Location.X;
int ypos = btnBall.Location.Y;
#region checkball#region checkX//check xif (Ball1.xdir == 1)
{
//check to see if edge of screenif (Ball1.xpos + 50 > this.Width)
{
Ball1.xpos -= Ball1.speed;
Ball1.xdir = 0;
}
else
{
Ball1.xpos += Ball1.speed;
}
}
else
{
//check to see if edge of screenif (xpos else
{
xpos -= BallSpeed;
}
}
#endregion#region checky//checkyif (varDirectionY.Text == "+")
{
//check to see if edge of screenif (ypos + 50 >= this.Height)
{
ypos -= BallSpeed;
varDirectionY.Text = "-";
}
else
{
ypos += BallSpeed;
}
}
else
{
//check to see if edge of screenif (ypos else
{
ypos -= BallSpeed;
}
}
#endregion#endregion
btnBall.Location = new Point(xpos, ypos);
}
privatevoidbtnBall_Click(object sender, System.EventArgs e)
{
}
}
}
3 Answers
Know the answer? Post it. Somebody with the same question will find it here.
Krishna GaradRank #191 · 12.8K reputationPosted Jan 27, 2011, 2:14 AM EST
✓ Accepted answer0
your class members are static then whenever you are accessing thos member you can not access it with creating object/instance of that class. Also your class is not static but in your class there is no any instance member all are static member while accessing those static members of class you must have to access with class name.
hmm. is there a way I can add moe balls then?
I was was next going to try add multiple "balls" so was hoping to be able to make new ones on the fly
0
Sam HobbsRank #107 · 20.5K reputationPosted Jan 27, 2011, 11:40 AM EST
0
You probably need to remove "static" from the members of the Ball class, but since I don't know what you are trying to do, I don't know how to make your program do what you are trying to do.
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.
Krishna GaradRank #191 · 12.8K reputationPosted Jan 27, 2011, 2:14 AM EST
Box51 reputationPosted Jan 27, 2011, 3:20 AM EST
Sam HobbsRank #107 · 20.5K reputationPosted Jan 27, 2011, 11:40 AM EST
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.