patterncsharpMinor
Determining if a level exists
Viewed 0 times
leveldeterminingexists
Problem
This is the code that I'm using. I'm working on LINQ TO SQL and I'm using this model in my program:
How can I model this class in a database
The person must have to enter the level, for example 1.2, 1.1.4, etc. The program must check if the level is available or busy. The function
The first condition, as you can see, is an inconsistency which I have in this post:
Problems with nullable types in a LINQ function
```
private void AddButton_Click(object sender, RoutedEventArgs e)
{
NorthwindDataContext cd = new NorthwindDataContext();
int[] levels = LevelTextBox.Text.ToIntArray('.');
string newGroupName = NameTextBox.Text;
Objective currentObjective = null;
int? identity = null;
for (int i = 0; i < levels.Length - 1; i++ )
{
int currentRank = levels[i];
if (identity == null)
{
currentObjective = (from p in cd.Objective
where p.Level == currentRank && p.Parent_ObjectiveID == null
select p).SingleOrDefault();
}
else
{
currentObjective = (from p in cd.Objective
where p.Level == currentRank && p.Parent_ObjectiveID == identity
select p).SingleOrDefault();
}
if (currentObjective == null)
{
MessageBox.Show("Levels don't exist");
return;
}
else
{
identity = currentObjective.ObjectiveID;
}
}
if (currentObjective != null)
{
if (levels.Last() == currentObjective.Level)
{
MessageBox.Show("Level already exists");
return;
}
}
else
{
var aux = (from p in cd.Objective
where p.Parent_ObjectiveID == null && p.Level == levels.Last()
How can I model this class in a database
The person must have to enter the level, for example 1.2, 1.1.4, etc. The program must check if the level is available or busy. The function
ToIntArray is an extension and consists of splitting the text and converting the resulting string[] to int[].The first condition, as you can see, is an inconsistency which I have in this post:
Problems with nullable types in a LINQ function
```
private void AddButton_Click(object sender, RoutedEventArgs e)
{
NorthwindDataContext cd = new NorthwindDataContext();
int[] levels = LevelTextBox.Text.ToIntArray('.');
string newGroupName = NameTextBox.Text;
Objective currentObjective = null;
int? identity = null;
for (int i = 0; i < levels.Length - 1; i++ )
{
int currentRank = levels[i];
if (identity == null)
{
currentObjective = (from p in cd.Objective
where p.Level == currentRank && p.Parent_ObjectiveID == null
select p).SingleOrDefault();
}
else
{
currentObjective = (from p in cd.Objective
where p.Level == currentRank && p.Parent_ObjectiveID == identity
select p).SingleOrDefault();
}
if (currentObjective == null)
{
MessageBox.Show("Levels don't exist");
return;
}
else
{
identity = currentObjective.ObjectiveID;
}
}
if (currentObjective != null)
{
if (levels.Last() == currentObjective.Level)
{
MessageBox.Show("Level already exists");
return;
}
}
else
{
var aux = (from p in cd.Objective
where p.Parent_ObjectiveID == null && p.Level == levels.Last()
Solution
A few comments:
-
You should split this code into different methods.
You have three responsibilities here:
The fact that all three actions use the same data doesn't mean you need to put them in one method.
-
Maybe I get it wrong somewhere, but I can't understand how this code checks if the level already exists. I can't see how the code will ever get into the
- You should move this code from the
AddButton_Clickto some business logic class.
- Maybe you should add some validation of
LevelTextBox.Text, before using its value.
-
You should split this code into different methods.
You have three responsibilities here:
- Find out if the level exists at all.
- Find out if the specified level already exists.
- Create a new level.
The fact that all three actions use the same data doesn't mean you need to put them in one method.
-
Maybe I get it wrong somewhere, but I can't understand how this code checks if the level already exists. I can't see how the code will ever get into the
MessageBox.Show("Level already exists"); in both places.Context
StackExchange Code Review Q#4160, answer score: 3
Revisions (0)
No revisions yet.