i have a piece of code that is to be repeated several times throughout my form when certain buttons are pressed. on the clicking of the buttons it should be adding to the decimal value inside the textbox by the value that varies from button to button.my code so far looks like this:
Function EditPrice(ByVal a As Decimal, ByVal b As Decimal)
Dim num1 As Decimal
num1 = a + b
Return num1
End Function
Private Sub btnMinusMushroom_Click(sender As Object, e As EventArgs) Handles btnMinusMushroom.Click
EditPrice(CDec(txtEditCost.Text) + "0.5")
End Sub
if that isn't clear what the code should be doing, the variable of "a" in the function "EditPrice" is the decimal value inside the textbox "txtEditCost" and the decimal value of .5 is the value that should be added to the textbox value.
the function name is underlined red saying "argument not specified for parameter 'b' of 'public function EditPrice(ByVal a As Decimal, ByVal b As Decimal) as object' ".
i dont know what that means or how to fix the problem, any ideas?, thanks
There are a number of issues with your code. To help you avoid such issues in future, I strongly suggest that you set Option Strict On. That will disallow narrowing implicit conversions and late-binding, which will force you to give more consideration to the data types you're using. You can set it On for the current project in the project properties. You can also set it On in the IDE options so that it will be On by default for all future projects.
The first issue is one that Option Strict On would flag, i.e. you have declared a function without a return type. This:
Function EditPrice(ByVal a As Decimal, ByVal b As Decimal)
should be this:
Function EditPrice(ByVal a As Decimal, ByVal b As Decimal) As Decimal
The second issue relates to the naming of that function. You have named it EditPrice but, if you look at the actual code of the method, the name has nothing at all to do with it. There's no price there and there's no editing. All it does is add two numbers of type Decimal and return the result. There's nothing about that that requires the numbers to represent a price and nothing is edited because nothing is changed by the method.
The third issue is with how you're calling the method. The method is declared as taking two Decimal arguments yet what are you doing?
EditPrice(CDec(txtEditCost.Text) + "0.5")
How does that make sense? You get a String from a TextBox and convert that to a Decimal and then you add another String to that and then pass that single result to the method. That's all kinds of wrong. Firstly, why are you adding a String to a Decimal? If you want to add the number 0.5 to something then use a number, not a String. If you want to add to a Deciaml then use a `Decimal number. Secondly, why are you adding the two numbers there and passing the result to the method when the whole point of the method is to add the two numbers? That code should be more like this:
EditPrice(CDec(txtEditCost.Text), 0.5D)
That's still a problem though because, as I said, the EditPrice method doesn't actually edit anything. It just adds two inputs and returns the result, but you don't actually use that result. You presumably want to display that result somewhere. If it's in the same TextBox again then you need to do that:
txtEditCost.Text = EditPrice(CDec(txtEditCost.Text), 0.5D).ToString()
Note the ToString call there too. That will be required by Option Strict On because that Text property is type String and that method returns a Decimal.
The code is still a bit dodgy even with those changes. What's the point of a method that adds two numbers and returns the result when you could do that inline with a simple addition operator? If a method is to be named EditPrice then it actually ought to edit a price. In that case, it would make more sense to have it take a single number to add to the current price and then have the method make the change, e.g.
Private Sub EditPrice(valueToAdd As Decimal)
Dim currentValue As Decimal
'This will get the current value or zero if there is no valid value.
Decimal.TryParse(txtEditCost.Text, currentValue)
Dim newValue As Decimal = currentValue + valueToAdd
txtEditCost.Text = newValue.ToString()
End Sub
Note that that is a Sub rather than a Function because it actually makes a change rather than return a value that other code uses to make a change. In this case, the name actually describes what the method does. Method names should ALWAYS describe what the method actually does.
Related
The code I use is the following:
' (is on top of the listing directly after Public class form1)
Dim Score as Integer
...
Score=Val(Txtbox5.text) "Txtbox5 is control where I put in the value."
Lbl2.text=score "Lbl2 is the label where the score must display."
Txtbox5.text="" "Makes the Textbox empty."
Then the problem occurs: when I enter a new value in Txtbox5.text the I want this value update
the value in the label with the value that is already in the label by the score.
The code I use therefor is Lbl2.text=score +score but whats happend is the score in the label is double???
So what I want is: when I have a value in the label say 2 and I insert a new value in the textbox
say 3 then I want to see in the label a value of 5.
But I have tried everything but nothing works
Is there somebody who can help me with this problem??
If I understand you correctly you want to sum the current value of the TextBox5 to the Score variable and then update the label
This could be simply resolved without any thinking with
Score += Val(Textbox5.Text)
Lbl2.text=Score
Txtbox5.Text=""
But you could meet some problems because in your textbox the user can type anything, not just a number. In that context the Val function from the Microsoft.VisualBasic compatibility assemble doesn't tell you anything, simply converts the non numeric string to zero. This could be acceptable or not depending on your requirements. If you want to have a message for your user when the input is not good you could write
Dim currentInput as Integer
if Not Int32.TryParse(Txtbox5.Text, currentInput) Then
MessageBox.Show("You should input a numeric value!")
Else
Score = Score + currentInput
Lbl2.text=Score
Txtbox5.Text=""
End If
I have tried everything but nothing works
That's not strictly true..
Lbl2.Text = (Convert.ToInt32("0"&Lbl2.Text) + Convert.ToInt32(Txtbox5.Text)).ToString()
ps;
Your life would be a lot simpler if you used a couple of numericupdown controls, one readonly and no border etc to look like a label. Then life would be like:
resultNumericUpDown.Value += inputNumericUpDown.Value
Whenever you're asking the user for numbers, using a NUD saves a lot of headaches
I saw this line of code txtNewCaption.SetFocus and was told that SetFocus is used as a sub
here but it is also a function. I also read online that the difference between a sub and a function is that a sub doesn't return any value but a function returns a value. I couldn't imagine what kind of value SetFoucs could return, so I searched quite some articles online about SetFocus but none of them gave me an example of SetFocus used as a function to return a value. I am assuming there is something inaccurate or wrong with either my understanding or what I was told.
Could you please help me to clarify the confusion?
Thank you!
Well, in this case and example, .setFocus is not a sub, nor is it a function.
A sub is a separate bit of code, you call it like
Call MySub
(or a shorter form skips the word "call" - it is optional)
eg:
MySub
Or, if there are parameters, you might go:
Call MySub(InvoiceNumber)
In the case of a function? That is again a external bit of code. You can use functions that return a value and IGNORE that return value.
So
MyVar = MyFunction()
And just like subs, the function can accept parameters:
MyVar = MyFunction(InvoiceNumber)
And as I noted, you can "ignore" the return value of a function like this:
MyFuncton InvoiceNumber
If you write as per above, the you are passing the value, but the return value of the function is not "placed" or put into anything. so you can "use" a function that returns a value. You also skip use of the () around the value you pass.
However, in the above two examples we are talking about VBA sub and functions YOU write.
SomeTextBox.SetFocus is NOT sub, nor is it a function.
What the above is called? It is called a Method of the object.
So, when you use Access objects, say record Sets, controls etc? Well that "object" will have what we call properties (usually things that you can set or get a value from).
So, for a text box, you have:
MyControl.Value = "Hello"
The above would set the value (text) of a text box to Hello. Thus ".value" is the property of that text box. It also happens to be the default property. So you can go:
MyControl = "Hello"
Now, if you go:
Msgbox("Value of MyControl = " & MyControl.Value)
So a property of a "thing" (a object) often can be set a value, and you can get (retrieve) a value. This is called a property.
However, these objects also have what are called methods. Methods are a "action" and are similar to a sub/function (but YOU did not write that method).
And such methods are "some code" that runs that is part of the object. So sometimes the difference between a property of a object, and a method of a object is "gray". Some methods can accept values and return values, but the distinction between the two (method and property) is that MORE then setting a value can occur with a method. (so it not just limited to get and set).
A "method" can be thought of some "action" and code. Use of that Method tends to be assumed that some code will run. (and it not your code in most cases - it is "code" attached to the built in object.
So
MyTextBox.SetFocus
It is a method of the text box control, or the given control in question. Not all controls necessary may have this "method".
So .SetFocus is not sub, not a function, but is in fact a method of the control in question.
Functions don't necessarily have to return a value. You can call functions just like you can call subs. Try for yourself: write a function, step through the function in a sub, and then check your locals window.
Functions have the added bonus of being able to return values, but don't have to.
EDIT: Aside from the need to return a value, I'm sure there is a reason to use one over the other, but I generally determine whether to use a sub or a function by the complexity of the task. I use functions to perform simple tasks and subs to perform more complex ones.
If you search the Microsoft Documentation (MSDN) for SetFocus, you will find it defined as:
method on a user-interface control object, moving the focus to it ... with no return value
(above quotation is not literally cited, but conveys the meaning)
SetFocus Method [Access 2003 VBA Language Reference]
TextBox.SetFocus method (Access)
SetFocus method (Microsoft Forms)
Function?
Thus it is not a function in VBA syntax.
Besides: a function's return value in VBA is optional. See VBA language reference: Function statement:
Optional. Return value of the Function.
Common method naming conventions
In most programming languages the common naming convention (case ignored) for methods apply:
use get (often without parameters) to return a value. Such methods are called getter or accessor.
use set (often with parameter as value to set) to set a new state (or value). Usually the have no return value, because the new value or state is predicted by the argument passed (their parameter). Such methods are called setter or mutator.
use is, has, exists, contains, starts, ends, etc. to return a boolean value. Such methods are used to check flags (on/off, true/false, yes/no).
use calculate, locate, indexOf, find, fetch, lookup, determine, etc. (often with parameters) to return a value resulting of some processing based on specified parameters (by some logical, mathematical algorithm or queried by data-retrieval)
I have the following:
=Round(ReportItems!Textbox47.Value
+ CDbl(ReportItems!Textbox218.Value)
+ CDbl(ReportItems!Textbox222.Value)
+ CDbl(ReportItems!Textbox226.Value)
+ CDbl(ReportItems!Textbox230.Value)
+ CDbl(ReportItems!Textbox234.Value),2,MidpointRounding.ToEven)
This is saved in the Textbox called ReportItems!TotalSaves.Value
This TextBox is part of DataSet1. What I want to do is call that TextBox in another DataSet.
I tried a Lookup and it does not work. I tried to save the value of that TextBox in a global variable but it does not take Items in the report collection.
EDIT1:
I tried to do the following:
Public Function GetValue(Byval value as Double)
prueba = value
End Function
Public Function Value() As Double
Return prueba
End Function
I called the function GetValue inside the Dataset1 where ReportItems!TotalSaves.Value exists in an expression:
Code.GetValue(ReportItems!TotalSaves.Value)
And then call the Value function in the Dataset2
Code.Value()
But it returns 0 so there is no value saved, maybe it is saved but for some reason Value does not get the value that it is saved in the GetValue function, even if it is the same variable.
EDIT2:
I tried to do the changes Alejandro told me:
But:
In English:
"The expression value for the TextBox50 make a reference to an element "TotalFrecPromVentas" (This is the textbox that exists in Tablix1/dataset1 that does not exists in Tablix2/Dataset2),the element expressions of the report can only reference to another elements of the same group.
You can reference a textbox in another dataset by using the ReportItems!TotalSaves.Value you identified in your question.
For example if I have a text box that sums the values in a column as follows
I can reference this value from another table using a different dataset as follows
This would result in the following output, in this case a very simple definition of the 6 times table
Is this the sort of solution you were looking for? If you need further clarification, or a different approach then let me know.
Try change:
Dim public total as Double
Public Function GetValue(ByVal numero as Double ) AS Double
total = numero
return total
End Function
In the selected textbox named Mytextbox I put manually a double value: =12.456.
The textbox in red rectangle is the textbox where I want to place the
referenced Mytextbox value
So in the textbox I put the following expression:
=Code.GetValue(ReportItems!MyTextbox.Value)
You have to put =Code.GetValue(ReportItems!TotalSaves.Value).
Note I am not using Value() function. My function just returns the value of the passed parameter.
Don't use Value() function use my code.
It worked for me. Let me know if it could help you.
i have a report with parameter is the year i can select a multiple value: for example i selected 2005,2006,2007
when i click in view report i get this result
i added some custom code to add value in arraylist
Dim values As System.Collections.ArrayList
Function AddValue(ByVal newValue As Integer)
If (values Is Nothing) Then
values = New System.Collections.ArrayList()
End If
values.Add(newValue)
End Function
Public Function GetArray(Item as Integer)
return values(Item)
End Function
i added my code in my matrice this is the result
The first row i got the right anwer but the other rows are false answers this is the resut what i need
Without RDL details regarding your expressions - it is difficult to answer your question. What are your expected results? What is the exact input and output you are expecting? Please provide samples.
In this scenario, it seems the issue is in your custom code. Once you return the result at the end of first row. The variable(called "result") keeps the "66" and it was never overwritten so that it always shows "66". In Reporting Services, it generates cells from left to right, top to bottom. So you need to pass the first column (HP, DELL, Acer) as argument in your function and use a variable (called "previous")to receive this value. Always do the judgment at the beginning of the function, if the passing argument is not equal to the "previous". You need to clear the "result" variable.
I'm trying to make a NumericUpDown box set the decimal points of a textbox and update everytime it is clicked up or down. I have tried it 2 different ways:
Me.txtCalc.Text = FormatNumber(Me.txtCalc.Text, Me.NumericUpDown1.Value)
This way works but it isn't keeping the decimal values when i increase the value. So if the textbox said 2 and is really supposed to be 2.987899. I change the NumericUpDown box from 0 to 6 and all it does is display: 2.000000.
I've also tried it this way (Which i think I'm doing wrong..)
Me.txtCalc.Text = Math.Round(CDec(Me.txtCalc.Text), CInt(Me.NumericUpDown1.Value))
This was the only way I could get it to do anything. And when I try this, everytime I click the vlaue to go up, it does absolutely nothing. But if I set it to 7 first and then add some values like: 2.987899; when i click from 7 down to 1, it removes one point at a time like it should... it just doesnt go back up. at all.
I assume I have to use Math.Round because that's the only way it will keep the decimal values...
Hopefully I explained this so you guys understand.
Any help would be appreciated.
Me.txtCalc.Text = FormatNumber(Me.txtCalc.Text, Me.NumericUpDown1.Value)
this not only formats the value, it resets the starting value for any next iteration. Assuming a start of 2.987899 going down in decimal places changes the VALUE:
2.98789
2.9878
2.987
..
2.
When "2" gets assigned to the TB, the starting value for going up is 2. you lost the rest of the decimals. You need to store in a decimal variable and format THAT to go up and down as you want:
Dim decVal As Decimal = 2.987899
Private Sub NumericUpDown1_ValueChanged(ByVal sender As System.Object, _
ByVal e As System.EventArgs) Handles NumericUpDown1.ValueChanged
' now, I am not changing the underlying VALUE, just the format
TB1.Text = FormatNumber(decVal, NumericUpDown1.Value)
End Sub
I'm not sure how you get different results with Math.Round because the code shown will still assign a new value to the TB with fewer decimals and therefore lost.