I know the logic, but its killing me why this isn't working
Basically I have a program thats supposed to gather information, both as string and as double.
There are to be two sub procedures used
One for gathering input and another for printing that input out into a listbox
I'm not sure how to pass the information that I got using Input() sub procedure to Output() sub procedure.
I know once I have it passed its a gravy train, all I need to use is ListBox1.Items.Add function to print what I want in the listbox
However I'm getting errors like "Error 1 Too many arguments to 'Public Sub Input()'. C:\Users\Documents\Visual Studio 2008\ProjectsForm1.vb 16 15 Lab6
"
So the way he wants me to do this, I can't just declare variables on the class level as far as I'm aware. The input() sub procedure (which I have to declare myself, it's not built in) has to pass data to output() sub procedure again which I have to declare) and then I can get it from there.
There are several variables
Organization Visited, Dates, and Location are all string variables
Expenses for meals and entertainment, airline fees, lodging, and taxi fares are all variables defined as Double
heres my source code
Public Class Lab6
Private Sub btnCalculate_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnCalculate.Click
lstResults.Visible = True ' list box is only to appear once button pushed
lstResults.Items.Clear() ' clears out previous results
Input()
Output()
End Sub
Sub Input() ' gathers all the information the form asks for and assigns
' it to variables so that sub Output() can read the variables
' and then print them in the list box
End Sub
Sub Output() ' prints out items gathered from sub Input()
' * note that meal and entertainment expenses are only
' 50% decutable, not the whole amount
' Calculation must be made where 50% meals and entertainment expense = CDBL(meals and entertainment expenses) * .50
End Sub
End Class
Re: I know the logic, but its killing me why this isn't working
Use a different name besides Input and Output.... Input at least is actually a keyword in VB, and that may be causing a conflict.... You could try GetInput and SetOutput for your sub names
-tg
Re: I know the logic, but its killing me why this isn't working
Ok here is my source code now, I have changed the sub procedure names.
As you can see I have declared seven variables... the first 3 are string variables as they relate to "Organization visited", "Dates", and "Location" and the next 4 are are double as they are "Expenses for meals and entertainment", "Airline Fee's", "Expenses for lodging" and "Taxi Fares"
I have just assigned the first 3 varibles A,B,C as String = TextBox1.Text (2 and 3 for B and C)
The next 4 I have assigned as Double using D,F,G,H as Double = CDBL(TextBox4.Text) (5,6,7 for F,G,H respectively)
Now that I have gathered values for the variables in the sup procedure GatherData how do I pass that so that Sub ResultsShown can have access for me to do some calculations and print data in a list box using those variables. IE something like I wanted that ResultsShown to take the variable D and multiply it by .50 (because only 50% is deductible) and then print that in the list box
Right now, if I use ResultsShown(A,B,C,D,F,G,H) it tells me
"Error 1 Too many arguments to 'Public Sub ResultsShown()'. C:\Users\Documents\Visual Studio 2008\Projects\Form1.vb 15 22
"
Public Class Lab6
Private Sub btnCalculate_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnCalculate.Click
Dim A As String
Dim B As String
Dim C As String
Dim D As Double
Dim F As Double
Dim G As Double
Dim H As Double
lstResults.Visible = True ' list box is only to appear once button pushed
lstResults.Items.Clear() ' clears out previous results
GatherData(A, B, C, D, F, G, H)
ResultsShown()
End Sub
Sub GatherData(ByVal A As String, ByVal B As String, ByVal C As String, ByVal G As Double, ByVal H As Double, ByVal D As Double, ByVal F As Double)
' gathers all the information the form asks for and assigns
' it to variables so that sub ResultsShown() can use the variable
' and then print them in the list box
A = TextBox1.Text
B = TextBox2.Text
C = TextBox3.Text
D = CDbl(TextBox4.Text)
F = CDbl(TextBox5.Text)
F = CDbl(TextBox6.Text)
H = CDbl(TextBox7.Text)
End Sub
Sub ResultsShown() ' prints out items gathered from sub GatherData()
' * note that meal and entertainment expenses are only
' 50% decutable, not the whole amount
End Sub
End Class
Re: I know the logic, but its killing me why this isn't working
this looks like dot net code, ask a mod to move to correct forum
Re: I know the logic, but its killing me why this isn't working
a couple of things.... first for the sake of sanity - yours as well as anyone else... try giving the variable meaningful names....not jsut A B C D, etc... call them what they are, it'll make debugging easier later - trust me.
Next.... I would declare the varaibles as private module level variables... and not inside the click sub....plus then you don't need to pass them around, they are just there....
lastly, in your gaterdata sub, since you are assigning values to the variables, you should pass them as ByRef rather than ByVal.... otherwise, when you get back to the calling sub.... their values will be gone. But then, if you declare them at the module level, you don't need to pass them around and this becomes moot.
-tg
Re: I know the logic, but its killing me why this isn't working
I had given the variables meaningful names, I just changed them to make it easier since I totally re wrote this code like 10 different times with none of them working. They will all be changed back to the text values "Organization attended" "Date" "Location" "Expenses of meals" "Airline Fee" "Lodging Fee" "Taxi Fee" after I have the code itself worked out... thats an easy thing to do.
from what you say at a module level... I take this as making them global variables since this program has no other procedures then the ones I have declared now.
The point isn't to just assign the variables and write everything inside the one private sub when the button... the point of using the sub procedures GatherData and ResultsShown to get data entered into the form and storing it as variables and then using another sub to read those variables, do a calculation and print values in a listbox. Using the subs would be pointless for this if they weren't gathering information and storing as variables.
And yea, the ByRef thing was just an oversight... this program has me a little ticked off... since I know what I want to do is easy I just can't seem to make it work.
Re: I know the logic, but its killing me why this isn't working
Quote:
Originally Posted by brkick2005
I had given the variables meaningful names, I just changed them to make it easier since I totally re wrote this code like 10 different times with none of them working. They will all be changed back to the text values "Organization attended" "Date" "Location" "Expenses of meals" "Airline Fee" "Lodging Fee" "Taxi Fee" after I have the code itself worked out... thats an easy thing to do.
OK.
Quote:
Originally Posted by brkick2005
from what you say at a module level... I take this as making them global variables since this program has no other procedures then the ones I have declared now.
Not global... they would still be private, but declared at the top of the code, before any subs... it makes them available to any running code inside the file.
Quote:
Originally Posted by brkick2005
The point isn't to just assign the variables and write everything inside the one private sub when the button... the point of using the sub procedures GatherData and ResultsShown to get data entered into the form and storing it as variables and then using another sub to read those variables, do a calculation and print values in a listbox. Using the subs would be pointless for this if they weren't gathering information and storing as variables.
I understand the point of the subs, and I don't think I ever mentioned getting rid of them. if this is in reference to what I think, what I meant was that if you use module-level variables, you wouldn't need to pass them around as parameters to the subs... that's not to say don't use the subs, or you wouldn't need them.... what you wouldn't need is the parameters.
Quote:
Originally Posted by brkick2005
And yea, the ByRef thing was just an oversight... this program has me a little ticked off... since I know what I want to do is easy I just can't seem to make it work.
It comes with experience and learning some of those quirks... I've been doing this a long time, and I still get tripped up on some of the simple things.
-tg
Re: I know the logic, but its killing me why this isn't working
Quote:
Originally Posted by westconn1
this looks like dot net code, ask a mod to move to correct forum
While correct (look at the sub start lines) the code within the sub is not specific to VB.NET and there is no reason why he shouldn't be able to ask here as it doesn't use anything not present in VB6 :-)
Re: I know the logic, but its killing me why this isn't working
Thread moved from 'VB6 and Earlier' forum to VB.Net (VB2002 and later) forum
Re: I know the logic, but its killing me why this isn't working
So you have a class, declare the variables as such:
Code:
Public Class lab6
Private A As String
Private B As String
Private C As String
Private D As Double
Private F As Double
Private G As Double
Private H As Double
'Now for the sub
Sub GatherData()
dim d2 as double
' gathers all the information the form asks for and assigns
' it to variables so that sub ResultsShown() can use the variable
' and then print them in the list box
A = TextBox1.Text
B = TextBox2.Text
C = TextBox3.Text
If Double.TryParse(Textbox4.Text, d2)
D = d2
Else
Windows.Forms.MessageBox.Show("That's not a number! Shape up!")
End If
If Double.TryParse(Textbox5.Text, d2)
E = d2
Else
Windows.Forms.MessageBox.Show("That's not a number!")
End If
If Double.TryParse(Textbox6.Text, d2)
F = d2
Else
Windows.Forms.MessageBox.Show("That's not a number! Shape up!")
End If
If Double.TryParse(Textbox7.Text, d2)
G = d2
Else
Windows.Forms.MessageBox.Show("That's not a number! Shape up!")
End If
If Double.TryParse(Textbox8.Text, d2)
H = d2
Else
Windows.Forms.MessageBox.Show("That's not a number! Shape up!")
End If
End Sub
End Class
With that, the button click event can call the sub and it will fill in all the variables. The variables are not global, but declared private such that they are visible to all methods of the form, but not to anything outside the form. I also changed your CDbl to the safer Double.TryParse which will not throw an exception if the user either leaves a textbox empty, or puts some characters or non-numeric symbols into the textbox (such as dollar signs).