Can you overcome vb.net from HttpContext.Current.Request.Url.AbsoluteUri and HttpContext.Current.Request.RawUrl using different case symbols? - vb.net

The code below throws a server error:
Option Compare Text
Dim strAppURL As String = HttpContext.Current.Request.Url.AbsoluteUri.Substring(0, HttpContext.Current.Request.Url.AbsoluteUri.IndexOf(HttpContext.Current.Request.RawUrl))
System.ArgumentOutOfRangeException: 'Length cannot be less than zero.
because:
HttpContext.Current.Request.Url.AbsoluteUri =
"http://localhost:22222/Dev/Canvas.aspx?&act=ccis&filn=002+(Ene+D%C4%83nu%C5%A3).png"
and
HttpContext.Current.Request.RawUrl =
"/Dev/Canvas.aspx?&act=ccis&filn=002+(Ene+D%c4%83nu%c5%a3).png"
Observe the symbols are different: %C4%83 vs. %c4%83 and %C5%A3 vs. %c5%a3
Just in case this issue was encountered before, I would welcome some help in handling it outside the code (other than using ToUpper() or ToLower() methods). I would prefer an application level directive. Thank you.

Related

Why does AllowExponent makes Decimal.Parse fail to parse normal numbers?

I tried these
Dim a = Decimal.Parse("0.00001", System.Globalization.NumberStyles.Any)
Dim c = Decimal.Parse("0.00001")
Dim cb = Decimal.Parse("0.00001", System.Globalization.NumberStyles.AllowExponent)
The first code works.
The second line works.
The third line doesn't work.
It seems that if I do not mention the numberstyles, the numberstyles default to something. Default to what?
It basically allow parsing normal number (".00001"). However, that ability is turned off if System.Globalization.NumberStyles.AllowExponent is specified as parameter.
Why is this happening?
What is the default value of NumberStyles if we use the normal overload of Decimal.parse, without additional parameters?
You wouldn't write a number with exponent other than a whole number. See my comment above.
Dim cb = Decimal.Parse("1e-5", System.Globalization.NumberStyles.AllowExponent)
MessageBox.Show(cb.ToString)
Dim ab = Decimal.Parse("1e-5", System.Globalization.NumberStyles.Any)
MessageBox.Show(ab.ToString)
Both message boxes show .00001.
I just want to add that per Parsing decimal in scientific notation
System.Globalization.NumberStyles.AllowExponent
doesn't allow putting decimals.
It's probably unituitive.
Hence,
cb = Decimal.Parse("0.00001", System.Globalization.NumberStyles.AllowExponent)
will throw error because the parser cannot parse .
I have no idea why it's designed that way. So how are we going to parse 4.5e10, for example.
You need other options. AllowExponent seems to do exactly that. Allow Exponent and that's it.

VB Console Mode text files

I'm new to VB console mode but I have to use it for my programming paper and one of the programs i have to code for requires me to create a text file called "Carsales.text"
The thing is I'm using my textbook as a guide and this is the example they provide
Dim FileHandle As IO.StreamWriter
Dim LineOfText As String
FileHandle = New
IO.StreamWriter("Sample.TXT")
FileHandle.WriteLine(LineOfText)
FileHandle.Close()
I tried to run it first to see how exactly it works but
FileHandke = New
IO.StreamWriter("Sample.txt")
both produce 2 errors:
Error 1 Type expected
Error 2 'StreamWriter' is a type in 'IO' and cannot be used as an expression.
Considering this is exactly what's in the textbook I don't know how to modify it to solve the problem
Error 1 Type expected
This error is because your class is on a new line and need's moved up. The second error happens because of this issue...
Error 2 'StreamWriter' is a type in 'IO' and cannot be used as an expression.
This error is because IO.StreamWriter("Sample.TXT") is on a different line as VS is interpreting it as an expression . You need to move this up to where FileHandle = New is. Then it should look like this:
FileHandle = New IO.StreamWriter("Sample.TXT")
On another note: Wrap your StreamWriter in a Using statement, very important. When you wrap it in a Using statement it helps to release resources used by the TextWriter object. If you do not wrap it, at least call FileHandle.Dispose() after FileHandle.Close()...
For example...
Using FileHandle As New IO.StreamWriter("Sample.TXT")
FileHandle.WriteLine(LineOfText)
FileHandle.Close()
End Using

The specified RegistryOptions value is invalid

What im trying to do is write a key to the registry but im stepping from one problem to another, first permissions problem, now this..
This is the line of code.
If PNGchk.Checked = True Then
My.Computer.Registry.Users.CreateSubKey(UserSID & "\Software\Microsoft\Windows\CurrentVersion\Explorer\FileExts\.png\UserChoice", True, Security.AccessControl.RegistryRights.FullControl).SetValue("Progid", "SIV.png", Microsoft.Win32.RegistryValueKind.String)
End If
You must have Option Strict Off for that code to even compile, so you might want to fix that to start with. Option Strict On would have flagged issues with that code right away. You should read the documentation or at least pay attention to Intellisense for that method because your second and third arguments make no sense. No overload that I can see has a Boolean parameter and if you want to use a RegistryRights value you do so within a RegistrySecurity object as far as I can see.
RegistryKeyPermissionCheck.ReadWriteSubTree worked for me.
Using clsid64 = view64.CreateSubKey("Software\Microsoft\Windows\CurrentVersion\Explorer\FileExts\.png\UserChoice", RegistryKeyPermissionCheck.ReadWriteSubTree)
clsid64.SetValue("StubPath", "SIV.png")
clsid64.Close()
End Using

Ascw returns "Cannot convert to 'Integer'." in Watch or Immediate

I have following sample code
Dim q As Char = "a"
Dim res As String = CStr(AscW(q))
res contains correctly "97" but when I use AscW(q) in watch or immediate it returns message: Cannot convert to 'Integer'.
Is it a bug or Visual Studio or am I doing something not correctly?
How can I see a character code in Immediate.
Note
The code presented is just an example. I found the problem when trying to see Character code in the Watch Window.
For a workaround, how about the command
? System.Text.Encoding.Unicode.GetBytes(q)
I personally believe that any acceptable VB.Net code should be acceptable in the Immediate window and really don't understand why AscW is causing errors when VB.Net offers no equivalent (e.g. in C#, but not VB.Net, you can cast a Char variable to an Integer to get the character code).
You are doing everything right (and the outputs will be OK in any case), although you are using old VB code. If you need functionalities like AscW (, Asc, ChrW, etc.), you would have to rely on this "old code" to get what you want (directly or via Microsoft.VisualBasic.Strings which, btw, does not show a different behaviour). But, in any other case, you should avoid the utilisation of this old code.
Test these two lines in the Immediate Window:
Dim res As String = CStr(5)
res = 5.ToString()
As you can see, you get an "error" (VS 2010, right-click on the line and select "QuickWatch") in the first line (old version), but not in the second one (.NET version).
Thus, the behaviour you observed can be considered as an inoffensive bug (no real effects in the execution) more or less understandable if you analyse the situation (you are asking a certain language (VB.NET) to support all its own features and the ones from an old language (VB); with the old one, some secondary functionalities might not be perfect).

Is there any good defacto standard 'everything went smoothly' message in a method that returns string errors?

So my class takes data and does it's thing, returning an error message if anything went wrong. What should I make the string if everything went fine? null? "1"? "OK!"? "success"?
Please support your answer.
Unix standard return codes use '0' as OK - by analogy, it's often recommended to use empty string (length 0), at least in languages where you can treat the "" value as "false".
E.g., in Perl: if ( $error = my_method_call() ) { print "Failed: $error\n" }
In a language where there's no such implication (use "" as false), any string can be chosen as "OK", as long as it's obvious and readable ("OK" fits the bill).
Methods should never return string errors. They are way too error prone and are more costly than the obvious alternative, returning integer codes, which you can translate to descriptive constants and have a single method which translates those codes to strings.
If you will use error codes to print them out (or log them), then it might be fine to use strings, but then again, there is nothing preventing you from printing or logging the error in the erroring method itself and returning a failing status code to the caller.
If you will use string error codes to check internally in code for different conditions, strings are a pain:
rv = some_function();
if (rv == "The file could not be read") {
take_corrective_action();
}
About including details in error code, the caller (usually) has the details and can compose the complete error message:
rv = read_data(FILE);
if (rv == READ_PERMISSION_ERROR) {
log("The file " + FILE + " could not be read. You don't have permissions");
}