Click here to Skip to main content
15,885,366 members
Please Sign up or sign in to vote.
1.50/5 (2 votes)
Hi friends,

how can I implement this Pseudocode without return in c#(because I want to use it in button event handler)?

Thanks
large_int example(large_int u,large_int v)
{
  .
  .
  .//some codes
  .
      x = u % 10 ^ 2;
      y = u / 10 ^ 2;
       w = v % 10 ^ 2;
       z = v / 10 ^ 2;
  return example(x,w)+example(y,z)///it should gives sum of them as output
  //it means when example(x,w)+example() returns, x replace with u and w replace with v

}
Posted
Updated 19-Dec-10 19:32pm
v5
Comments
Abhinav S 20-Dec-10 1:21am    
Move this logic to the button click directly, You will not need a return statement - however it is much better to actually have this code in a separate method with a return statement as that results in better decoupling.
Sandeep Mewara 20-Dec-10 1:23am    
If it's in a button click event then you must be using the event for calculation to display or do something. What is that? You want to swap and do what? Just swap and leave?
arashmobileboy 20-Dec-10 1:33am    
i edited my question,it should sum two example()
Sergey Alexandrovich Kryukov 20-Dec-10 17:49pm    
Please, don't use such terminology "Pseudocode without return". Do you even understand what are you talking about? From the rest of your question, I think (but it was hard to understand what you talking about) you need something completely different.

(Such thing as code without return does exist, but -- forgive me please, I don't mean to be rude, I only want to help as you may need to ask other questions -- ...but I suspect it's a bit above your level of knowledge at the moment. Example is the method always throwing exception or even some system code cooperatively switching out of current thread or killing current thread and the like...)

Also, Abhinav S is absolutely right. You need to keep calculation apart from UI, so you really need separate functions with the profile like yours.
Sergey Alexandrovich Kryukov 20-Dec-10 18:00pm    
Oh, wait a second! By "without return", did you mean unlimited recursion?

This is different story. Absolutely eliminate unlimited recursion. Re-formulate the problem.
If you still need recursive step, re-define your method to do just one step. Define, what intermediate results you want.

Anyway, edit your question to resolve the controversy.

1 solution

WinForms, yeh? And I guess you mean .NET v.4 and System.Numerics.BigInteger, right?
(There is no such type as "large_int", so I did not know what to think about. No matter, use the type you want: the rest of the code will be exactly the same; see below.)

Then, instead of you pseudo-code you'll have something like this:
C#
System.Numerics.BigInteger Example(System.Numerics.BigInteger u, System.Numerics.BigInteger v) {
    //...
    return u + v; //in fact, whatever you want to calculate
    //I put this line just to compile
}


Ok, then you suppose to have some 3 controls to hold string representation of your big integers, plus your button:

C#
TextBox
    TextBoxLeftOperand,
    TextBoxRightOperand;
    TextBoxBigIntegerTestResult;
Button MyButtonTextBoxBigIntegerTest;


And you will need a code for a hander:
C#
void BigIntegerTestClickHandler() {
    System.Numerics.BigInteger u =
        System.Numerics.BigInteger.Parse(TextBoxLeftOperand.Text);
    System.Numerics.BigInteger v =
        System.Numerics.BigInteger.Parse(TextBoxRightOperand.Text);
    TextBoxBigIntegerTestResult.Text = Example(u, v).ToString();
}


Expect getting exceptions thrown from the code above.

Finally, you'll need to setup your button event. You can do it right after the form is constructed (or later, but certainly before the user can actually click this button):

C#
MyButtonTextBoxBigIntegerTest.Click +=
    (sender, args) => { BigIntegerTestClickHandler(); };


My advice: always use the syntax above, not the gravely obsolete syntax used in Microsoft's auto-generated code. The benefits: you don't have to specify exact type for event handler arguments, which is especially good if you're not using the argument (very typical case); also, you're not bound to having same arguments in the handler (otherwise you would have to specify the arguments sender and args even if you're not using them). Finally, you're allowed to avoid writing a hander like BigIntegerTestClickHandler as a separate method at all; instead, you could simply put a body of this method directly between curly brackets of the above codelet.

Note: Perhaps, the numeric part is overkill; and you meant some simple integer type (BigInteger is extremely sophisticated type supporting unlimited presision. (And be ready to hang your computer by certain operations with really big intermediate numbers.)). Not to worry. Just use the same code and replace System.Numerics.BigInteger with whatever type you want.

That's it.
Is this something you wanted?

Please do not down-vote this answer because it does not answer the question posed!

I think nobody can say for sure what the author of the question means. This is just an introductory suggestion in hope it can be helpful. As soon as author of the question answers my questions and resolve the controversies, I'll be able to give a final answer.

 
Share this answer
 
v11
Comments
Espen Harlinn 26-Feb-11 10:59am    
Good effort, my 5
Sergey Alexandrovich Kryukov 26-Feb-11 19:50pm    
Thank you.
--SA

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