Note: These functions are for a duel column listview in Visual Basic 6, useing the ReportView.
Public Function AddUser(lvListView As ListView, StrUsername As String, StrStats As String, LngFlags As Long, LngPing As Long)
With lvListView
.ListItems.Add , , StrUsername, , GetIconCode(StrStats, LngFlags)
.ListItems(.ListItems.Count).ListSubItems.Add , , , GetPingCode(LngPing, LngFlags)
End With
End Function
Public Function RemoveUser(lvListView As ListView, StrUsername As String)
Dim usrIndex As Integer
With lvListView
usrIndex = .FindItem(StrUsername).Index
.ListItems.Remove usrIndex
End With
End Function
Public Function ModifyUser(lvListView As ListView, StrUsername As String, StrStats As String, LngFlags As Long, LngPing As Long)
Dim usrIndex As Integer
If StrStats = "" Then StrStats = "TAHC"
With lvListView
usrIndex = .FindItem(StrUsername).Index
.ListItems.Remove usrIndex
.ListItems.Add usrIndex, , StrUsername, , GetIconCode(StrStats, LngFlags)
.ListItems(usrIndex).ListSubItems.Add , , , GetPingCode(LngPing, LngFlags)
End With
End Function
Public Function ClearItems(lvListView As ListView)
lvListView.ListItems.Clear
End Function
Public Function ItemCount(lvListView As ListView) As Integer
ItemCount = lvListView.ListItems.Count
End Function
I'm sure you can make or find your own geticon/getping code, i would post mine but 75% of you would probably get lost in it.
"Oh joy!" for some of you.
Quote from: dRAgoN on July 17, 2004, 05:51 AM\I'm sure you can make or find your own geticon/getping code, i would post mine but 75% of you would probably get lost in it.
"Oh joy!" for some of you.
Uh, they aren't hard to understand, especially if you comment them well.
For example:
Private Function GetPingCode(Flags As Long)
If Flags And &H16 Then GetPingCode = ICON_LAGPLUG: Exit Sub
Select Case
Case 5 To 199 '// Ping of 5 to 300 :o
GetPingCode = ICON_LAG1
Case 200 To 300 '//Ping Of 200 to 300
GetPingCode = ICON_LAG2
Case 301 To 400 '//Ping Of 301 to 400
GetPingCode = ICON_LAG3
Case 401 To 600 '//Ping Of 401 to 600
GetPingCode = ICON_LAG4
Case 601 To 1200 '//Ping Of 601 to 1200
GetPingCode = ICON_LAG5
Case Is > 1200 '//Ping Higher than 1201
GetPingCode = ICON_LAG6
End Select
End Function
If yours is any more complicated than that, I think you should consider rewriting it.
Quote from: ChR0NiC on July 20, 2004, 03:22 AM
Uh, they aren't hard to understand, especially if you comment them well.
For example:
Private Function GetPingCode(Flags As Long)
If Flags And &H16 Then GetPingCode = ICON_LAGPLUG: Exit Sub
Select Case
Case 5 To 199 '// Ping of 5 to 300 :o
GetPingCode = ICON_LAG1
Case 200 To 300 '//Ping Of 200 to 300
GetPingCode = ICON_LAG2
Case 301 To 400 '//Ping Of 301 to 400
GetPingCode = ICON_LAG3
Case 401 To 600 '//Ping Of 401 to 600
GetPingCode = ICON_LAG4
Case 601 To 1200 '//Ping Of 601 to 1200
GetPingCode = ICON_LAG5
Case Is > 1200 '//Ping Higher than 1201
GetPingCode = ICON_LAG6
End Select
End Function
If yours is any more complicated than that, I think you should consider rewriting it.
Can't Exit Sub in a function. Also, > operator is greater than, not greater than or equal to.
Another way, not better, but leaves no holes.
Private Function GetPingCode(Flags As Long)
If Flags And &H16 Then GetPingCode = ICON_LAGPLUG: Exit Sub
Select Case pingval
Case Is < 5:
GetPingCode = ICON_LAG0
Case Is < 201 '// Ping of 5 to 200
GetPingCode = ICON_LAG1
Case Is < 301 '//Ping Of 200 to 300
GetPingCode = ICON_LAG2
Case Is < 401 '//Ping Of 301 to 400
GetPingCode = ICON_LAG3
Case Is < 601 '//Ping Of 401 to 600
GetPingCode = ICON_LAG4
Case Is < 1201 '//Ping Of 601 to 1200
GetPingCode = ICON_LAG5
Case Else '//Ping Higher than 1201
GetPingCode = ICON_LAG6
End Select
End Function
Your Select Case did not have a variable argument, so I made one up.
Anyway, you're using incorrect range (assuming you're trying to emulate Blizzard client). You can use your own ranges, but it might confuse users who are used to the others.
Quote from: Spht on June 20, 2004, 05:53 PM
(ping being an unsigned long)
Ping Is < 10
' No latency bars
Ping Is < 200
' ONE latency bar
Ping Is < 300
' TWO latency bars
Ping Is < 400
' THREE latency bars
Ping Is < 500
' FOUR latency bars
Ping Is < 600
' FIVE latency bars
Ping Is >= 600
' SIX latency bars
Yes I use Sphts way now since its cleaner than mine. Ty Spht.
Quote from: Spht on July 20, 2004, 05:31 PM
Ping Is < 10
' No latency bars
Ping Is < 200
' ONE latency bar
Ping Is < 300
' TWO latency bars
Ping Is < 400
' THREE latency bars
Ping Is < 500
' FOUR latency bars
Ping Is < 600
' FIVE latency bars
Ping Is >= 600
' SIX latency bars
I recommend getting in the habit of using a Case Else, even if you believe you have covered all the possibilities and ranges. It is similar to always providing a default case in C. You never know when you will go back and modify code, and inadvertently stop handling a case. Plus, it is a good habit since more complex cases can leave non-obvious logic holes, which a Case Else would handle, making debugging simpler.
Public Function GetPing(ByVal Ping As Long) As Integer
Select Case Ping
Case Is < 10
' No latency bars
Case Is < 200
GetPing = PING_1G
Case Is < 300
GetPing = PING_2G
Case Is < 400
GetPing = PING_3Y
Case Is < 500
GetPing = PING_4Y
Case Is < 600
GetPing = PING_5R
Case Is >= 600
GetPing = PING_6R
Case Is > 1201
GetPing = PING_6R
Case Is < 0
GetPing = PING_6R
Case Else
GetPing = PING_PLUG
End Select
End Function
Thats my Ping Code I wrote just now.
Edit: Silly me forgot [ code ] tags
Note that what I pasted was merely pseudocode to explain the correct ping range for matching icons which Blizzard's legacy clients use.
Quote from: WaR[KL] on July 21, 2004, 02:22 PM Case Is > 1201
GetPing = PING_6R
The
Is >= 600 check covers your
Is > 1201 check.
Quote from: WaR[KL] on July 21, 2004, 02:22 PM Case Is < 0
GetPing = PING_6R
The
Is < 10 check covers your
Is < 0 check.
Quote from: WaR[KL] on July 21, 2004, 02:22 PM Case Else
GetPing = PING_PLUG
Not sure what you're doing here. You covered all possible cases. A user's UDP state is in their flags, not ping.
Quote from: Spht on July 21, 2004, 02:49 PM
Note that what I pasted was merely pseudocode to explain the correct ping range for matching icons which Blizzard's legacy clients use.
Case Else
GetPing = PING_PLUG
Not sure what you're doing here. You covered all possible cases. A user's UDP state is in their flags, not ping.
Quote
Right - that's exactly my point. You are looking at your code and going "ok great, covered all possible cases", and then not putting in a Case Else. I promise you, that is a dangerous habit to get into. Even when all cases are covered, you should have a Case Else. Someone, you or someone else, will modify your code someday and the changes will leave a hole open, on some Select or some switch, and this failure to have good coding habits will bite you in the ass. Promise!
And the Case Else should always contain a Stop statement. Don't try to handle it; you can't handle a coding error. For this reason, you should never have a valid case end up in a Case Else handler, unless this is really "what should happen for anything unhandled".
Quote from: UserLoser. on July 20, 2004, 08:12 AM
Can't Exit Sub in a function. Also, > operator is greater than, not greater than or equal to.
Sorry Exit Sub just due to force of habit, and VB usually just corrects it for me, kind of made me lazy :P Also I used just the > because I didn't want 1200, only 1201 and higher.
Quote from: Adron on July 22, 2004, 02:43 PM
And the Case Else should always contain a Stop statement. Don't try to handle it; you can't handle a coding error. For this reason, you should never have a valid case end up in a Case Else handler, unless this is really "what should happen for anything unhandled".
You're right of course, and I should do this more like you described.
Case Is >= 600
GetPing = PING_6R
Case Else
Debug.Print "You missed a case in your Select cases..."
Stop
End Select
I put the Plug there because I had nothing more to put there :P . I changed it with all of your suggestions. Thanks.