Home > Enterprise >  Why can't i use these Selected Indexes in a method? - HRK to EURO Currency Converter
Why can't i use these Selected Indexes in a method? - HRK to EURO Currency Converter

Time:10-24

Pretty new to C#.

I want to read the user input from a combo box and use the selected currencies in a method which converts one to another. The output isn't in console, it's on a Windows form.

This is my combo box input read code:

public void comboBox1_SelectedIndexChanged(object sender, EventArgs e)
{
    double first = comboBox1.SelectedIndex;
}

public void comboBox2_SelectedIndexChanged(object sender, EventArgs e)
{
    double second = comboBox2.SelectedIndex;
}

And this is my code that uses the selected indexes and converts them:

internal class Currencies
{
    public double[] Convert(double[] currencies, int amount)
    {
        double[] convertedAmount = new double[currencies.Length];

        if (Converter.comboBox1.first == HRK && Converter.comboBox2.second == EURO)
        {
            int exchangeRate1 = 1;
            double exchangeRate2 = 7.4;

            for (int i = 0; i < currencies.Length; i  )
            {
                convertedAmount[i] = amount * exchangeRate1 / exchangeRate2;
            }
        }
        else if (Converter.comboBox1.first == EURO && Converter.comboBox2.seocnd == HRK)
        {
            int exchangeRate1 = 1;
            double exchangeRate2 = 7.4;

            for (int i = 0; i < currencies.Length; i  )
            {
                convertedAmount[i] = amount * exchangeRate1 / exchangeRate2;
            }
        }
        else
        {
            throw new NotSupportedException();
        }

        return convertedAmount;
    }
}

Why doesn't the method let me use the variables "first" and "second" where I stored the selected indexes to convert the currencies?

CodePudding user response:

i think this is better for your question

CodePudding user response:

Sorry when problem I have
public string SelectedItem { get; set; } you must To Add new class such a utils and you must to { get ; set } every you have Concept This is better

CodePudding user response:

You can use this approach, this is what I always do in my apps

  1. Create three folders (ViewModel, View, and model)
  2. Install fody.propertyChange from Nugget
  3. Create a viewModel for your page
  4. Define a variable string SelectedIted {get; set;} in your viewwModel
  5. in the xaml code-behind for your page, put DataContext = new NameOfYourViewModel

Something like this


 <ComboBox
            Grid.Row="2"
            Grid.Column="1"
            Margin="10,0,10,0"
            HorizontalAlignment="Stretch"
            ui:ControlHelper.PlaceholderText="Select Lenguague"
            DisplayMemberPath="Value.Name"
            ItemsSource="{Binding LanguagesDictionary}"
            SelectedValue="{Binding SelectedItem}"
            SelectedValuePath="Value.Code" />

in Your VM

       public string SelectedItem { get; set; }

in your XAML code behind

            DataContext = new AudioPageViewModel();
  • Related