Conversion from string (x) to type 'Integer' is not valid - visual-studio-2012

doing something for class and i'm completely stuck, even teacher seems unsure. any response appreciated. it's the first printline that is causing problems:
"A first chance exception of type 'System.InvalidCastException' occurred in Microsoft.VisualBasic.dll
Additional information: Conversion from string "1: Night F3, 10 tickets purchase" to type 'Integer' is not valid."
Dim filename As String
Dim fridaytickets As Integer
fridaytickets = 0
filename = "Z:\Computing Science\S5 (Higher)\Coursework Assessment\output.txt"
FileOpen(1, filename, OpenMode.Output)
For x = 1 To 300
If Mid(TicketID(x), 1, 1) = "F" Then
PrintLine((x) & ": Night " & TicketID(x) & ", " & TicketNo(x) & " tickets purchased. £" & (TicketNo(x) * 10) & " made.")
fridaytickets = fridaytickets + TicketNo(x)
End If
PrintLine(fridaytickets & " were purchased for Friday night.")
PrintLine("£" & (fridaytickets * 10) & " was made.")

This is the line:
PrintLine(fridaytickets & " were purchased for Friday night.")
Your fridaytickets is an integer, Strangely, it seems that & is taken as binary operator, so both side with integer. It should not be so, but maybe it is a bug or version dependent. Or just an overloading of the normal concatenation operator.
In any case, the correct way is to prepare the strings to be concatenated:
PrintLine(fridaytickets.toString & " were purchased for Friday night.")

Related

How to convert a "fraction" character into a decimal?

I have a dataset where the format of data is like this:
10 ¾ AB 02/15/19
I'm trying to convert the ¾ into .75 so that the data looks like:
10.75 AB 02/15/19
I am thinking of trying to iterate through each character in the string, but once I do so how can I convert the ¾ into .75?
Simple set of string replacements:
Public Function numerify(s As String) As String
numerify = Replace(s, " " & ChrW(190), ".75")
numerify = Replace(numerify, " " & ChrW(188), ".25")
numerify = Replace(numerify, " " & ChrW(189), ".5")
End Function
NOTES:
We replace the space before the fraction as well as the fraction to get the desired result.There may be other uni-code "fractions" you may need to consider.

Comparing time in ASP classic/VBscript

I am working on a function for an asp page that compares if a time entered is greater than a time with added leeway. I noticed certain times when checked would fail the test when the times are equal. Included is a snip of my function to illustrate. Not sure why equal dates would fail, and would like to know if this is a good way to go about comparing time.
<%
function TimeTest(testTime, checkTime, buffer, try)
checkingTime = FormatDateTime(cdate(DateAdd("n", buffer, cdate(checkTime))),4)
if try = 1 then
testTime = FormatDateTime(testTime, 4)
checktime = FormatDateTime(checkTime, 4)
end if
if cdate(testTime) > DateAdd("n", buffer, cdate(checkTime)) then
TimeTest = "<p class = 'redS'>Fails! testTime: "&testTime&" < checkTime:"&checkingTime&"</p>"
else
TimeTest = "<p class = 'greenS'>Works! testTime: "&testTime&" > checkTime:"&checkingTime&"</p>"
end if
end function
response.write("<br><br><h1>Test2</h1><br>")
for i=0 to 23
for j=0 to 59
response.write(TimeTest(i&":"&j&":00", i&":00:00", j, 1))
response.write("<BR>")
next
next
%>
This problem has earned my attention! I can reproduce the results and it's very unclear what's going on behind the scenes in these comparisons. However, I have a workaround for you
Here is a modified version of the code that I've been using to analyse the issue...
<%
Option Explicit
Function TimeTest(a, b, buffer)
Dim c : c = DateAdd("n", buffer, b)
Dim s : s = Join(Array("a=" & a, "b=" & b, "c=" & c, "buffer=" & buffer), ", ")
Dim passed : passed = a <= c
'Dim passed : passed = DateDiff("s", a, c) <= 0
If passed Then Exit Function
Dim color : color = "red" : If passed Then color = "green"
TimeTest = "<div style='color:" & color & "'>" & s & "</div>"
End Function
Dim i, j, a, b
For i = 0 To 23
For j = 0 To 59
a = CDate(i & ":" & j & ":00")
b = CDate(i & ":00:00")
'a = CDate(Date() & " " & i & ":" & j & ":00")
'b = CDate(Date() & " " & i & ":00:00")
Response.Write(TimeTest(a, b, j))
Next
Response.Write("<hr>")
Next
%>
Note that commenting out line 13 will reveal lines that pass. By default, I'm showing only failures.
The first thing to note is that I have some commented variants on lines 24-25 where I add today's date to the value before casting it. Interestingly, doing this changes the pattern of which times fail the test. There are still roughly the same number of failures but they occur at different buffer values.
This leads me to believe that behind the scenes in VBScript, these datetimes might be cast to floating-point numbers when you use the native < <= > >= comparison operators on them and that's resulting in some precision errors. If they were converted to long integers, then they should surely be correct.
I did a version of the code where instead of using a direct comparison on the VBDateTimes, I compared the integer representation of them (unix time) using this function:
Function date2epoch(myDate)
date2epoch = DateDiff("s", "01/01/1970 00:00:00", myDate)
End Function
When doing that, all tests passed. However, it is an unusual way to do things. I thought there should be a more 'normal' way.
I then went back and replaced the straightforward <= operator with a call to DateDiff instead (comment out line 10, uncomment line 11). Whether I used seconds or minutes, the tests passed. So, I think the takeaway lesson here might be to always use DateDiff when comparing VBDateTimes. As someone who's used VBS for a while and never encountered issues with native comparisons before, this is a revelation and I may need to offer this advice to my colleagues too.

MS Access: How do I remove the 13 leading spaces of this field?

I have a table where one of the fields has 13 leading spaces (no visible characters in them). I tried TRIM() and REPLACE([Field1], " ", "") but neither one worked. Could anyone venture a guess as to what's going on and how to fix this?
This is not an answer but it will allow you to see what the chr value is for the string. Just call it like MsgBox WhatAreTheAscValues([Field1]) and you will see what value the characters that make up the string and allow you to adjust your code accordingly.
Function WhatAreTheAscValues(str As String)
Dim i As Integer
Dim answer As String
answer = "The Chr(x) values in this string are listed below" & vbCrLf
For i = 1 To Len(str)
answer = answer + CStr(Asc(Mid(str, i, 1))) & ", "
Next i
WhatAreTheAscValues = Left(answer, Len(answer) - 2)
End Function

Why am I getting an InvalidCastException here?

I'm trying to write code to print out "Row (0 to 3): ".
Here's my code:
ReadOnly gridSize As Integer = 4
Dim s1 As String
s1 = "Row (0 to " & (gridSize - 1) & "): "
WriteLine(s1)
I'm getting an InvalidCastException with the following error at the last line, when the machine tries to print out the string:
Conversion from string "Row (0 to 3): " to type 'Integer' is not valid.
Integer is not a String.
How about using gridSize.ToString() ?

Get the double part from string with vbscript

I want to create a barcode with vbscript that will be decoded from my company's erp (my company devides the nubers with 10000.
The barcode should have this type of look:
99XXXXXXXXXQQQQQQQQPPPPP where: X is my barcode, Q is quantity, and P is the price. With concatenation I have:
Result = 99 & [sheet$.BARCODE] & right("00000000" & quantity*10000, 8) & right("00000" & VBScript1*10000,5)
Now VBScript1 has this style, because it is used elsewhere in the program:
VBScript1 = "PRICE: "& FormatCurrency([sheet$.TIMH SAKOYLAKI]/[sheet$.SAKOYLAKI TWN]*1.3*(Round((40*CDbl(zyg))/CDbl([sheet$.GR/40 TEM]))),2)
so the output of VBScript1 is like Price: $0,40
Now my question is how to extract from the string the number only and then multiply it by 10000, in order to use it above?
For my example I want Price: Price: $0,40 to be used as 04000 in barcode.
Use Split() on $ to get the numerical part of (e.g.) "Price: $0,40", deal with the decimal comma, pad on the left:
>> s = "Price: $0,40"
>> p = CDbl(Replace(Split(s, "$")(1), ",", "."))
>> t = Right(String(5, "0") & p * 10000, 5)
>> WScript.Echo t
>>
04000
>>
Like Marc B said, I tried this and it worked nice and easy :)
Result =[...]right("00000" & CDbl((Mid(VBScript1,7,7)))*10000,5)[...]

Resources