Is value of vbNewLine platform-dependent? - vb.net

Is vbNewLine constant equivalent to vbCrLf (0x0D 0x0A) or is it equivalent to Environment.NewLine which is platform-dependent?
vbNewLine definition is not clear enough here: Represents a newline character for print and display functions.
In Windows, vbNewLine has 2 characters. It would be best if someone could verify vbNewLine at Mono. :)
I would prefer vbNewLine before Environment.Newline if they were equivalent because it is handier.

For Mono, you can find the Microsoft.VisualBasic.Constants source code here. It explicitly states that vbNewLine is equal to vbCrLf:
Public Const vbNewLine As String = vbCrLf
For completeness, the corresponding .NET source code can be found here.

Related

Classic ASP - Server.CreateObject - Unauthorized operation

I have the following code :
Set objUpload = Server.CreateObject("ASPUploadComponent.cUpload")
It seems to be failing, because the following debug code :
Response.Write(" begin ")
Response.Write(" Err : " & Err & " ")
Response.Write(" TypeName(Err) : " & TypeName(Err) & " ")
Response.Write(" Str(Err.Number): " & Err.Number & " ")
Response.Write(" Str(Err.Source): " & Err.Source & " ")
Response.Write(" Str(Err.Description): " & Err.Description & " ")
Response.Write(" TypeName(objUpload) : " & TypeName(objUpload) & " ")
Response.Write(" end ")
Outputs :
begin Err : -2147352567 TypeName(Err) : Object Str(Err.Number): -2147352567 Str(Err.Source): ASPUploadComponent Str(Err.Description): Unhandled error: 007~ASP 0104~Op�ration non autoris�e~ TypeName(objUpload) : Empty end
It says unauthorized operation.
This exact same way of creating the object works perfectly on some other script, but not here.
What could possibly cause this?
Edit
It's different from this question, because you would have to know it's related to the file size to begin with, so while the solution is the same, the other question would have not helped me, because I wouldn't have searched for it in the first place.
So after more digging, I found the actual reason, and it's definately not straight forward.
It is due to the fact that the file I was trying to upload, despite being rather small (1.27 Mb) was exceeding the miaximum size allow by IIS. And, for some reason which I can't explain, instead of letting me know that this was the issue, the COM object would just refuse to be created.
To solve this, one must change the maximum allowed size in the IIS settings as follows :
1):
:
It's in french, but you'll figure out.

find & vbCrLf & within a string - vb.net

In a string I have something like "First & vbCrLf & Name" - however, I want to take out the & vbCrLf & so it doesnt cause a line break.
I have done something like
If theString.Contains("& vbCrLf &") Then
' and replace, could do this above of course, but I just want it to go into the IF
End If
and
If theString.Contains("\n") Then
' and replace, could do this above of course, but I just want it to go into the IF
End If
and even "\r\n" but to no avail.
What am I missing?
If theString.Contains(vbCrLf) Then
'Do something
End If
Alternatively...
theString = theString.Replace(vbCrLf, "")
Try:
If theString.Contains(Environment.NewLine) Then
' Code goes here
End If
Remove the vbCrLf from the string literal in Contains.
testVal = testVal.Replace(vbCrLf, String.Empty).Replace("&", String.Empty)
Metacharacters not supported by VB.Net for Strings - can be used with RegEx and probably a few other .Net functions.
In your OP I think you intended:
If theString.Contains("& vbCrLf &") Then
to be
If theString.Contains(vbCrLf) Then
You can test for and replace in one command:
Dim s As String = vbCrLf
MsgBox(s.Length)
s = s.Replace(vbCrLf, "")
MsgBox(s.Length)

AppendText won't append to the next line in a text file / vb

I'm making a program that lets you add student info to an existing CSV text file but whenever I use append text it adds the new info to part of the last line and then a new line.
I want it to do this:
John Doe,29,Male
John Doe,29,Male
It does this instead:
John Doe,29,MaleJo
hn Doe,29,Male
Note: there isn't actually an empty line between each set of info, I just wanted it to be easy to read when I posted it here.
Here is the code for that section:
Dim swVar As IO.StreamWriter = IO.File.AppendText(frmMain.fileName)
swVar.WriteLine(txtName.Text & "," & txtAge.Text & "," & gender)
swVar.Close()
Any help would be appreciated, thanks!
A handy tool in VS2010 (and others) is snippets - right click Insert Snippets... - lots of code patterns for typical tasks. In your case here is a modified snippet:
Sub AddToFile(textToAdd As String, filePath As String)
My.Computer.FileSystem.WriteAllText(filePath, textToAdd, True)
End Sub
You may want to check/add a vbNewLine to the text being added since one is not automatically added as with WriteLine.
Not a direct reply to your stated problem, but an alternate method.
See if something like this works better:
IO.File.AppendText(frmMain.fileName, vbNewLine & txtName.Text & "," & txtAge.Text & "," & gender & vbNewLine)
AppendText will open write and close all in one operation so there's no need for a separate streamwriter. It also doesn't add newlines so those must be added separately
Unless of course you are doing a series of writes to the same file then something like this would probably be more appropriate:
Dim swVar As New IO.StreamWriter(frmMain.fileName, True)
'write your lines here
swVar.WriteLine(txtName.Text & "," & txtAge.Text & "," & gender)
swVar.Close()

Use the actual value of String as a Formatted Value for TextBlock.Text

So basically I have this:
A WPF window with 1 Button (btn_Convert) and 2 TextBoxes (txtBox_StringValue and txtBox_Result).
In txtBox_StringValue I then paste in a formatted string value:
"This is a Header" & vbCrLf & "======================" & _
vbCrLf & "INFO" & vbCrLf & "======================"
Then when I click btn_Convert I would like the following to happen.
Code:
Dim tempStringValue = txtBox_StringValue.Text
txtBox_Results.Text = tempStringValue
However (obviously), when I do the above the Results TextBox just displays the string again:
"This is a Header" & vbCrLf & "======================" & _
vbCrLf & "INFO" & vbCrLf & "======================"
Instead of:
This is a Header
======================
INFO
So how do I get the value of the string and then strip the containing double-quotes so that the value when assigned acts like it was a variable value set in code, not just passing a string.
From the research I have done I am guessing that I need to use Reflection, however I am not familiar with the Reflection concept and don't know how to approach it.
Any help would be greatly appreciated!
Reflection won't help you in this case. It sounds like what you're talking about is dynamically interpreting some VB.NET source code and output the result of executing that code to another text box. In that case you need to use the Code DOM classes to dynamically build an assembly in memory and execute it.

New line in VB.NET

Why, when I do im my code:
"Land Location \\r\\n Roundoff (C)"
I see the \\r\\n and not a new line feeder at the output?
Any idea how to do that?
As I said I must have only one string there, without using a "&". Can I put that vbCrLf inside of my string somehow?
There is no \ escape codes in VB so you can't put a line break in a string literal. The only escape character in VB strings is the double quotation marks used to insert a quotation mark in a string.
You can use the VB constant for a Windows type line break:
"Land Location " & vbCrLf & " Roundoff (C)"
For the code to be platform independent, you should use the NewLine property instead:
"Land Location " & Environment.NewLine & " Roundoff (C)"
Whether you should use the platform independent code or not depends on the situation.
If you need it as a single string for some reason, you would have to use a marker for the line break, that you replace when you use the string:
Dim s As String = "Land Location \n Roundoff (C)"
s = Replace(s, "\n", Environment.NewLine)
May be "Land Location " & vbCR & vbLF .....
--
Edit: per #JeffSahol's comments, you can use string interpolation since VB 14, so it can be like
$"...{vbCrLf}..."
Instead of including the newline manually in the String use System.Environment.NewLine.
vbCrLf
vbCr
vbLf