【问题标题】:Binding objects to radio buttons c#将对象绑定到单选按钮 c#
【发布时间】:2015-10-20 04:17:07
【问题描述】:

我是 OOP 的新手,所以如果可能的话,我不知道我在问什么,但这里是。我的表单上有两个单选按钮“radiobutton1”和“radiobutton2”。我有一个带有名为“car1”的实例的车辆类。我还有一个 Motor 类,它有两个实例,分别称为“smallMotor”和“largeMotor”。我正在尝试创建一个名为“SetMotor”的方法,该方法应该将 smallMotor 或 largeMotor 设置为 car1 的电机。此方法应该采用两个布尔值,由两个单选按钮表示,以确定为 car1 设置哪个电机对象。我尝试了以下方法,但它不起作用并给了我错误。这是我的代码:

主要形式:

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace WindowsFormsApplication12
{
    public partial class Form1 : Form
    {
        Motor largeMotor;
        Motor smallMotor;
        Vehicle car1;
        public Form1()
        {
            InitializeComponent();
            largeMotor = new Motor(2000);
            smallMotor = new Motor(1000);
            car1 = new Vehicle();
        }

        private void button1_Click(object sender, EventArgs e)
        {
            car1.SetModel(textBoxModel.Text);
            car1.SetNumDoors((int)numericUpDown1.Value);
            car1.SetMotor(radioButton1.Checked, radioButton2.Checked); 
        }
    }
}

我的汽车班:

    class Motor
    {
        private int power;

        public Motor(int p)
        {
            power = p;
        }
    }

车辆类:

class Vehicle
{
    private Motor motor;

    public void SetMotor(bool smallMotor, bool largeMotor)
    {
        if (largeMotor)
        {
            motor = largeMotor;
        }
        else
            motor = smallMotor;
    }
}

这给我的错误是:

错误 6 无法将类型“bool”隐式转换为“WindowsFormsApplication12.Motor”。

那么,是否可以将两个电机对象绑定到单选按钮?非常感谢任何帮助。

【问题讨论】:

  • 你的motor对象是什么?,一定有motor.IsLarge之类的
  • largeMotor 是布尔值,而 motor 是电机类的对象,这就是为什么你会得到 Cannot implicitly convert type 'bool' to 'WindowsFormsApplication12.Motor'
  • 你应该知道你的源代码中多了一个括号
  • 您能否添加更多源代码,因为我目前看到的主要错误是您的电机字段(猜测它是您表单中的字段)并且您说我的电机实例等于没有意义的布尔值。 @ShaminderSAujla 说的非常好。您可以重构您的 Vehicule 类以拥有一个名为 IsLarge 的属性。并且不需要两个单选按钮。一个单选按钮就可以了。未选中时,IsLarge 为假(小电机),选中时,isLarge 为真(大电机)。瞧,不需要 2 个单选按钮

标签: c# oop object


【解决方案1】:

在您当前的代码中,您尝试将 bool 值分配给 Motor,由于您的代码是强类型的,因此无法做到这一点。

largeMotorsmallMotor 对象之间没有区别。如果您以后需要知道电机是大还是小,您的Vehicle 对象将无法告诉您。就您的班级结构而言,这是一个快速建议:

class Vehicle
{
    public Motor Motor
    {
        get;
        set;
    }
}

class Motor
{
    public MotorType Type
    {
        get;
        set;
    }
}

enum MotorType
{
    Large,
    Small
}

class SomeClass
{
    public void SetMotorType(bool isSmall)
    {
        // This object would probably come from some place else
        Vehicle vehicle = new Vehicle();
        vehicle.Motor = new Motor { Type = (isSmall) ? MotorType.Small : MotorType.Large };
    }
}

现在,Vehicle.Motor.MotorType 会一直让你知道电机是大是小。

编辑:

暂时忘掉编程和 OOP,想想现实世界吧。有一辆汽车有发动机和轮胎。一个类,在编程中,是一个完整的真实世界对象的表示(仅限于现在)。现在,按照这种理解,您的代码中应该有一个类Car。现在,汽车有发动机和轮胎。引擎和轮胎本身也是完整的。所以你需要一个EngineTyre 的类。接下来,你知道汽车有发动机和轮胎。要在代码中关联这三个类,您可以使用如下属性:

class Car
{
public Engine Engine{get;set;}

// Since car has more than one tyres, it should be one to many relation
public List<Tyre> Tyres{get;set;}

}

现在,引擎可以分解为活塞、气缸等部分。根据您想要的粒度,活塞可能是引擎的一个属性,也可能是一个类,具有诸如 MadeOf、Diameter、Length 等属性等。为了简单起见,我不会做更深入的。那么,如何将这些属性链接到 Engine?通过使用属性。因此,

class Engine{
// Or any other Piston attribute you are interested in.
public int PistonId{get;set;}

public decimal CylinderVolume{get;set;}
}

同样,轮胎将使用它们拥有的属性来定义。像半径,它们是否是无内胎和其他属性。所以,

class Tyre{
public int radius{get;set;}
public bool IsTubeless{get;set;}
}

现在,您可以说 Car 有无内胎轮胎,因此我们可以将 IsTubeless 属性放在汽车中。但从逻辑上思考,Tyre 类对象知道它是否是无内胎更有意义。这就是你可以定义你的类的方式。

之前,我提到类代表完整的真实世界对象。一旦你深入挖掘并偶然发现抽象类,你就会意识到这不是真的。但留到以后再说吧。

【讨论】:

  • 没有比这更简单的了。很好的答案!
  • 写得好。确实很好的答案。
  • 感谢您的回答。很多东西对我来说都是新的,所以我会试着理解它。
  • @grammer 编辑了回复并提供了一些解释。希望对您有所帮助。
猜你喜欢
  • 2014-06-06
  • 2013-04-23
  • 2016-05-21
  • 2012-05-18
  • 2011-10-06
  • 2015-04-27
  • 2015-01-29
  • 1970-01-01
  • 2018-08-03
相关资源
最近更新 更多