implicit conversion from double to string vb.net - vb.net

I try to run this code but iget the message implicit conversion from double to string vb.net i try different conversion but its the same
Private Sub TxtRemise_TextChanged(sender As Object, e As EventArgs) Handles TxtRemise.TextChanged
Dim TotalTTc As Decimal
TotalTTc = CDec(TotalHT.Text) + CDec(TXTMontantTva.Text)
TxtTTC.Clear()
TxtTTC.Text = TotalTTc) - val(TxtRemise.Text)
End Sub

You perform a mathematical operation, which will obviously result in a number, and you then assign that to the Text property of a TextBox, which is type String. If you want to assign to a String property then you need to assign a String. If you have a number, that means calling ToString on it.

I checked your code. There was some little mistakes. Check my example and comment if you had the solution to your question, thank you.
Public Class Form1
Private Sub TxtRemise_TextChanged(sender As Object, e As EventArgs) Handles TxtRemise.TextChanged
Dim TotalTTc As Decimal
TotalTTc = CDec(TotalHT.Text) + CDec(TXTMontantTva.Text)
TxtTTC.Clear()
TxtTTC.Text = (TotalTTc - CDec(TxtRemise.Text)).ToString
End Sub
End Class

Related

With vb.net, how can I split this filename/string?

Dim suffix As String = "_version"
I have a file called "Something_version1.jpg"
I need to split this so that I get "Something_version"
The following gets me "1.jpg"
filename = filename.Split(New String() {suffix}, StringSplitOptions.None)(1)
And the following gets me "Something"
filename = filename.Split(New String() {suffix}, StringSplitOptions.None)(0)
But what I need is "Something_version"
The suffix is dynamic, and can change.
Hope this is easier than I'm making it.
Thank you.
If you don't care about the "1.jpg" part at all, and all you want is the suffix and the part before the suffix, you can do what you have above (the second one) to get the prefix, and simply concatenate the prefix and suffix to get the answer you're looking for.
The split call might be overkill but it will do the job.
Try this!
Option Strict On
Option Explicit On
Option Infer Off
Public Class Form1
Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
Dim suffix As String = "_version"
Dim searchThis As String = "something_version1.png"
MsgBox(GetPrefix(suffix, searchThis))
End Sub
Function GetPrefix(suffix As String, searchThis As String) As String
Dim suffixLocation As Integer = searchThis.IndexOf(suffix) + suffix.Count
Return searchThis.Substring(0, suffixLocation)
End Function
End Class

Assigning value of User Defined Class (UDC) to textbox

Im learning about UDC in vb.net and trying to get a very simple program to display the value of a UDC in a textbox my code follows below:
Structure carDriverInfo
Dim carMake As String
Dim driverName As String
End Structure
Private Sub Button1_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles Button1.Click
Dim car As carDriverInfo
Dim driver As carDriverInfo
car.carMake = "Ford Fiesta"
driver.driverName = "J Hudgsons"
TextBox1.Text = car
TextBox2.Text = driver
End Sub
The problem is the compiler gives me the error that CarDriverInfo cannot get converted to string...
What am I doing wrong here?
Compiler doesn't know what variable to show. You can choose one value that will represent string output of your structure by adding following to your structure
Public Overrides Function ToString() As String
Return carName
End Function
But if you want to get different values from your structure you need to define your variables as properties and use those properties as you see fit like
car.carName

Backgroundworker progress: how can it report a double number instead of integer

I am developing a window application in VB.net and i as using backgroundworker.
Maybe it is a very simple question, but is it possible report the progress as a double number and not as the integer part of the progress percentage?
I need the full number in order to display some more info, and i can only do this when i know the exact iteration the algorithm is in.
Is there any easy way doing so?
Thanks in advance!
The ReportProgress method has two overloads. The first one takes only a percentProgress As Integer parameter, but the second one takes an additional userState As Object parameter. With that second overload, you can pass any type of data that you want. In your case, you could pass a Double value as your user-state, like this
BackgroundWorker1.ReportProgress(0, myDouble)
Then, in the ProgressChanged event handler, you can convert the value back to a Double, like this:
Private Sub BackgroundWorker1_ProgressChanged(sender As Object, e As ProgressChangedEventArgs) Handles BackgroundWorker1.ProgressChanged
Dim myDouble As Double = CDbl(e.UserState)
' ...
End Sub
As in the above example, if you don't need the percentProgress parameter, you can just pass a value of 0 for that parameter. You are not limited to passing just one or two values either. If you need to pass additional information, such as a status string, you could do so by creating your own class to encapsulate all of the status-related data and then pass one of those objects as your userState parameter. For instance:
Public Class MyUserState
Public Property MyDouble As Double
Public Property StatusDescription As String
End Class
Then you could call the ReportProgress method like this:
Dim myState As New MyUserState()
myState.MyDouble = 1.1
myState.StatusDescription = "Test"
BackgroundWorker1.ReportProgress(0, myState)
Then, you can read the values like this:
Private Sub BackgroundWorker1_ProgressChanged(sender As Object, e As ProgressChangedEventArgs) Handles BackgroundWorker1.ProgressChanged
Dim myState As MyUserState = DirectCast(e.UserState, MyUserState)
' ...
End Sub

Search from a list of string() in VB.NET

I am trying to find if mylines() contains a value or not. I can get the value by mylines.Contains method:
Dim mylines() As String = IO.File.ReadAllLines(mypath)
If mylines.Contains("food") Then
MsgBox("Value Exist")
End If
But the problem is that I want to check if mylines() contains a line which starts with my value. I can get this value from a single line by mylines(0).StartsWith method. But how do I find a string from all the lines which is starting from some value, e.g. "mysearch" and then get that line number?
I am using a for loop to do so, but it is slow.
For Each line In mylines
If line.StartsWith("food") Then MsgBox(line)
Next
Constrained to code for .NET 2.0, please.
Here's one way with Framework 2.0 code, simply set SearchString with the the string you want to search for:
Imports System.IO
Public Class Form1
Dim SearchString As String = ""
Dim Test() As String = File.ReadAllLines("Test.txt")
Private Sub Form1_Load(sender As System.Object, e As System.EventArgs) Handles MyBase.Load
SearchString = "Food"
End Sub
Private Function StartsWith(s As String) As Boolean
Return s.StartsWith(SearchString)
End Function
Private Sub Button1_Click(sender As System.Object, e As System.EventArgs) Handles Button1.Click
Dim SubTest() As String = Array.FindAll(Test, AddressOf StartsWith)
ListBox1.Items.AddRange(SubTest)
End Sub
End Class
When I test this with a file with 87,000 lines, it takes about .5 sec, to fill the listbox.
I'm not a VB guy, but I think you could use Linq:
Dim mylines() As String = IO.File.ReadAllLines(mypath)
??? = mylines.Where(Function(s) s.StartWith("food")) //not sure of the return type
Check out: How do I append a 'where' clause using VB.NET and LINQ?

Splitting a 'Decimal' in VB.NET

I have 532.016, and I want to get only the 532 part in VB.NET. How can I do that?
Math.Truncate(myDecimal)
will strip away the fractional part, leaving only the integral part (while not altering the type; i. e. this will return the type of the argument as well, be it Double or Decimal).
Cast it to an Integer.
Dim myDec As Decimal
myDecimal = 532.016
Dim i As Integer = Cint(myDecimal)
'i now contains 532
You could use System.Text.RegularExpressions:
Imports System.Text.RegularExpressions 'You need this for "Split"'
Public Class Form1
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
Dim yourNumber As String = 532.016 'You could write your integer as a string or convert it to a string'
Dim whatYouWant() As String 'Notice the "(" and ")", these are like an array'
whatYouWant = Split(yourNumber, ".") 'Split by decimal'
MsgBox(whatYouWant(0)) 'The number you wanted is before the first decimal, so you want array "(0)", if wanted the number after the decimal the you would write "(1)"'
End Sub
End Class
Decimal.floor(532.016) will also return 532.
Decimal.Floor rounds down to the closest integer.
However it won't work for negative numbers. See this Stack Overflow question for a complete explanation
Decimal.Truncate (or Math.Truncate) is really the best choice in your case.