|
-
May 19th, 2013, 02:13 PM
#1
Thread Starter
Member
-
May 19th, 2013, 07:01 PM
#2
Re: Splitting string and adding contents into listview columns.
You need to use the For loop with a Step of 3 and grab indexes i, i + 1, and i + 2 as you go along. The ListViewItem constructor has an overload that takes a string array, use that to define the full ListViewItem text in each of the 3 columns.
Something like this:
Code:
Dim len As Integer = 3
For i As Integer = 0 To SplitArrayLength - len Step len
ListView1.Items.Add(New ListViewItem(New String() {SplitArray(i), SplitArray(i + 1), SplitArray(i + 2)}))
Next
<<<------------
.NET Programming (2012 - 2018)
®Crestron - DMC-T Certified Programmer | Software Developer <<<------------
-
May 19th, 2013, 07:04 PM
#3
Re: Splitting string and adding contents into listview columns.
try this:
Code:
Sub getUsers()
Dim Str As Stream
Dim srRead As StreamReader
Dim req As WebRequest = WebRequest.Create(url)
Dim resp As WebResponse = req.GetResponse
Str = resp.GetResponseStream
srRead = New StreamReader(Str)
Dim readStr As String = srRead.ReadToEnd
readStr = readStr.Replace("</table>", "")
readStr = readStr.Replace("<table>", "")
readStr = readStr.Replace("<tr>", "")
readStr = readStr.Replace("<td>", "")
readStr = readStr.Replace("</tr>", "")
readStr = readStr.Replace("</td>", "#")
MsgBox(readStr)
Dim splitStr() As String = readStr.Split("#"c)
For x As Integer = 0 To splitStr.GetUpperBound(0) Step 3
userList.Items.Add(New ListViewItem() splitStr.Skip(x).Take(3).ToArray)
Next
End Sub
Last edited by .paul.; May 19th, 2013 at 07:09 PM.
- Coding Examples:
- Features:
- Online Games:
- Compiled Games:
-
May 19th, 2013, 07:12 PM
#4
Re: Splitting string and adding contents into listview columns.
I don't see the need to use LINQ though for something simple enough to index yourself and declare the array elements manually. Otherwise you're generating an IEnumerable result calling methods to start from the beginning, skip x elements, and take 3 from that location, then cast to an array. Indexing directly is more efficient.
<<<------------
.NET Programming (2012 - 2018)
®Crestron - DMC-T Certified Programmer | Software Developer <<<------------
-
May 26th, 2013, 04:20 PM
#5
Thread Starter
Member
Re: Splitting string and adding contents into listview columns.
 Originally Posted by AceInfinity
You need to use the For loop with a Step of 3 and grab indexes i, i + 1, and i + 2 as you go along. The ListViewItem constructor has an overload that takes a string array, use that to define the full ListViewItem text in each of the 3 columns.
Something like this:
Code:
Dim len As Integer = 3
For i As Integer = 0 To SplitArrayLength - len Step len
ListView1.Items.Add(New ListViewItem(New String() {SplitArray(i), SplitArray(i + 1), SplitArray(i + 2)}))
Next
Bit of a late response but that's sorted it.
I've currently got
Code:
Dim splitStr() As String = readStr.Split("#")
Dim SplitArrayLength As Integer = 0
Dim x As String = "#"
For Each x In readStr
SplitArrayLength = SplitArrayLength + 1
Next
Dim Len As Integer = 3
For i As Integer = 0 To SplitArrayLength - Len Step Len
userList.Items.Add(New ListViewItem(New String() {splitStr(i), splitStr(i + 1), splitStr(i + 2)}))
Next
which seems to do the job!
There's probably a more efficient way but when I figure that out I'll change it.
Thanks to both of you.
-
May 26th, 2013, 04:21 PM
#6
Re: Splitting string and adding contents into listview columns.
Just as a tip, you should be defining your array variables like this:
Code:
Dim splitStr As String() = readStr.Split("#")
With the parenthesis after the type, not the variable name itself. MSDN coding conventions will point this out to you, it's better practice.
Last edited by AceInfinity; May 26th, 2013 at 05:15 PM.
<<<------------
.NET Programming (2012 - 2018)
®Crestron - DMC-T Certified Programmer | Software Developer <<<------------
-
May 26th, 2013, 04:30 PM
#7
Re: Splitting string and adding contents into listview columns.
 Originally Posted by AceInfinity
Split("#")
that version of split requires a char parameter:
see MSDN:
http://msdn.microsoft.com/en-us/libr...ing.split.aspx
it's correct practice...
Last edited by .paul.; May 26th, 2013 at 04:35 PM.
- Coding Examples:
- Features:
- Online Games:
- Compiled Games:
-
May 26th, 2013, 04:58 PM
#8
Re: Splitting string and adding contents into listview columns.
 Originally Posted by .paul.
I copied his code I didn't write that.
 Originally Posted by ComYOU
I currently got
Code:
Dim splitStr() As String = readStr.Split("#")
Dim SplitArrayLength As Integer = 0
Dim x As String = "#"
For Each x In readStr
SplitArrayLength = SplitArrayLength + 1
Next
Dim Len As Integer = 3
For i As Integer = 0 To SplitArrayLength - Len Step Len
userList.Items.Add(New ListViewItem(New String() {splitStr(i), splitStr(i + 1), splitStr(i + 2)}))
Next
<<<------------
.NET Programming (2012 - 2018)
®Crestron - DMC-T Certified Programmer | Software Developer <<<------------
-
May 26th, 2013, 05:00 PM
#9
Re: Splitting string and adding contents into listview columns.
 Originally Posted by AceInfinity
I copied his code I didn't write that.
i'd still like to see the MSDN article that says array parentheses should be placed after the type
- Coding Examples:
- Features:
- Online Games:
- Compiled Games:
-
May 26th, 2013, 05:01 PM
#10
Re: Splitting string and adding contents into listview columns.
Alright, as you requested: http://msdn.microsoft.com/en-us/libr.../h63fsef3.aspx
I did not mean any offense. This is coding conventions though directly from MSDN. 
 Originally Posted by Arrays
Put the array designator on the type, not on the variable. For example, use the following syntax:
Code:
Dim letters4 As String() = {"a", "b", "c"}
Last edited by AceInfinity; May 26th, 2013 at 05:06 PM.
<<<------------
.NET Programming (2012 - 2018)
®Crestron - DMC-T Certified Programmer | Software Developer <<<------------
-
May 26th, 2013, 05:10 PM
#11
Re: Splitting string and adding contents into listview columns.
There is no practical reason to follow that misguided convention.
Both parentheses on the array name + parentheses on the type work equally well.
- Coding Examples:
- Features:
- Online Games:
- Compiled Games:
-
May 26th, 2013, 05:12 PM
#12
Re: Splitting string and adding contents into listview columns.
 Originally Posted by .paul.
There is no practical reason to follow that misguided convention.
Both parentheses on the array name + parentheses on the type work equally well.
Why no practical reason? They mention things for reasons. Perhaps they are deciding to remove support for using () after the variable name in future standards of VB? The standard before (VB 2005 and I think VB 2008) mentioned () after the variable name I was told. It has changed, and there must be a reason. Yes, old VB6 methods work still too, but that's not a reason in itself to be using them in VB.net in my opinion.
In C# for instance we don't put [] after the variable name unless we are indexing.
Cheers,
~Ace
<<<------------
.NET Programming (2012 - 2018)
®Crestron - DMC-T Certified Programmer | Software Developer <<<------------
-
May 26th, 2013, 05:14 PM
#13
Re: Splitting string and adding contents into listview columns.
This is coding conventions though directly from MSDN.
Uh-uh! And what do they know?
As the 6-dimensional mathematics professor said to the brain surgeon, "It ain't Rocket Science!"
Reviews: "dunfiddlin likes his DataTables" - jmcilhinney
Please be aware that whilst I will read private messages (one day!) I am unlikely to reply to anything that does not contain offers of cash, fame or marriage!
-
May 26th, 2013, 05:16 PM
#14
Re: Splitting string and adding contents into listview columns.
I'm not going to be starting a war here though... I just thought it was worth mentioning. That's all...
<<<------------
.NET Programming (2012 - 2018)
®Crestron - DMC-T Certified Programmer | Software Developer <<<------------
-
May 26th, 2013, 05:18 PM
#15
Re: Splitting string and adding contents into listview columns.
Perhaps they are deciding to remove support for using () after the variable name in future standards of VB?
In which case they'll have to remove support for sized arrays too! I'm with paul in thinking this just a bit of silly thing to even think about.
As the 6-dimensional mathematics professor said to the brain surgeon, "It ain't Rocket Science!"
Reviews: "dunfiddlin likes his DataTables" - jmcilhinney
Please be aware that whilst I will read private messages (one day!) I am unlikely to reply to anything that does not contain offers of cash, fame or marriage!
-
May 26th, 2013, 05:20 PM
#16
Re: Splitting string and adding contents into listview columns.
It was an example, not my main reason for why it is good/bad as a coding convention. But I do not see MSDN changing their standard convention guidelines (since 2005/2008) without reason like this; a reason most likely exists; unknown at this point. They provide that information "as-is," and that's just the way they say it should be.
In addition to that, I think I should apologize to .paul. here for putting him on the spot like that. It was not my initial intention, and I didn't even know or see that his code reflected that same code I was pointing out in OP's code.
In which case they'll have to remove support for sized arrays too!
No... For instance:
vbnet Code:
Dim arr As String() = New String(5) {}
vbnet Code:
Dim x As Integer() = New Integer(4) {}
Last edited by AceInfinity; May 26th, 2013 at 05:24 PM.
<<<------------
.NET Programming (2012 - 2018)
®Crestron - DMC-T Certified Programmer | Software Developer <<<------------
-
May 26th, 2013, 05:23 PM
#17
Re: Splitting string and adding contents into listview columns.
I just found it strange that you picked up on a matter of coding style + completely ignored an implicit conversion error.
- Coding Examples:
- Features:
- Online Games:
- Compiled Games:
-
May 26th, 2013, 05:25 PM
#18
Re: Splitting string and adding contents into listview columns.
 Originally Posted by .paul.
I just found it strange that you picked up on a matter of coding style + completely ignored an implicit conversion error.
It was left to right reading instinct . I seen () before I missed seeing a c there. But yes, admittedly I didn't see it until you mentioned it. I was more focused on the loop OP was using to reflect indexing of that array instead also.
<<<------------
.NET Programming (2012 - 2018)
®Crestron - DMC-T Certified Programmer | Software Developer <<<------------
-
May 26th, 2013, 05:26 PM
#19
Re: Splitting string and adding contents into listview columns.
Well, that's even sillier!!!!!
As the 6-dimensional mathematics professor said to the brain surgeon, "It ain't Rocket Science!"
Reviews: "dunfiddlin likes his DataTables" - jmcilhinney
Please be aware that whilst I will read private messages (one day!) I am unlikely to reply to anything that does not contain offers of cash, fame or marriage!
-
May 26th, 2013, 05:28 PM
#20
Re: Splitting string and adding contents into listview columns.
 Originally Posted by dunfiddlin
Well, that's even sillier!!!!!
Similar to the way you'd write it in C#:
Just a character less (; is added in C# lol).
When you omit the {} in VB.net though, it thinks you are using the constructor. Granted, that page on MSDN is very odd and unclear at explaining the good conventions for fixed size declarations, which is not good at all.
For example, they say NOT to do this:
Code:
Dim letters6(2) As String
letters6(0) = "a"
letters6(1) = "b"
letters6(2) = "c"
With no alternative.
They also say this:
Do not use the following syntax:
Code:
Dim letters5() As String = {"a", "b", "c"}
Then proceed to say:
Use the { } syntax when you declare and initialize arrays of basic data types. For example, use the following syntax:
Code:
Dim letters5() As String = {"a", "b", "c"}
Notice that these are the same code...
Last edited by AceInfinity; May 26th, 2013 at 05:35 PM.
<<<------------
.NET Programming (2012 - 2018)
®Crestron - DMC-T Certified Programmer | Software Developer <<<------------
-
May 26th, 2013, 05:40 PM
#21
Re: Splitting string and adding contents into listview columns.
my guess is that even the mighty Microsoft are not infallible.
forums like this are invaluable for identifying + pointing out their errors
- Coding Examples:
- Features:
- Online Games:
- Compiled Games:
-
May 26th, 2013, 05:42 PM
#22
Re: Splitting string and adding contents into listview columns.
 Originally Posted by .paul.
my guess is that even the mighty Microsoft are not infallible.
forums like this are invaluable for identifying + pointing out their errors
Nobody is perfect *thumbs up* I agree.
<<<------------
.NET Programming (2012 - 2018)
®Crestron - DMC-T Certified Programmer | Software Developer <<<------------
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|