Dim and set the variable in 2 lines in vb.net - vb.net

i have this line:
Dim strings_extreme = input.Split(","c).Distinct().OrderBy(Function(s) s)
i need to Dim it in one line, and set it a value on another line
how do i do that?
would it just be Dim strings_extreme() ??
and then strings_extreme = input.split.... ?

Even though VB.net allows you not to specify the type, it's always more safe to specify it explicitly. Hence:
Dim strings_extreme as string()
strings_extreme = input...

Just like this:
Dim strings_extreme
strings_extreme = input.Split(","c).Distinct().OrderBy(Function(s) s)
One note, though. I would turn on option strict. Its never a good idea to declare a variable without a type.
Dim strings_extreme As String()

Pretty close. Here it is:
Dim strings as string = "grape,apples,lime"
Dim strings_extreme
strings_extreme = Input.Split(strings,",").Distinct().OrderBy(Function(s) s)
for each e in strings_extreme
e.tostring 'do something with this
next

Related

VB.net need help parsing a substring

I have a value I am capturing from an Http Request
Dim someValue As String = Request.Params("search")
Here is the value of my string:
?MyId1=VALUE1&MyId2=VALUE2&MyBoolen=True
I am trying to capture VALUE2. I tried the below code, but haven't had any success.
If Not String.IsNullOrEmpty(someValue) Then
Dim x = someValue.Substring(someValue.IndexOf("&"c) + 1)
If Not String.IsNullOrEmpty(someValue) Then
Dim y = x.Substring(someValue.IndexOf("="c) + 1)
End If
End If
How can I do this?
It looks like you're overthinking this. The Request object will let you look up the MyId2 value directly:
Dim MyId2 As String = Request.QueryString("MyId2")
It's also possible this is a nested query string, where what you actually have is something more like this:
/?search=%3FMyId1%3DVALUE1%26MyId2%3DVALUE2%26MyBoolen%3DTrue
This would give you the original string after the runtime URL Decodes the search element. In that case, you should look at the HttpUtility.ParseQueryString() method, rather than trying to do this yourself:
Dim search As String = Request.Params("search")
Dim searchValues As NameValueCollection = HttpUtility.ParseQueryString(search)
Dim MyId2 As String = searchValues("MyId2")
Which could even be written as a one-liner if we really wanted:
Dim MyId2 As String = HttpUtility.ParseQueryString(Request.Params("search"))("MyId2")
But if you really wanted to parse this by hand, one of the nice things about this is everything should be URL-encoded. This means you don't have worry about stray & or = characters as part of the data, and a simple Split() call should be safe:
Dim MyId2 As String = ""
Dim items = someValue.Substring(1).Split("&"c)
For Each item As String In Items
Dim parts = item.Split("="c)
If parts(0) = "MyId2" Then
MyId2 = parts(1)
Exit For
End If
Next
Or
Dim parts() As String = someValue.Substring(1).Split("&"c).
Select(Function(s) s.Split("="c)).
FirstOrDefault(Function(p) p(0) = "MyId2")
Dim MyId2 As String = If(parts IsNot Nothing, parts(1), "")

how to get the fix substring from dynamic string content?

I am developing VB.NET windows app. in VS 2010.
I want to get the substring
$CostCenterId|4^10
from the below string .
PaymentMode|NEFT^$IsPaid|False^$Currency|INR-Indian
Rupee^$CostCenterId|4^10$LedgerId|2^3$
The position of current string ($CostCenterId|4^10) in the sequence may be change.
but it will always between the two $ sign.
I have written the below code, but confused abt what to write next ?
Public Sub GetSubstringData()
dim sfullString = "PaymentMode|NEFT^$IsPaid|False^$Currency|INR-Indian
Rupee^$CostCenterId|4^10$LedgerId|2^3$"
Dim CostIndex As Integer
CostIndex = sDiscription.IndexOf("CostCenterId")
sDiscription.Substring(CostIndex,
End Sub
Have a look into the Split function of a string. This allows you to split a string into substrings based on a specified delimiting character.
You can then do this:
Dim sfullString = "PaymentMode|NEFT^$IsPaid|False^$Currency|INR-Indian Rupee^$CostCenterId|4^10$LedgerId|2^3$"
Debug.WriteLine("$" + sfullString.Split("$"c)(3))
Result: $CostCenterId|4^10
You will probably want to do some error checking to make sure the string actually contains the data you expect though.
However looking at the data, what you have is a string containing key-value pairs so you would be better to have a property to hold the CostCenterId and extract the data like this:
Public Property CostCenterId As String
Public Sub Decode(ByVal code As String)
For Each pair As String In code.Split("$"c)
If pair.Length > 0 AndAlso pair.Contains("|") Then
Dim key As String = pair.Split("|"c)(0)
Dim value As String = pair.Split("|"c)(1)
Select Case key
Case "CostCenterId"
Me.CostCenterId = value
End Select
End If
Next
End Sub
Then call it like this:
Decode("PaymentMode|NEFT^$IsPaid|False^$Currency|INR-Indian Rupee^$CostCenterId|4^10$LedgerId|2^3$")
Why not split() the string by $ into an array, and then look for the element which contains CostCenterId
This should work:
Dim token = "$CostCenterId"
Dim costIndexStart As Integer = sfullString.IndexOf(token)
Dim costIndexEnd As Integer = sfullString.IndexOf("$", costIndexStart + token.Length)
Dim cost As String = sfullString.Substring(costIndexStart, costIndexEnd - costIndexStart + 1)
Result: "$CostCenterId|4^10$"
If you want to omit the dollar-signs:
Substring(costIndexStart + 1, costIndexEnd - costIndexStart - 1)
Try something like this:
Dim CostIndex As Integer
CostIndex = sDiscription.IndexOf("CostCenterId")
auxNum = sDiscription.IndexOf("$"c, CostIndex) - CostIndex
sResult = sDiscription.SubString(CostIndex, auxNum)
Your string,
Dim xString = "PaymentMode|NEFT^$IsPaid|False^$Currency|INR-Indian Rupee^$CostCenterId|4^10$LedgerId|2^3$"
Substring process,
xString = xString.Substring(xString.IndexOf("$CostCenter"), xString.IndexOf("$", xString.IndexOf("$CostCenter") + 1) - xString.IndexOf("$CostCenter"))
Try this Code:
Dim sfullString = "PaymentMode|NEFT^$IsPaid|False^$Currency|INR-Indian" _
& "Rupee^$CostCenterId|4^10$LedgerId|2^3$"
Dim sp() As String = {"$"}
Dim ar() As String = sfullString.Split(sp, StringSplitOptions.RemoveEmptyEntries)
Array.Sort(ar)
MsgBox("$" & ar(0))

Adding values to array

I am trying to run an event which will search through the different files in a given directory. The goal is to have it search for all files that begin with 'SP_', which are .sql files containing Stored Procedures. I would then like to add the full text of these Procedures to an array to be used later. This is causing an error when run, which I believe is because 'FullProcedureArray()', the string array I am trying to load does not have defined boundaries. When I declare it as 'FullProcedureArray(7)', or with some other value, it appears to run fine. But I don't want to have to hard-code a boundary for 'FullProcedureArray'; I would rather let it be defined by whatever the number of files in the folder is.
My question: Is there a way to declare 'FullProcedureArray' without having to give it an absolute value? I may just be missing something painfully obvious, but I haven't worked with this type of array much in the past. Thanks in advance for your help.
Dim AppDataLocation As String = "C:\Files\TestFiles\"
Dim ProcedureArray As String()
Dim ProcedureText As String
Dim FullProcedureArray() As String
Dim sourceDirectoryInfo As New System.IO.DirectoryInfo(AppDataLocation)
Dim fileSystemInfo As System.IO.FileSystemInfo
Dim i As Integer = 0
For Each fileSystemInfo In sourceDirectoryInfo.GetFileSystemInfos
If (fileSystemInfo.Name.Contains("SP_")) Then
ProcedureArray = System.IO.File.ReadAllLines(AppDataLocation & fileSystemInfo.Name)
ProcedureText = Join(ProcedureArray, "")
FullProcedureArray.SetValue(ProcedureText, i)
i = (i + 1)
End If
Next
An array by definition has a fixed upper bound. If you don't want a fixed upper bound, don't use an array. Use, for example, a List(Of String) instead:
Dim AppDataLocation As String = "C:\Files\TestFiles\"
Dim ProcedureList As New List(Of String)
Dim sourceDirectoryInfo As New System.IO.DirectoryInfo(AppDataLocation)
For Each fileSystemInfo As System.IO.FileSystemInfo In sourceDirectoryInfo.GetFileSystemInfos
If (fileSystemInfo.Name.Contains("SP_")) Then
Dim ProcedureText As String = _
System.IO.File.ReadAllText(AppDataLocation & fileSystemInfo.Name)
ProcedureList.Add(ProcedureText)
End If
Next
If, for some reason, you still need the result as an array afterwards, simply convert the list to an array:
Dim myArray() As String = ProcedureList.ToArray()
If you don't want to give a size to your array or want to change at runtime, you can use "Redim Preserve"
http://msdn.microsoft.com/en-us/library/w8k3cys2%28v=vs.71%29.aspx

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

Left of a character in a string in vb.net

say if I have a string 010451-09F2
How to I get left of - from the above string in vb.net
I want 010451
The left function doesn't allow me to specify seperator character.
Thanks
Given:
Dim strOrig = "010451-09F2"
You can do any of the following:
Dim leftString = strOrig.Substring(0, strOrig.IndexOf("-"))
Or:
Dim leftString = strOrig.Split("-"c)(0) ' Take the first index in the array
Or:
Dim leftString = Left(strOrig, InStr(strOrig, "-"))
' Could also be: Mid(strOrig, 0, InStr(strOrig, "-"))
Dim str As String = "010451-09F2"
Dim leftPart As String = str.Split("-")(0)
Split gives you the left and right parts in a string array. Accessing the first element (index 0) gives you the left part.
Sorry not sure on the vb syntax, but the c# is
string mystring ="010451-09F2";
string whatIwant = mystring.Split('-')[0];
Get the location of the dash first (or do it inline), and use that value for the left. This is old school VBA, but it'll be something like this:
Left(YourStringWithTheDash, InStr(YourStringWithTheDash)-1)
dim s as String = "010451-09F2"
Console.WriteLine(s.Substring(0, s.IndexOf("-")))
Console.WriteLine(s.Split("-")(0))
Use something like this:
Mid("010451-09F2",1,InStr("-"))
Dim sValue As String = "010451-09F2"
Debug.WriteLine(sValue.Substring(0, sValue.IndexOf("-"c)))
This helped
Dim str1 as string = me#test.com
Dim str As String
str = Strings.Left(str1, str1.LastIndexOf("#"))