hex() function returns wrong values - why? - vb.net

I have a VB.net project in that I need to convert a character into an hex value. According to https://msdn.microsoft.com/de-de/library/963zt96e(v=vs.90).aspx, I tried this (example):
Dim sChar as String = "€"
Dim sNum as Integer = AscW(sChar)
Dim sHex as String = hex(sNum).ToString
When I set a breakpoint after that, I get these values in direct console:
?sNum
8364
?hex(sNum)
"20AC"
Which is correct and works as expected.
But the value calculated in running program is garbage:
?sHex
"20254"
Why do I get different results in running code and direct input console?
And how do I get the expected string value ("20AC")?

okay, found a solution (but no explaination)
sHex = convert.ToString(sNum, 16)
I cannot explain these wrong values, but the code above works for me, just in case that someone else has the same problem.

Related

index was outside bounds of array in SSRS when parameter come as blank

I am working on SSRS report where user!userid variable coming with 3 pipe delimited values
E.g :
ReportUser |500|100
I split the values using below expression
=(Split(Parameters!QueryString.Value, "|")).GetValue(0)
=(Split(Parameters!QueryString.Value, "|")).GetValue(1)
=(Split(Parameters!QueryString.Value, "|")).GetValue(2)
When the parameter is coming with values above expression works fine. But, when any parameter coming as blank, I am getting below below error during report execution.
index was outside bounds of array for the parameter.
I tried below workarounds with iif expression
=iif((Split(Parameters!QueryString.Value, "|")).GetValue(0)=NOTHING,0,
(Split(Parameters!QueryString.Value, "|")).GetValue(0))
=iif((Split(Parameters!QueryString.Value, "|")).GetValue(0)="",0,
(Split(Parameters!QueryString.Value, "|")).GetValue(0))
Still I am getting the same error. Could someone help how to handle the blank values with this expression?
I had initially though that this would work
This code fails
=SWITCH
(
Split(Parameters!QueryString.Value, "|").Length <3, "",
True, Split(Parameters!QueryString.Value, "|").GetValue(1)
)
As Switch stops evaluating at the first True result but for some reason I still got an Error. I'm not sure why this happens but I got round it by writing a function to do the work.
This does work
I added this code to the Reports Code property (it can probably be made better but I've not done any VB.Net for years)
Public Function GetSplitValue(inputString as String, delim as string, index as Integer) as String
Dim arr() AS String = Split(inputString, delim)
Dim result AS string
TRY
result = arr(index)
CATCH
result = ""
END TRY
RETURN result
End Function
You can then call this in your expression using something like this in your text boxes or whatever you are populating.
=Code.GetSplitValue(Parameters!QueryString.Value,"|",0)
=Code.GetSplitValue(Parameters!QueryString.Value,"|",1)
=Code.GetSplitValue(Parameters!QueryString.Value,"|",2)
If you want something other than an empty string, just edit the code in the CATCH block.

String.Format wrong format error

I'm stuck on something simple that I can't figure out why doesn't work now.
I tested the same code before(last Friday) and it works
I just need to get an String with four hexadecimal digits from an integer variable no mather what number between &H0000 and &HFFFF
So I write this simple code
Dim NumHex As Integer = 352
Dim NumHexStr As String = String.Format("{X4:0}", NumHex)
But now I'm getting
"Input string was not in a correct format."
Can you see something wrong on that code?
Thanks.
Your format condition must be String.Format("{0:X4}", value) or Value.ToString("X4") or may be Hex(value).PadLeft(4, "0"c)

dr.Item("Value1") has value but always return 0 when assigned to another variable

I need to modify some VB.net code. There is a strange problem that I am facing. I am retrieving value from a DataTable and trying to assign it to a variable. When I check the value of some column in QuickWatch window then it has value but when I assign it to a variable then 0 is returned to the variable. Below is the simple statement that is causing the problem.
Dim MyAmount As Double = Double.Parse(dr.Item("Amount").ToString)
In the QuickWatch window when I check dr.Item("Amount") then it has value 30.12 and after executing the above statement MyAmount has value 0. May be VB.net work somewhat different that I do not know?
Edit:
It is kind of wierd that above mentioned statement is not returning value. The following statement is running absolutely fine.
Dim tmpVar As String() = dr.Item("Amount").ToString.Split(".")
Latest Edit:
I think it has become more wierd. The problem does not seem to be related with dr.Item("Amount"). Suppose I want to store the current culture value in a variable by following code,
Dim CultureInformation As String = System.Globalization.CultureInfo.CurrentCulture.DisplayName
Now CutlureInformation variable after the statement is executed contains "nothing" but the DisplayName has value of English (United States). So I think the problem is somewhere else.
You should be using this syntax:
Dim MyAmount As Double = dr.Field(Of Double)("Amount")
I am not sure why you are getting this behavior - your line should work too.
You can also try this:
Dim MyAmount As Double = DirectCast(dr.Item("Amount"), Double)
When facing a weird issue like this, always try various options to achieve the same result, do your research and compare the outputs. It greatly helps to answer a question on StackOverflow.

A Perfectly good Substring Error

Im having a problem parsing a string array of Directories. The end goal is to query the path tied to the [global].MyDataDir & "\saved" to get all folders in this directory. However the actual foldernames, the last bit of text after the last indexof "\" holds the name of a plugin that I need to compare against an enumerated list of plugins for further functionality I won't get into here. The problem here is my last bit of code wont work. The Dim foldername as String = (etc...), It returns an error saying Index and length must refer to a location within the string. Parameter name: length.
Can any of you wizards, help me out here. Much appreciated.
Dim dirList As String() = System.IO.Directory.GetDirectories([global].MyDataDir & "\saved")
For dir As Integer = 0 To dirList.Length - 1
If IO.Directory.GetFiles(dirList(dir)).Length > 0 Then
For Each file As String In IO.Directory.GetFiles(dirList(dir))
Dim folderName As String = dirList(dir).ToString.Substring(dirList(dir).ToString.LastIndexOf("\"), dirList(dir).ToString.Length - 1)
Next
End If
Next
Semper Fi.
Use System.IO.Path.GetDirectoryName() instead.
Next time use the VB.NET Left() convenience function to avoid getting this wrong.
I found the reason....
The problem lies in the arguments of Substring(starting index, length of copy from starting index). I was under the impression, the length argument would take into account the entire string when calculating the length. Instead the second argument of this function acts upon the results of the first argument, not the entire string. So the length of the string is actually much longer than what exists after taking an index of it.
Thanks for the help.

convert string to double in vb.net

data table contain column named as Fld_primary. this column contain value like 0.00 it is double datatype in mysql table.i want store that datatable value in double variable. am always getting error when i convert to double datatype.
my code
-------
Dim ds5 As dataset1.DTSUMDataTable = TA5.GetData(users)
Dim dttot As Double
dttot = CType(ds5("fld_primary").ToString, Double)
Error
Conversion from string "fld_primary" to type 'Integer' is not valid.
Edited # 3:01 AM with most recent screen caps.
Sometimes I find myself second guessing certain code based on people's answers, but I went ahead and took the time to check if the code that they are all using is even valid:
As you can see that code is a no go, so I used the correct code you see at the bottom there to reference a column.
However, if you wish to get a single cell use the chunk of code below that uses the foreach loop (the rest is my basic setup to show you how it works):
"Y" will equal the value of the datatable cell and you may convert it using the Double.Parse() method:
Dim y = Double.Parse(zDataRow("cat").ToString())
Be careful, if you have multiple rows you will notice that the value of y will change as it makes its way through all the rows.
you can convert it using the Convert class.
Dim dttot As Double = Convert.ToDouble(ds5("fld_primary"))
Your error is actually: ds5 expects an integer as a parameter, so using ds5("fld_primary") is not valid in your code. Perhaps you can try ds5(0)("fld_primary").
After you fixed it, use
dttot = Double.Parse(whatever_string_you_should_put_here)
If you cannot ensure your string must be a valid double, then use Double.TryParse.
You are better off using the 'Double.TryParse' way of converting as this handles any errors better and simply returns a boolean if succesful or not, using 'parse' will throw an exception which isnt anywhere near as elegant.
Dim dub as Double = 0
Double.TryParse("Your String Here", dub)
Try using Double.TryParse(text,value)
Try using this:
For a=0 to yourgridview.rows.count-1
Yourgridview.rows(a).cells(targetcolumnnumber).value=cdbl(Yourgridview.rows(a).cells(targetcolumnnumber).value)
Next