Click here to Skip to main content
15,906,094 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi Got interface IMathOperationDemo where are defined the methods of class Normale.

C#
IMathOperationDemo CurrentOperation;
       private void comboShperndarjet_SelectedIndexChanged(object sender, EventArgs e)
       {
           switch (comboShperndarjet.SelectedItem.ToString)
           {
               case "Normale":
                   decimal Alpha = numericUpAlpha.Value;
                   CurrentOperation = new Normale();
                   break;


In the main form got a combo-box and want to fill some textbox
by accessing class Normal methods inside the case Normale.

How can I make this possible?

Regards
Posted

The mere fact that you use an immediate string constant (hard-coded) in a switch statement makes your case nearly hopeless. This style has nothing to do with real programming. You also did not show the class Normale and explain what the combo box list items correspond to.

(One of acceptable solutions: store non-string descriptive data in combo-box, override object.ToString to show as string in the combo box; I don't want to give more detail because you question is not properly formulated).

—SA
 
Share this answer
 
v2
Comments
KORCARI 8-Jun-11 14:31pm    
I have :

class Normale : IMathOperationDemo
{
decimal Alpha;
decimal Beta;
string mesatarja;
string moda;
string mesorja;
string varianca;
string Minimum;
string Maximum;

public string getMinimum(string Minimum)
{

return ((Alpha)).ToString();

}
public string getMaximum(string Maximum)
{

return ((Beta)).ToString();

}

public string GetMesatarja(string mesatarja)
{
return (((Beta + Alpha)/2).ToString());

}
public string GetMesorja(string mesorja)
{
return (((Beta + Alpha) / 2).ToString());
}
public string GetModa(string moda)
{
return (((Beta + Alpha)/2).ToString());
}
public string GetVarianca(string varianca)
{
return (((Beta - Alpha + 1) * (Beta - Alpha + 1) - 1) / 12).ToString();
}

}
}
And combo items:
comboShperndarjet.Items.Add("Normale");
comboShperndarjet.Items.Add("Binomiale");
comboShperndarjet.Items.Add("Puason");
comboShperndarjet.Items.Add("Gjeometrike");

How can I make combo work with a normal switch-case and by Selecting one of items get use of class normal?
regards
Sergey Alexandrovich Kryukov 8-Jun-11 19:05pm    
I saw this code after I posted this answer. In my other answer I tried to explain why it does not make sense. Could you move this code from here to answer using "Improve answer". Now, you did not explain how this code is should be related to the combo box. Should each instance of combo box be associated with and instance of this class? If so, how come one item text in combo box is the same as type name? Or other items are other types?

Anyway, the solution it to add class (structure) instances to the combo box list, not strings.

Perhaps if you explain your ultimate goal I would be able to help you more.
--SA
Monjurul Habib 8-Jun-11 18:16pm    
nice advice, my 5.
Sergey Alexandrovich Kryukov 8-Jun-11 19:10pm    
Thank you, Monjurul.
--SA
KORCARI 9-Jun-11 11:01am    
every item of combobox should direct to a different class.
for now item Normale should direct to class Normale because I am testing only one class.Classes will have their own methods and identical ones.
Regards and thx
This seems to be a perfect use case for the strategy pattern:Wikipedia[^]

I would probably turn the Strings into constants. Then you can have a dictionary with the mapping String Constant -> Implementation Class (in your example: "Normale"...). It is important that they all have a common superclass (or Interface for that matter). That way you have a Dictionary<IMathOperationDemo>. It looks like, that you already have this interface, which is good.

Thinking about it, you could just do it the way you did up there. Just pass Alpha and Beta in the Constructor. You don't need the dictionary I suppose. Just turn the Strings into Constants!

Why are there string arguments in all the methods? You do not use them...
 
Share this answer
 
v2

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