开发者

C# (again,again?) Variable not saving

Whoo guess who's back! Program runs (horrah) but now the variable isn't saving the value. the main form:

Airplane plane = new Airplane();    

private void btnAccel_Click(object sender, EventArgs e)
{
    lblStatus.Text = plane.speed.ToString();
    plane.speed = double.Parse(txtSpeed.Text);
    plane.Accelerate();
    lblStatus.Text = plane.speed.ToString();        

}

And from the class Airplane:

class Airplane
{
    private string name{get; set;}
    private Position PlanePosition;
    private static int numbe开发者_开发知识库rCreated;

    public Airplane()
    {
        this.PlanePosition = new Position();
    }

    public void Accelerate()
    {
        // increase the speed of the airplane    
        if (PlanePosition.speed < Position.MAX_SPEED)
        {
            PlanePosition.speed +=1;  // or speed += 1;
        }//end of if
        numberCreated++;  // increment the numberCreated each time an Airplane object is created    
    }

"Position" is another class:

class Position
{
    internal int x_coordinate;
    internal int y_coordinate;
    internal double speed;
    internal int direction;
    internal const int MAX_SPEED = 50;

    public Position()
    {                
    }

    public string displayPosition()
    {
        return "okay";
    }
}

And for some reason the variable from the text box in the main form goes into a "speed" variable but the speed variable in the Airplane class doesn't have that variable.


Either your example isn't complete, or your code is magically compiling. I'm guessing you need to implement a property to access the speed contained in your PlanePosition instance.

class Airplane
{
    private string name{get; set;}
    private Position PlanePosition;
    private static int numberCreated;

    public double speed
    {
        get { PlanePosition.Speed = value; }
        set { return PlanePosition.speed; }
    }

    public Airplane()
    {
        this.PlanePosition = new Position();
    }

    public void Accelerate()
    {
        // increase the speed of the airplane    
        if (PlanePosition.speed < Position.MAX_SPEED)
        {
            PlanePosition.speed +=1;  // or speed += 1;
        }//end of if
        numberCreated++;  // increment the numberCreated each time an Airplane object is created    
    }
}


You are calling Accelerate right after setting the Speed from the textbox. Therefore automatically increasing the speed by 1... That's not the problem is it? If not can you provide a little more info for those not familiar with what you're doing?


In general terms, before we look at your problem, take a look at your object model. Is speed really an attribute of a plane's position? REALLY?

The reason you need to sort this out first is because, left as it is, you'll wind up building a kludge of unmaintainable code where the coder has to know specifically where everything is stored.

Also, the maximum speed should not be fixed as a constant value - someone somewhere will eventually build a faster plane and ruin your entire day.

I suggest, before going any further, you think about looking at these sort of issues. If you are working with other students in class, peer review might be something you all want to consider.

As for the problem itself, have you stepped through the code with a debugger? Just stepping through code a line at a time can often reveal the differences between what you have written, and what you intended to write at the time...

Stick a breakpoint in btn_Accel_Click - is it even firing? I know you think I'm being sarcastic, but I really aren't - I'm trying to help you. This is exactly what I would do before getting too bogged down in trying to analyse everything myself.

Martin.


Your Airplane Class doesn't have a speed atribute. How would it work??

Or you make like Greg Buehled aswered, or you could try something like this :

set your PlanePositon as public

class Airplane
{
    private string name{get; set;}
    public Position PlanePosition;
    private static int numberCreated;

    public Airplane()
    {
        this.PlanePosition = new Position();
    }

    public void Accelerate()
    {
        // increase the speed of the airplane    
        if (PlanePosition.speed < Position.MAX_SPEED)
        {
            PlanePosition.speed +=1;  // or speed += 1;
        }//end of if
        numberCreated++;  // increment the numberCreated each time an Airplane object is created    
    }

and then you change it in your btnAccel_Click event.

private void btnAccel_Click(object sender, EventArgs e)
{
    lblStatus.Text = plane.speed.ToString();
    plane.PlanePosition.speed = double.Parse(txtSpeed.Text);
    plane.Accelerate();
    lblStatus.Text = plane.PlanePosition.speed.ToString();        

}

It's not the best, but is also a way.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜