My task:


To calculate the values ​​using the formula (60 * 1,5 ^ (the value of TextBox-1)), which is located in the library and is described as:

public class Cost { public static int Cost_Of_MetalMine(int MM_Metal, int MM_Silicon) { return MM_Metal = (int)(60 * Math.Pow(1.5,значение TextBix - 1)); } } 

I need to pass the TextBox value to the formula. I am trying to do all this through a delegate, declare in the library:

 namespace SCore { public delegate int GetComboboxindex(int SelectedIndex); ... ... } 

You'll get a method to get the TextBox value into a separate class:

 namespace Sim { class Operations { public static void MFD_GetSelectedIndex(int SI_LevelOfBuilding, int SI_NameOfBuilding) { KalkulatorStoimosti chc = new KalkulatorStoimosti(); SI_LevelOfBuilding = int.Parse(chc.LevelOfSelectedBuilding.Text); // TextBox SI_NameOfBuilding = chc.SelectedIndexOfBuilding.SelectedIndex; // ComboBox } } } 

And pass it on:

 namespace Sim { public partial class KalkulatorStoimosti : Form { public int SI_LevelOfBuilding; public int SI_NameOfBuilding; public KalkulatorStoimosti() { InitializeComponent(); GetComboboxindex Get_Combobox_Index_Method = Operations.MFD_GetSelectedIndex(SI_LevelOfBuilding,SI_NameOfBuilding); Core.CoreMethod(Get_Combobox_Index_Method); } } } 

The library accepts:

 public static void CoreMethod (GetComboboxindex CallBackFinction_GetIndex) { CallBackFinction_GetIndex(); } 

I did not complete the passed arguments in the function because I got confused ... because here:

 GetComboboxindex Get_Combobox_Index_Method = Operations.MFD_GetSelectedIndex(SI_LevelOfBuilding,SI_NameOfBuilding); 

writes: implicit conversion of void to int is impossible. I do not understand how to correct this situation. I tried everything I knew ... Explain whether I'm on the right path? Do you do it right? How can I transfer the value of TxtBox to the library function? ...


UPD1 : @Flammable , thanks for the help, I would like to ask how to tie the switch to the library method? initialization of variables is needed for transmission, but using switch, variables are initialized only in it that is not right. What better way to do? @alexlz , thanks for the info. I'll try after I deal with this problem. I was interested;) I want to ask in advance that it is more productive to pass a parameter to a function or use

 tuple<T,T>` 

Library function:

 public static void Cost_Of_SelectedBuiding(out int MM_Metal, out int MM_Silicon, int SI_LevelOfBuilding, int SI_SelectedComboboxIndex) { //MM_Metal = (int)(60 * Math.Pow(1.5, SI_LevelOfBuilding - 1)); //MM_Silicon = (int)(15 * Math.Pow(1.5, SI_LevelOfBuilding - 1)); switch (SI_SelectedComboboxIndex) { case 0: MM_Metal = (int)(60 * Math.Pow(1.5, SI_LevelOfBuilding - 1)); MM_Silicon = (int)(15 * Math.Pow(1.5, SI_LevelOfBuilding - 1)); break; } 

    1 answer 1

    Wow ... Inside MFD_GetSelectedIndex you perform assignments to local variables. Obviously, you will not get any data from it. If you need to return SI_LevelOfBuilding and SI_NameOfBuilding, you should use parameter passing by reference using the ref / out keywords:

     public static void MFD_GetSelectedIndex(out int SI_LevelOfBuilding, out int SI_NameOfBuilding) { KalkulatorStoimosti chc = new KalkulatorStoimosti(); SI_LevelOfBuilding = int.Parse(chc.LevelOfSelectedBuilding.Text); // TextBox SI_NameOfBuilding = chc.SelectedIndexOfBuilding.SelectedIndex; // ComboBox } 

    Generally it is a bad design. It is not surprising that confused. Declare a library method like this:

     public static int Cost_Of_MetalMine(int MM_Metal, int MM_Silicon, int bindingLevel) { return MM_Metal = (int)(60 * Math.Pow(1.5, bindingLevel - 1)); } 

    and call something like this:

     var bindingLevel = int.Parse(chc.LevelOfSelectedBuilding.Text); var cost = Cost_Of_MetalMine(MM_Metal, MM_Silicon, bindingLevel); 
    • Spent another hour in stupid attempts to edit your code. But still did not understand what was happening. @Flammable, can you explain on the fingers of the call? Swears on this code that passes TB values ​​to the library function: GetComboboxindex GCBI = Operations.MFD_GetSelectedIndex (out SI_LevelOfBuilding, out SI_NameOfBuilding); on account of converting void to int - Sier
    • Come on first. As I understand it all: you have a library that contains some method. You need to pass int to this method, obtained from the textbox text. So? - nitrocaster
    • Yes. And there is. - Sier
    • one
      So do it: var textBoxValue = Convert.ToInt32 (textBox.Text); var result = ExternalLibraryMethod (textBoxValue); textBox is your textbox, ExternalLibraryMethod is a library method. - nitrocaster
    • 2
      You can pass parameters by reference, as advised by @Flammable, and transfer results by these links "backwards" (or "backwards") (invisibly to an inexperienced reader, almost in secret). And if you try to return not int but Tuple <int, int>? msdn.microsoft.com/en-us/library/system.tuple.aspx . Those. type of method Tuple <int, int> - alexlz