Help me clean up this code

  • Thread starter Thread starter Bob Graham
  • Start date Start date
B

Bob Graham

'create object array
Dim jobnums(1000)

Omitted: code that retrieves job numbers (integers) from sql server to put in a combobox
I have a combobox (cbJob) that I want to put these values into. If I read them in from a SqlDataReader it takes
an eternity, about 7 seconds to accomplish. But if I read them into an array, it takes no measurable time.
Then I add the array items to the combobox all at once with the ..AddRange method of the combobox thus:

cbJob.Items.AddRange(jobnums)

This also takes no measurable time, and works fine.
My question is, how should I alter this so I can turn Option Strict back on?
Option strict requires a type declaration when declaring the array:
'So:
Dim jobnums(1000) as Integer

And then you can't assign the array to the .Addrange method, because the combobox wants objects, not integers.

Is there a syntax for Ctype(???) that works to convert as array of integers to objects?

Thanks, Bob Graham
 
Seems easy, but wouldn't a plain
Dim jobnums(1000) As Integer
Dim blah As Object = CObj(jobnums(1000))
work?
-----Original Message-----
'create object array
Dim jobnums(1000)

Omitted: code that retrieves job numbers (integers)
from sql server to put in a combobox
I have a combobox (cbJob) that I want to put these
values into. If I read them in from a SqlDataReader it
takes
an eternity, about 7 seconds to accomplish. But if I
read them into an array, it takes no measurable time.
Then I add the array items to the combobox all at once
with the ..AddRange method of the combobox thus:
cbJob.Items.AddRange(jobnums)

This also takes no measurable time, and works fine.
My question is, how should I alter this so I can turn Option Strict back on?
Option strict requires a type declaration when declaring the array:
'So:
Dim jobnums(1000) as Integer

And then you can't assign the array to the .Addrange
method, because the combobox wants objects, not integers.
Is there a syntax for Ctype(???) that works to convert
as array of integers to objects?
 
Bob Graham said:
Is there a syntax for Ctype(???) that works to convert as array of
integers to objects?

I think you need to create a new object array and a loop to fill it from the
integer array.
 
CMG said:
Seems easy, but wouldn't a plain
Dim jobnums(1000) As Integer
Dim blah As Object = CObj(jobnums(1000))

Actually,

Dim jobnums(1000) As Object
cbJob.Items.Addrange(jobnums)


Works fine, just seems sloppy to me to use object variables until you have
to.

Bob Graham
 
I think this is essentially what you are looking for:

Dim intO() As Object = New Object() {1, 2, 3, 4, 5, 6, 7}

Debug.Assert(Not intO Is Nothing)

ComboBox1.Items.AddRange(intO)





'create object array
Dim jobnums(1000)

Omitted: code that retrieves job numbers (integers) from sql server to put in a combobox
I have a combobox (cbJob) that I want to put these values into. If I read them in from a SqlDataReader it takes
an eternity, about 7 seconds to accomplish. But if I read them into an array, it takes no measurable time.
Then I add the array items to the combobox all at once with the ..AddRange method of the combobox thus:

cbJob.Items.AddRange(jobnums)

This also takes no measurable time, and works fine.
My question is, how should I alter this so I can turn Option Strict back on?
Option strict requires a type declaration when declaring the array:
'So:
Dim jobnums(1000) as Integer

And then you can't assign the array to the .Addrange method, because the combobox wants objects, not integers.

Is there a syntax for Ctype(???) that works to convert as array of integers to objects?

Thanks, Bob Graham
 
Bob,

This is probably obvious but why would you fill a combo box with 1000 items,
this does not seem to be user friendly nor efficient?

Why not make a text box where the user can enter a partial job number and
then make your search on the partial number entered. You could even do the
search while they are entering each individual number.

Dan


'create object array
Dim jobnums(1000)

Omitted: code that retrieves job numbers (integers) from sql server to put
in a combobox
I have a combobox (cbJob) that I want to put these values into. If I read
them in from a SqlDataReader it takes
an eternity, about 7 seconds to accomplish. But if I read them into an
array, it takes no measurable time.
Then I add the array items to the combobox all at once with the .AddRange
method of the combobox thus:

cbJob.Items.AddRange(jobnums)

This also takes no measurable time, and works fine.
My question is, how should I alter this so I can turn Option Strict back on?
Option strict requires a type declaration when declaring the array:
'So:
Dim jobnums(1000) as Integer

And then you can't assign the array to the .Addrange method, because the
combobox wants objects, not integers.

Is there a syntax for Ctype(???) that works to convert as array of integers
to objects?

Thanks, Bob Graham
 
Hi Bob,

Try this and be suprised

:-)

Cor

\\\
Dim a(1000) As Integer
Private Sub Form2_Load(ByVal sender As Object, _
ByVal e As System.EventArgs) Handles MyBase.Load
Dim a(1000) As Integer
For i As Integer = 0 To a.Length - 1
a(i) = i
Next
End Sub
Private Sub Button1_Click(ByVal sender As Object, _
ByVal e As System.EventArgs) Handles Button1.Click
Dim dt As New DataTable
Dim dc As New DataColumn("num")
dt.Columns.Add(dc)
For i As Integer = 0 To a.Length - 1
dt.Rows.Add(dt.NewRow)
dt.Rows(i)("num") = a(i)
Next
Me.ComboBox1.DataSource = dt
Me.ComboBox1.DisplayMember = ("num")
End Sub
//
 
Solex,
This is probably obvious but why would you fill a combo box with 1000 items,
this does not seem to be user friendly nor efficient?

I use a derived combobox that has AutoComplete well sorted out. It performs
quite well and allows users to cursor up or down a job, or to accept partly
typed entry as soon as AutoComplete is showing the job number they want.
Why not make a text box where the user can enter a partial job number and
then make your search on the partial number entered. You could even do the
search while they are entering each individual number.

Dan


'create object array
Dim jobnums(1000)
.......
 
hmmmm, interesting reply, I didn't know you could declare your "For" variable on the fly.

Cor said:
Hi Bob,

Try this and be suprised

:-)

Cor

\\\
Dim a(1000) As Integer
Private Sub Form2_Load(ByVal sender As Object, _
ByVal e As System.EventArgs) Handles MyBase.Load
Dim a(1000) As Integer
For i As Integer = 0 To a.Length - 1
a(i) = i
Next
End Sub
Private Sub Button1_Click(ByVal sender As Object, _
ByVal e As System.EventArgs) Handles Button1.Click
Dim dt As New DataTable
Dim dc As New DataColumn("num")
dt.Columns.Add(dc)
For i As Integer = 0 To a.Length - 1
dt.Rows.Add(dt.NewRow)
dt.Rows(i)("num") = a(i)
Next
Me.ComboBox1.DataSource = dt
Me.ComboBox1.DisplayMember = ("num")
End Sub

I thought of binding the Combo to a datatable, but I'm really only using the combo to pick a job for the purpose of filtering a datagrid by re-wording its select command.

This is now working quite well, with Option Strict enabled:

Dim jobnums(1000) As Object '<== I dont' like using Object Variables

Dim i As Integer
Dim cmdJobJums As New SqlCommand("Select JobName from Jobs where JobName " & _
"is not null and salesVol > '1/1/2003'", SqlConnection1)
SqlConnection1.Open()
drJobNums = cmdJobJums.ExecuteReader
Do While drJobNums.Read
jobnums(i) = drJobNums("JobName")
i += 1
If i Mod 1000 = 0 Then ReDim Preserve jobnums(i + 1000)
Loop
ReDim Preserve jobnums(i - 1)
drJobNums.Close()
SqlConnection1.Close()
cbJob.Items.AddRange(jobnums) '<== this is virtually instantaneous

I just feel sloppy using object variables, although it seems that that's what the combo wants passed to its AddRange method anyway.

I also have to look at my redim statement when the count reaches a thousand, I think I'm creating an "off by one" scenario.

Believe it or not, on a fast 2.4 Ghz p4, adding 1600 numbers to a combobox from a datareader took 7 seconds! With this method it's less than 1 second.

Bob
 
Hi Bob,

I changed it here in this message direct in the datatable I did not test it.
Dim dt As New DataTable
Dim dc As New DataColumn("JobName")
Dim cmdJobJums As New SqlCommand("Select JobName from Jobs where JobName " & _
"is not null and salesVol > '1/1/2003'", SqlConnection1)
SqlConnection1.Open()
drJobNums = cmdJobJums.ExecuteReader
Do While drJobNums.Read
dim dr as datarow = dt.newrow
dr("Jobname"") = drJobNums("JobName")
dt.Rows.Add(dr)
Loop
drJobNums.Close()
SqlConnection1.Close()
Me.ComboBox1.DataSource = dt
Me.ComboBox1.DisplayMember = ("JobName")

I think this goes faster than that it was.


Cor
 
* "Bob Graham" <rvgtech@pacbell.net> scripsit:
[...]

Your message is "unreadable" for me. Please do not post in rich text format.
 
* "CMG said:
Seems easy, but wouldn't a plain
Dim jobnums(1000) As Integer
Dim blah As Object = CObj(jobnums(1000))
work?

This will only convert the element with index 1000...
 
Bob Graham said:
hmmmm, interesting reply, I didn't know you could declare your "For"
variable on the fly.

Bob,

I hope someone else will correct me if this is wrong, but I believe
that only works in Visual Studio 2003.

Charlie
 
Sorry about that, I wasn't sure if many used newsgroups in plain text only.

I find that if I paste code into a plain text message, it displays with
double line spacing, what am I doing wrong?

Bob
 
Back
Top