Click here to Skip to main content
15,888,401 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
The problem is that I have is that I have code in my program that is suppose to populate the combobox with the values in the array but it doesn't seem to work.

using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Runtime.InteropServices.WindowsRuntime;
using Windows.Foundation;
using Windows.Foundation.Collections;
using Windows.UI.Xaml;
using Windows.UI.Xaml.Controls;
using Windows.UI.Xaml.Controls.Primitives;
using Windows.UI.Xaml.Data;
using Windows.UI.Xaml.Input;
using Windows.UI.Xaml.Media;
using Windows.UI.Xaml.Navigation;

// The Blank Page item template is documented at https://go.microsoft.com/fwlink/?LinkId=402352&clcid=0x409

namespace Arsalan_Salam_991571527_A2
{
    /// <summary>
    /// An empty page that can be used on its own or navigated to within a Frame.
    /// </summary>
    public sealed partial class MainPage : Page
    {
        public MainPage()
        {
            this.InitializeComponent();
            AddingEnumIntoComboBox(null);
            AddingIntArrayIntoComboBox(null);
        }
        //private List<Product> _products = new List<Product>();
        private CarRepository _carRepository = new CarRepository();
        private Car CaptureProductInfo()
        {
            string vinNumber = vinNumberInput.Text;
            string carMake = carMakeInput.Text;
            CarType typeOfCar = (CarType)carTypeInput.SelectedItem;
            float purchasePrice = float.Parse(purchasePriceInput.Text);
            int modelYear = Int32.Parse((string)modelYearInput.SelectedItem);
            int mileage = Int32.Parse(mileageInput.Text);
            Car c = new Car(vinNumber, carMake, typeOfCar, purchasePrice, modelYear, mileage);
            AddingIntArrayIntoComboBox(c);
            return c;
        }
        private void RenderProductInfo(Car car)
        {
            vinNumberInput.Text = car.VinNumber;
            carMakeInput.Text = car.CarMake;
            carTypeInput.Text = car.TypeOfCar.ToString();
            purchasePriceInput.Text = car.PurchasePrice.ToString();
            modelYearInput.Text = car.ModelYear.ToString();
            mileageInput.Text = car.Mileage.ToString();
        }
        private void ClearUI()
        {
            vinNumberInput.Text = "";
            carMakeInput.Text = "";
            carTypeInput.Text = "";
            purchasePriceInput.Text = "";
            modelYearInput.Text = "";
            mileageInput.Text = "";
            errorMessageOutput.Text = "";
        }
        private void OnCarSelectionChanged()
        {
            //1. get the selected product
            Car c = (Car)lstCarDetailOutput.SelectedItem;

            //2. render the fields in the textboxes
            RenderProductInfo(c);
        }
        private void AddingEnumIntoComboBox(object p)
        {
            foreach (var item in Enum.GetValues(typeof(CarType)))
            {
                var carTypeName = Enum.GetName(typeof(CarType), item);
                carTypeInput.Items.Add(carTypeName);
            }
        }
        private void AddingIntArrayIntoComboBox(Car c) 
        {
            for (int i = 0; i < c._modelYear.Length; i++)
            {
                int year = c._modelYear[i];
                modelYearInput.Items.Add(year);
            }
        }
        private void addingCar_Click(object sender, RoutedEventArgs e)
        {
            try
            {
                //_products.Add(CaptureProductInfo());
                Car c = CaptureProductInfo();
                _carRepository.AddProduct(c);
                lstCarDetailOutput.Items.Add(c);//the advantage of adding Product rather than a string?
            }
            catch (Exception ex)
            {
                errorMessageOutput.Text = ex.Message;
            }
            //
        }
        private void clearing_Click(object sender, RoutedEventArgs e)
        {
            ClearUI();
        }
        private void updatingCar_Click(object sender, RoutedEventArgs e)
        {
            OnCarSelectionChanged();
        }
    }
}



This is the entire code

What I have tried:

I had a different way of doing the populating my second combobox which is called modelYearInput and the code that I am using to do this is as shown below:

private void AddingIntArrayIntoComboBox(Car c) 
        {
            for (int i = 0; i < c._modelYear.Length; i++)
            {
                int year = c._modelYear[i];
                modelYearInput.Items.Add(year);
            }
        }


Thank you in advance for any help
Posted
Updated 29-Jun-20 6:19am

"It doesn't work" is probably the most useless problem report we get - and we get it a lot. It tells us nothing about what is happening, or when it happens.
So tell us what it is doing that you didn't expect, or not doing that you did.
Tell us what you did to get it to happen.
Tell us any error messages.

In the meantime, you need to look at why "it doesn't work" - and we can't do that for you, we don't have your data.

So, it's going to be up to you.
Fortunately, you have a tool available to you which will help you find out what is going on: the debugger. If you don't know how to use it then a quick Google for "Visual Studio debugger" should give you the info you need.

Put a breakpoint on the first line in the function, and run your code through the debugger. Then look at your code, and at your data and work out what should happen manually. Then single step each line checking that what you expected to happen is exactly what did. When it isn't, that's when you have a problem, and you can back-track (or run it again and look more closely) to find out why.

Sorry, but we can't do that for you - time for you to learn a new (and very, very useful) skill: debugging!
 
Share this answer
 
Quote:
The problem is that I have is that I have code in my program that is suppose to populate the combobox with the values in the array but it doesn't seem to work.

Don't suppose, make sure. There is 1 tool that can help you, the debugger.

Your code do not behave the way you expect, or you don't understand why !

There is an almost universal solution: Run your code on debugger step by step, inspect variables.
The debugger is here to show you what your code is doing and your task is to compare with what it should do.
There is no magic in the debugger, it don't know what your code is supposed to do, it don't find bugs, it just help you to by showing you what is going on. When the code don't do what is expected, you are close to a bug.
To see what your code is doing: Just set a breakpoint and see your code performing, the debugger allow you to execute lines 1 by 1 and to inspect variables as it execute.

Debugger - Wikipedia, the free encyclopedia[^]

Mastering Debugging in Visual Studio 2010 - A Beginner's Guide[^]
Basic Debugging with Visual Studio 2010 - YouTube[^]

Debugging C# Code in Visual Studio - YouTube[^]

The debugger is here to only show you what your code is doing and your task is to compare with what it should do.
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900