[RESOLVED] Supply function at run-time
Hello.. Please help...
My problem is that I need to have a function contained in module 'jfunction.bas' to be supplied by the user at run time. The module reads in a function F(I) in terms of variables X(I,... ):
' ------------------------------------------------
Public Sub JFUNC(N, M, K, X(), F(), I)
F(I) = X(I, 1) * X(I, 2) * X(I, 3)
End Sub
' ------------------------------------------------
Is it possible to make this module show up during execution and then be edited by the user? (Note: the user can edit only line 2 and can type in any combination of the Xs, leaving the I index intact.)
Joe Back
Re: Supply function at run-time
You can't edit the bas module at run time as it's part of the executable. That function looks awfully familiar though... If it always runs in the sequence "= X(I, 1) * X(I, 2) * X(I, 3)...." and the user has to stick with the sequence up to a chosen value i.e "....X(I, 47) * X(I, 48)", then you could put a textbox in your prog, where the user simply puts in "48" and your prog does the rest. What is that math function ?
Re: Supply function at run-time
There is another alternative if you aren't using a set lower/upper bound or it doesn't follow conventional numbering. If you had a textbox where the user entered numbers with commas inbetween like "1,10,12,30,50,65" the program could split() using the commas and then make use of the numbers inputted :-)
Re: Supply function at run-time
Thanks for your replies. However, the function could be anything, and not just a straight product of Xs. For example, the user should be able to input any combination of Xs (linear or nonlinear), leaving 'I' intact. Possible functions could be:
F(I) = 1 - SIN(X(I,1)) * COS(X(I,2))
or
F(I) = 2* X(I,1) - X(I,2)*X(I,3) + 6*X(I,4)^2
or even
F(I) = X(I,1) + X(I,2)/X(I,3) + X(I,4)*X(I,5)/X(I,6)
As you see, any function is possible, as long as 'I' remains as is, and the numbers 1,2,....n indicate each independent variable.
Evidently the function cannot be placed in a text box, as in that case it would be read as a string! This is why I placed it in a 'bas module'. Can this module be left apart from the executional program and be appended (invoked somehow) to the program at run time? Any ideas?
Re: Supply function at run-time
Can this module be left apart from the executional program and be appended (invoked somehow) to the program at run time? Any ideas?[/QUOTE]
the code looks simple enough to re-do in VBScript.
you could probably use webbrowser, or scripting host thingie to perform the function in script.
but i've only been doing vb for 2 months, so what do i know?
$0.02
Re: Supply function at run-time
Looking at what you've put there, as there's no set format, you'd have to parse the string, rebuilding it with math functions as you go along. Nigh on impossible if you don't know what the user's going to put in - including typos. As rnd_me points out, you could try vbscript, or even, if you trust your users, the "Visual Basic 2005 Express Edition" which is supposed to be free from the MS website. I don't know how functional it is, but your users could edit the bas module and run it in the IDE. (but you might have to teach them how to use it... :cry: )
Re: Supply function at run-time
I am using VB6, so I don't know if VB2005 can help. This is a school program that does optimization, and the original function posted is the familiar 'Post Office Problem' to maximize. The users will of course be told via a pop up form how to edit the function.
I am a good math programmer but I am not familiar with VB scripts. Can you tell how to workout a script and then link it to my main program?
Thanks again...
Re: Supply function at run-time
Quote:
Originally Posted by Joe Back
Can you tell how to workout a script and then link it to my main program?
well, the script simply goes in an HTML file, which anyone can edit in notepad.
i am unsure how to actually work in the code to the app. i bet someone around here knows, or perhaps this has been covered...
Re: Supply function at run-time
I am thinking about a form with many textboxes...one for X, one for Y etc, and one for the formula. You could then run multi passes on the formula, simplifying it at each pass. Let's say you had (making this very simple)
(X * 5) + (Y * X) -((X*2) /Y)
And let's say X=10 and Y=5
First pass would be on the Xs...would turn it to
(10 * 5) + (Y * 10) -((10*2) /Y)
Which can now be turned into
50 + (Y * 10) -(20 /Y)
so now we run the Y on it
50 + (5 * 10) -(20 /5)
Then refilter
50 +50 - 4
Thus (X * 5) + (Y * X) -((X*2) /Y) = 96 if X=10 and Y = 5
This obviously doesn't cater for any errors made by the user, and users are infinitely stupid ("Press any key to continue...Where's the 'any' key?") but that's your problem not mine :-P
what about several combo boxes?
your could practically remap every mathematical function if you needed to. a couple text boxes for digits, combos for ops.
is this going to be a traffic intensive routine?
Re: Supply function at run-time
I agree with everyone else. Yes, it's possible but it would require a lot of code. You would basically have to write an interpreter that would read the line of text. Break it down into functions and calculate the math for each arthimatic function.
You could make a limited program by using the textboxes for the value and maybe in between the textboxes, you could place the available function. Or maybe the functions below the textboxes. Again, it would probably be to rigid.
Re: Supply function at run-time
Actually........ this looks like it would be more suited to Excel than pure VB.
Now (you can blame smUX for this *suggestion* - a query in another thread has brought this to mind! ;) ) you can embed an Excel worksheet in a VB app by using the OLE container control. You've then got access to all it's math functions and flexibility. All it takes is 3 (?) mouse clicks. The only problem would be that the users would have to have access to Excel - either over a network (you mentioned a school!) or installed on each PC.
This would have the advantage of the user being able to split up (for clarity) and type out their formula as text strings ie. "F(I) =" in cell A1, "1 -" in cell B1, "SIN(X(I,1))" in cell C1, etc. before typing in the actual formula (after editing) in, say, cell A3.
They could even have multiple solutions on a single spreadsheet, print it, add their name, address and credit card numbers :bigyello: ..... and more! (and you'd still get the credit for writing the app!) :lol:
On the surface, it seems a ready-made solution. You'd certainly get to the pub sooner :D . Any thoughts ?
Edit:-
....save it to a file, get to the pub sooner, modify it on the fly, get to the pub sooner, have everyone asking "How did you do that ? nice one!", get to the pub sooner...... :bigyello: