Skip to content

Commit 3be1d02

Browse files
Konstantina ChremmouKonstantina Chremmou
authored andcommitted
Minor code smells.
Signed-off-by: Konstantina Chremmou <[email protected]>
1 parent 9a80dc9 commit 3be1d02

File tree

2 files changed

+51
-44
lines changed

2 files changed

+51
-44
lines changed

XenAdmin/Controls/MultipleDvdIsoList.cs

Lines changed: 47 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,7 @@ namespace XenAdmin.Controls
4444
{
4545
public partial class MultipleDvdIsoList : UserControl
4646
{
47-
bool inRefresh = false;
47+
private bool _inRefresh;
4848

4949
public MultipleDvdIsoList()
5050
{
@@ -61,7 +61,7 @@ public VM VM
6161
cdChanger1.VM = value;
6262
if (value != null)
6363
cdChanger1.VM.PropertyChanged += vm_PropertyChanged;
64-
refreshDrives();
64+
RefreshDrives();
6565
}
6666
get => cdChanger1.VM;
6767
}
@@ -72,24 +72,24 @@ public VM VM
7272
[Category("Appearance")]
7373
public Color LabelSingleDvdForeColor
7474
{
75-
get { return labelSingleDvd.ForeColor; }
76-
set { labelSingleDvd.ForeColor = value; }
75+
get => labelSingleDvd.ForeColor;
76+
set => labelSingleDvd.ForeColor = value;
7777
}
7878

7979
[Browsable(true)]
8080
[Category("Appearance")]
8181
public Color LabelNewCdForeColor
8282
{
83-
get { return newCDLabel.ForeColor; }
84-
set { newCDLabel.ForeColor = value; }
83+
get => newCDLabel.ForeColor;
84+
set => newCDLabel.ForeColor = value;
8585
}
8686

8787
[Browsable(true)]
8888
[Category("Appearance")]
8989
public Color LinkLabelLinkColor
9090
{
91-
get { return linkLabel1.LinkColor; }
92-
set { linkLabel1.LinkColor = value; }
91+
get => linkLabel1.LinkColor;
92+
set => linkLabel1.LinkColor = value;
9393
}
9494

9595
#endregion
@@ -115,24 +115,25 @@ internal virtual void DeregisterEvents()
115115
cdChanger1.DeregisterEvents();
116116
}
117117

118-
void vm_PropertyChanged(object sender, PropertyChangedEventArgs e)
118+
private void vm_PropertyChanged(object sender, PropertyChangedEventArgs e)
119119
{
120120
if (e.PropertyName == "VBDs")
121-
refreshDrives();
121+
RefreshDrives();
122122
}
123123

124-
private void refreshDrives()
124+
private void RefreshDrives()
125125
{
126126
VbdCombiItem prevSelection = comboBoxDrive.SelectedItem as VbdCombiItem;
127-
inRefresh = true;
127+
_inRefresh = true;
128128

129129
foreach (object o in comboBoxDrive.Items)
130130
{
131-
VbdCombiItem v = o as VbdCombiItem;
132-
v.vbd.PropertyChanged -= new PropertyChangedEventHandler(vbd_PropertyChanged);
131+
if (o is VbdCombiItem v)
132+
v.Vbd.PropertyChanged -= vbd_PropertyChanged;
133133
}
134134

135135
comboBoxDrive.Items.Clear();
136+
136137
if (VM != null && !VM.is_control_domain)
137138
{
138139
List<VBD> vbds = VM.Connection.ResolveAll(VM.VBDs);
@@ -142,31 +143,32 @@ private void refreshDrives()
142143
VM.Connection.CachePopulated += CachePopulatedMethod;
143144
return;
144145
}
146+
145147
vbds.RemoveAll(vbd => !vbd.IsCDROM() && !vbd.IsFloppyDrive());
146148
vbds.Sort();
149+
147150
int dvdCount = 0;
148151
int floppyCount = 0;
152+
149153
foreach (VBD vbd in vbds)
150154
{
151-
vbd.PropertyChanged +=new PropertyChangedEventHandler(vbd_PropertyChanged);
155+
vbd.PropertyChanged += vbd_PropertyChanged;
156+
VbdCombiItem item;
157+
152158
if (vbd.IsCDROM())
153159
{
154160
dvdCount++;
155-
VbdCombiItem i = new VbdCombiItem();
156-
i.name = string.Format(Messages.DVD_DRIVE_LABEL_NUMBERED, dvdCount);
157-
i.vbd = vbd;
158-
comboBoxDrive.Items.Add(i);
161+
item = new VbdCombiItem(string.Format(Messages.DVD_DRIVE_LABEL_NUMBERED, dvdCount), vbd);
159162
}
160163
else
161164
{
162165
floppyCount++;
163-
VbdCombiItem i = new VbdCombiItem();
164-
i.name = string.Format(Messages.FLOPPY_DRIVE_LABEL_NUMBERED, floppyCount);
165-
i.vbd = vbd;
166-
comboBoxDrive.Items.Add(i);
167-
}
166+
item = new VbdCombiItem(string.Format(Messages.FLOPPY_DRIVE_LABEL_NUMBERED, floppyCount), vbd);
167+
}
168+
comboBoxDrive.Items.Add(item);
168169
}
169170
}
171+
170172
if (comboBoxDrive.Items.Count == 0)
171173
{
172174
comboBoxDrive.Visible = false;
@@ -197,54 +199,59 @@ private void refreshDrives()
197199
newCDLabel.Visible = false;
198200
linkLabel1.Visible = true;
199201
}
200-
inRefresh = false;
202+
203+
_inRefresh = false;
204+
201205
// Restore prev selection or select the top item by default
202206
if (prevSelection != null)
203207
{
204208
foreach (object o in comboBoxDrive.Items)
205209
{
206-
VbdCombiItem v = o as VbdCombiItem;
207-
if (v.vbd.uuid == prevSelection.vbd.uuid)
210+
if (o is VbdCombiItem v && v.Vbd.uuid == prevSelection.Vbd.uuid)
208211
{
209212
comboBoxDrive.SelectedItem = o;
210213
return;
211214
}
212215
}
213216
}
214-
if (comboBoxDrive.Items.Count == 0)
215-
comboBoxDrive.SelectedItem = null;
216-
else
217-
comboBoxDrive.SelectedItem = comboBoxDrive.Items[0];
217+
218+
comboBoxDrive.SelectedItem = comboBoxDrive.Items.Count == 0 ? null : comboBoxDrive.Items[0];
218219
}
219220

220-
void vbd_PropertyChanged(object sender, PropertyChangedEventArgs e)
221+
private void vbd_PropertyChanged(object sender, PropertyChangedEventArgs e)
221222
{
222-
refreshDrives();
223+
RefreshDrives();
223224
}
224225

225226
private void CachePopulatedMethod(IXenConnection conn)
226227
{
227228
VM.Connection.CachePopulated -= CachePopulatedMethod;
228-
refreshDrives();
229+
RefreshDrives();
229230
}
230231

231-
internal class VbdCombiItem
232+
private class VbdCombiItem
232233
{
233-
public string name;
234-
public VBD vbd;
234+
public string Name { get; }
235+
public VBD Vbd { get; }
236+
237+
public VbdCombiItem(string name, VBD vbd)
238+
{
239+
Name = name;
240+
Vbd = vbd;
241+
}
235242

236243
public override string ToString()
237244
{
238-
return name;
245+
return Name;
239246
}
240247
}
241248

242249
private void comboBoxDrive_SelectedIndexChanged(object sender, EventArgs e)
243250
{
244-
if (inRefresh)
251+
if (_inRefresh)
245252
return;
246253

247-
cdChanger1.Drive = (comboBoxDrive.SelectedItem as VbdCombiItem)?.vbd;
254+
cdChanger1.Drive = (comboBoxDrive.SelectedItem as VbdCombiItem)?.Vbd;
248255
}
249256

250257

XenModel/Actions/VBD/VbdCreateAndPlugAction.cs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -62,7 +62,7 @@ public VbdCreateAndPlugAction(VM vm, VBD vbd, string vdiName, bool suppress)
6262

6363
protected override void Run()
6464
{
65-
string vbdServerRef = VBD.create(Session, vbd);
65+
string vbdRef = VBD.create(Session, vbd);
6666

6767
if (!VM.IsHVM() && vbd.empty)
6868
{
@@ -71,12 +71,12 @@ protected override void Run()
7171
}
7272

7373
// Then if we can plug the vbd in, do so...
74-
if (vbdServerRef != null &&
75-
VBD.get_allowed_operations(Session, vbdServerRef).Contains(vbd_operations.plug))
74+
if (vbdRef != null &&
75+
VBD.get_allowed_operations(Session, vbdRef).Contains(vbd_operations.plug))
7676
{
7777

7878
log.DebugFormat("Attempting to hot plug VBD {0}.", vbd.uuid);
79-
RelatedTask = VBD.async_plug(Session, vbdServerRef);
79+
RelatedTask = VBD.async_plug(Session, vbdRef);
8080
PollToCompletion();
8181
Description = Messages.ATTACHDISKWIZARD_ATTACHED;
8282
}

0 commit comments

Comments
 (0)