Tell me more ×
Code Review Stack Exchange is a question and answer site for peer programmer code reviews. It's 100% free, no registration required.

This is the the code what I'm using. I'm working on LINQ TO SQL and I'm using this model in my program:

http://stackoverflow.com/questions/7072819/how-can-i-model-this-class-in-a-database/7072841#7072841

The person must have to enter the leve, for example 1.2, 1.1.4, etc. It must check is the level is available or busy. The function ToIntArray is an extension, consists in split the text and convert that string[] to int[].

The first conditions as you can see is an inconsistence which I have at this post: http://stackoverflow.com/questions/7085713/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()
                       select p).SingleOrDefault();

            if (aux != null)
            {
                MessageBox.Show("Level already exists");
                return;
            }
        }

        var newObjective = new Objective();
        newObjective.Name = NameTextBox.Text;
        newObjective.Level = levels.Last();
        newObjective.Parent_ObjectiveID = currentObjective == null ? null : (int?)currentObjective.ObjectiveID ;

        cd.Objective.InsertOnSubmit(newObjective);
        cd.SubmitChanges();

        MessageBox.Show("The new objective has added successfully");
        NameTextBox.Clear();
        LoadObjectives();
    }

I think it's all. Thanks in advance.

share|improve this question

1 Answer

A few comments:
1. You should move this code from the AddButton_Click to some business logic class.
2. Maybe you should add some validation of LevelTextBox.Text, before using its value.
3. 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.
4. 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.

share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.