.Replace(String,String) VB function not works - vb.net

I need to replace the end of a pathfile in VB. So I try this code :
Private Function getfiledata(ByVal fichier As String) As String
Dim fileReader As String
Dim FichierFinal As String
MsgBox(fichier)
FichierFinal = fichier.Replace("E002.pfx","_E002.pem")
FichierFinal = fichier.Replace("X002.pfx","_X002.pem")
FichierFinal = fichier.Replace("A005.pfx","_A005.pem")
MsgBox(FichierFinal)
fileReader = My.Computer.FileSystem.ReadAllText(FichierFinal)
Return fileReader
End Function
The first MsgBox function return me the following result :
C:/Users/Bruno/Documents/Visual Studio
2010/Projects/SerEbics/SerEbics/bin/Debug/Certificats/512250X002.pfx
But the second return me the same path :
C:/Users/Bruno/Documents/Visual Studio2010/Projects/SerEbics/SerEbics/bin/Debug/Certificats/512250X002.pfx
So I need this result :
C:/Users/Bruno/Documents/Visual Studio2010/Projects/SerEbics/SerEbics/bin/Debug/Certificats/512250_X002.pem
Thank you in advance !
Thomas

The three lines in a row using teh Replace function are not doing quite what you think. Each time, they are setting FichierFinal to something new. So they are not building on each other. Try replacing them with this:
FichierFinal = fichier.Replace("E002.pfx","_E002.pem")
FichierFinal = FichierFinal.Replace("X002.pfx","_X002.pem")
FichierFinal = FichierFinal.Replace("A005.pfx","_A005.pem")

You are not doing what you think. At each new line you cancelled the previous one
and defining FichierFinal to a new value.
This will works (plus no need to do this in 3 lines):
FichierFinal = fichier.Replace("E002.pfx","_E002.pem").Replace("X002.pfx","_X002.pem").Replace("A005.pfx","_A005.pem")

This happens because your last Replace restore the original name in the variable FichierFinal. You should execute your replaces only if the file ends with one of the expected strings.
If fichier.EndsWith("E002.pfx") Then
FichierFinal = fichier.Replace("E002.pfx","_E002.pem")
Else if fichier.EndsWith("X002.pfx") Then
FichierFinal = fichier.Replace("X002.pfx","_X002.pem")
Else if fichier.EndsWith("A005.pfx") Then
FichierFinal = fichier.Replace("A005.pfx","_A005.pem")
End If
In this way you execute the Replace just one time and not three times. Remember that every time you call Replace a new string is allocated and returned and depending on the context of your calls this could have effects on the performances of your code.

Try this code u wrong replace:
Private Function getfiledata(ByVal fichier As String) As String
Dim fileReader As String
Dim FichierFinal As String
MsgBox(fichier)
FichierFinal = fichier.Replace("E002.pfx","_E002.pem")
FichierFinal = FichierFinal.Replace("X002.pfx","_X002.pem")
FichierFinal = FichierFinal.Replace("A005.pfx","_A005.pem")
MsgBox(FichierFinal)
fileReader = My.Computer.FileSystem.ReadAllText(FichierFinal)
Return fileReader
End Function

Related

Set statement, return empty string if error

From the following link there is an example at the bottom of the page which I have recreated in vb.net.
Before the following function runs, I save some data from a textfile into a dictionary called T.
For example:
Name - T0962
Value - 5.89
Public Shared Function initialization()
'Variables initialization
Dim parts As New List(Of Intialization)
'Add parts to the list.
parts.Add(New Intialization() With {
.PartName = "T0962",
.PartId = T.Item(.PartName))
})
If parts.Exists(Function(p) p.PartName = "T0962") Then
Dim value = parts.Where(Function(p) p.PartName = variable_type).FirstOrDefault()
Msgbox(value.PartId)
End If
End Function
The program works perfectly when I have "T0962" variable. When that variable does not exist in the textfile, it does not exist in the dictionary aswell. Thus, I get an error in the code, because the .PartId fails to be initialized. This is because in that textfile sometimes I have that value sometimes I do not.
After I have analized carefully I have noticed that the error happens in the Property statement, at Set(value As String) to be more exactly.
Public Property PartId() As String
Get
Return m_PartId
End Get
'here the error happens
Set(value As String)
m_PartId = value
End Set
End Property
Is there a way to avoid this in the Set statement? For example when there is an error then return an empty string?
Please let me know if there is something you do not understand.
Ok. Try Below. It works for me.
Dim partName As String
partName = "T0962"
parts.Add(New Intialization() With {
.PartName = partName,
.PartId = T.FirstOrDefault(Function(f) f.Key = partName).Value
})

Vb.net Text Splitting is Null

I just separating line by line in a text file, with this code
Sub Event2()
Dim Lines = File.ReadAllLines(Place + "Text.txt")
Dim Srl As String = Lines.ElementAtOrDefault(1)
Dim Str1() As String
Str1 = Srl.Split("^")
Title.Text = Str1(0)
LoadIMage(pb1, Str1(1))
LoadIMage(pb2, Str1(2))
LoadIMage(pb3, Str1(3))
TxtDesc.Text = Str1(4)
End Sub
But when the line is empty it is error, i try to added this code, but failed
If Srl.IsNullOrEmpty Then
What the code to ignore this sub when the line is Empty?
String.IsNullOrEmpty(Srl)
It's a Shared method available on the String class, so you needn't have a valid String instance to call it on.
Please try with:
If Srl IsNot Nothing Then

TextFieldParser Delimiters value not recognized correctly

I have some simply problem i guess, if you see below statments work without the last one: ValueFromTextFile which its value comming from my text file. This value in text file is exactly the same: "vbTab" - however it looks like when i trying to get it from my text file its not recognized the same as first line {vbTab} from example. Why is that?
.Delimiters = New String() {vbTab} <- this works
.Delimiters = New String() {","} <- this works
.Delimiters = New String() {ValueFromTextFile} <- this doesn't work
EDIT: (helper delimeter class):
Public Class CharDelimeterHelper
Private _delimeter As String
Public Sub New(ByVal delimeter As String)
Me._delimeter = delimeter
End Sub
Function GetDelimeterFormat() As ??
Dim result As ??
Select Case _delimeter
Case "vbTab"
result = ControlChars.Tab
Case ","
result = ","
Case Else
End Select
Return result
End Function
End Class
The string "vbTab" is not the same as the constant vbTab.
So if ValueFromTextFile equals "vbTab" is only works if all fields are separated by the string "vbTab" which i strongly doubt. I guess that they are separated by the tab-character which is represented by the vbTab-constant. You could also use ControlChars.Tab.

Splitting a CSV Visual basic

I have a string like
Query_1,ab563372363_C/R,100.00,249,0,0,1,249,1,249,1e-132, 460
Query_1,ab563372356_C/R,99.60,249,1,0,1,249,1,249,5e-131, 455
in a file
in two separate lines. I am reading it from the textbox. I have to output ab563372363_C/R and ab563372356_C/R in a text box. I am trying to use the split function for that but its not working..
Dim splitString as Array
results = "test.txt"
Dim FileText As String = IO.File.ReadAllText(results) 'reads the above contents from file
splitString = Split(FileText, ",", 14)
TextBox2.text = splitString(1) & splitString(13)
for the above code, it just prints the whole thing.. What's wrong?
Try this
Private Function GetRequiredText() As List(Of String)
Dim requiredStringList As New List(Of String)
Dim file = "test.txt"
If FileIO.FileSystem.FileExists(file) Then
Dim reader As System.IO.StreamReader = System.IO.File.OpenText(file)
Dim line As String = reader.ReadLine()
While line IsNot Nothing
requiredStringList.Add(line.Split(",")(1))
line = reader.ReadLine()
End While
reader.Close()
reader.Dispose()
End If
Return requiredStringList
End Function
This will read the file line by line and add the item you require to a list of strings which will be returned by the function.
Returning a List(Of String) may be overkill, but it's quite simple to illustrate and to work with.
You can then iterate through the list and do what you need with the contents of the list.
Comments welcome!!
Also this might work...
Dim query = From lines In System.IO.File.ReadAllLines(file) _
Select lines.Split(",")(1)
this will return an IEnumerable(Of String)
Enjoy
First
Since you are reading the whole text, your FileText would be ending like this:
Query_1,ab563372363_C/R,100.00,249,0,0,1,249,1,249,1e-132,460
\r\n
Query_1,ab563372356_C/R,99.60,249,1,0,1,249,1,249,5e-131, 455
So when you are referencing to your splitStringwith those indexes (1, 13) your result might probably be wrong.
Second
Try to specify what kind of type your array is, Dim splitString as Array should be Dim splitString As String()
Third
Make your code more readable/maintainable and easy to edit (not only for you, but others)
The Code
Private const FirstIndex = 1
Private const SecondIndex = 12
Sub Main
Dim myDelimiter As Char
Dim myString As String
Dim mySplit As String()
Dim myResult1 As String
Dim myResult2 As String
myDelimiter = ","
myString += "Query_1,ab563372363_C/R,100.00,249,0,0,1,249,1,249,1e-132, 460"
myString += "Query_1,ab563372356_C/R,99.60,249,1,0,1,249,1,249,5e-131, 455"
mySplit = myString.Split(myDelimiter)
myResult1 = mySplit(FirstIndex)
myResult2 = mySplit(SecondIndex)
Console.WriteLine(myResult1)
Console.WriteLine(myResult2)
End Sub

setting strings_array(0) = "" problem

im getting an exception on this:
Dim strings_extreme As String()
strings_extreme(0) = ""
it says that i am using it before it is being assigned a value
how do i initialize it?
please note that i need to be able to do this:
strings_extreme = input.Split(","c).Distinct().OrderBy(Function(s) s)
If you truly don't know how many strings there are going to be, then why not just use an IList:
Dim stringList As IList(Of String) = New List(Of String)
stringList.Add("")
You can get the Count of how many strings there are and you can For Each through all the strings in the list.
EDIT: If you're just trying to build an array from a Split you should be able to do this:
Dim strings_extreme() As String = input.Split(...)
Dim strings_extreme As List(Of String) = New List(Of String)
strings_extreme.Add("value1")
strings_extreme.Add("value 2")
strings_extreme.Add("value 3rd")
strings_extreme.Add("value again")
Dim strings() As String = strings_extreme.ToArray()
...
Dim strings_extreme(10) As String
strings_extreme(0) = ""
Further info: http://www.startvbdotnet.com/language/arrays.aspx
Dim strings_extreme as String() = {"yourfirstitem"}
But actually, why not take a look at some more advanced data structure from System.Collections namespace?
Shouldn't bother setting a string to "".
Try using:
Dim strings_extreme(10) As String
strings_extreme(yourIndex) = String.Empty