Is there a better way to do this

  • Thread starter kevininstructor
  • Start date
K

kevininstructor

This code populates a ComboBox fine, my question is there a better way then
shown to format the drives letters?

Dim Drives As [String]() = Environment.GetLogicalDrives()
Dim iCount As Integer
Dim i As Integer

iCount = System.Convert.ToInt32(Drives.GetLongLength(0))
If iCount > 0 Then
For i = 0 To iCount
Drives(i) = "(" & Replace(Drives(i), ":\", "") & ")"
Next
ComboBox1.Items.AddRange(Drives)
ComboBox1.SelectedIndex = 0
End If
 
C

Cor Ligthert

Kevin,

What is a better way, you use in my opinion to much code and it fills the
combobox to often, I would do it like this:

\\\\VBNet 2003
Dim Drives As String() = Environment.GetLogicalDrives()
Dim icount As Integer = _
System.Convert.ToInt32(Drives.GetLongLength(0))
For i As Integer = 0 To icount - 1 'this is in VBNet 2002 a little bit else
Drives(i) = "(" & Replace(Drives(i), ":\", ")")
Next
ComboBox1.Items.AddRange(Drives)
ComboBox1.SelectedIndex = 0
////
I hope this helps

Cor
 
K

kevininstructor

Thanks Cor, you are correct about having one line to many in regards to
iCount.

Cor Ligthert said:
Kevin,

What is a better way, you use in my opinion to much code and it fills the
combobox to often, I would do it like this:

\\\\VBNet 2003
Dim Drives As String() = Environment.GetLogicalDrives()
Dim icount As Integer = _
System.Convert.ToInt32(Drives.GetLongLength(0))
For i As Integer = 0 To icount - 1 'this is in VBNet 2002 a little bit else
Drives(i) = "(" & Replace(Drives(i), ":\", ")")
Next
ComboBox1.Items.AddRange(Drives)
ComboBox1.SelectedIndex = 0
////
I hope this helps

Cor

This code populates a ComboBox fine, my question is there a better way
then
shown to format the drives letters?

Dim Drives As [String]() = Environment.GetLogicalDrives()
Dim iCount As Integer
Dim i As Integer

iCount = System.Convert.ToInt32(Drives.GetLongLength(0))
If iCount > 0 Then
For i = 0 To iCount
Drives(i) = "(" & Replace(Drives(i), ":\", "") & ")"
Next
ComboBox1.Items.AddRange(Drives)
ComboBox1.SelectedIndex = 0
End If
 
J

Jay B. Harlow [MVP - Outlook]

Kevin,
In addition to the other comments:

Seeing as the drives are returned in the form of "<drive letter>:\", I would
use SubString instead of replace:

Something like:

Dim drives() As String = System.IO.Directory.GetLogicalDrives()
For index As Integer = 0 To drives.Length -1
drives(index) = drives(index).SubString(0, 1)
Next
ComboBox1.DataSource = drives

Note: Directory.GetLogicalDrives & Environment.GetLogicalDrives return the
same information. Directory.GetLogicalDrives states that the returned
strings are in the "<drive letter>:\" format, while
Environment.GetLogicalDrives does not. However I would expect one to be
implemented in terms of the other...

Also you can simply set the DataSource to an array rather then use AddRange.
The advantage of DataSource is if you change the values in the array the
combo box changes, The advantage of AddRange is it copies the contents of
the array.

You really don't need to use GetLongLength, as it is extremely unlikely that
you will have more then Integer.MaxValue (2G) logical drives on your
computer. Besides you are converting it to an integer any way, I simply used
the Integer Length property (saving the conversion).

.Length is short hand for .GetLength(0)
.LongLength is short hand for .GetLongLength(0)

Hope this helps
Jay
 
K

kevininstructor

Thanks Jay for your insight, would not have looked at DataSource until you
mentioned it.


Jay B. Harlow said:
Kevin,
In addition to the other comments:

Seeing as the drives are returned in the form of "<drive letter>:\", I would
use SubString instead of replace:

Something like:

Dim drives() As String = System.IO.Directory.GetLogicalDrives()
For index As Integer = 0 To drives.Length -1
drives(index) = drives(index).SubString(0, 1)
Next
ComboBox1.DataSource = drives

Note: Directory.GetLogicalDrives & Environment.GetLogicalDrives return the
same information. Directory.GetLogicalDrives states that the returned
strings are in the "<drive letter>:\" format, while
Environment.GetLogicalDrives does not. However I would expect one to be
implemented in terms of the other...

Also you can simply set the DataSource to an array rather then use AddRange.
The advantage of DataSource is if you change the values in the array the
combo box changes, The advantage of AddRange is it copies the contents of
the array.

You really don't need to use GetLongLength, as it is extremely unlikely that
you will have more then Integer.MaxValue (2G) logical drives on your
computer. Besides you are converting it to an integer any way, I simply used
the Integer Length property (saving the conversion).

.Length is short hand for .GetLength(0)
.LongLength is short hand for .GetLongLength(0)

Hope this helps
Jay

This code populates a ComboBox fine, my question is there a better way
then
shown to format the drives letters?

Dim Drives As [String]() = Environment.GetLogicalDrives()
Dim iCount As Integer
Dim i As Integer

iCount = System.Convert.ToInt32(Drives.GetLongLength(0))
If iCount > 0 Then
For i = 0 To iCount
Drives(i) = "(" & Replace(Drives(i), ":\", "") & ")"
Next
ComboBox1.Items.AddRange(Drives)
ComboBox1.SelectedIndex = 0
End If
 
J

Jay B. Harlow [MVP - Outlook]

Kevin,
Remember that .NET is fully object oriented, which means all types have
specific methods, properties, and events.

For example in the case of Replace, there are four Replace functions in
..NET:

I would use Microsoft.VisualBasic.Strings.Replace when I needed the extra
options that it offered .

I would use System.String.Replace when I only had one replacement to do
based on a single character or specific word.

I would use System.Text.StringBuilder.Replace when I had multiple
replacements to do based on single characters or specific words. Where the
characters or word replacements were 1 for 1.

I would use System.Text.RegularExpressions.RegEx.Replace when I needed to
replace based on varying patterns. For example replace repeating white space
with a comma.

Hope this helps
Jay
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top