Question ComboBox not filled with DataTable - Why?

VBobCat

Well-known member
Joined
Sep 6, 2011
Messages
137
Location
S?o Paulo, Brazil
Programming Experience
3-5
I used this code:

Friend Sub PopulateCB(ByRef mycontrol As ComboBox, _
                      ByVal expressionSQL As String)
    ' DMBD is my data-handling class:
    Using DataTableOrigem As DataTable = DMBD.Read_a_Table(expressionSQL)
        ' this MsgBox told me my Table is correctly filled:
        MsgBox(DataTableOrigem.Rows(0).Item(0).ToString) 
        ' this MsgBox told me my control was correctly passed:
        MsgBox(mycontrol.Name)
        mycontrol.DataSource = DataTableOrigem
        mycontrol.ValueMember = DataTableOrigem.Columns(0).ColumnName
        mycontrol.DisplayMember = DataTableOrigem.Columns(1).ColumnName
    End Using
End Sub


Nevertheless, mycontrol remains empty after this.

Could someone help me figure out why?

Thank you very much!
 
Using statement disposes the used resource at end of block, so you have disposed the DataTable.
 
[SOLVED] ComboBox not filled with DataTable - That's why!

Well, in my country there is a saying for when someone does something dumb, and someone else shows it. I did the dumb thing, so now "I feel the donkey's ears growing up in my head...".

Talking seriously now, thank you so much! I've already tested the fixed code. It made all ok and it is, also, far more elegant:

Friend Sub PopulateCB(ByRef mycontrol As ComboBox, ByVal expressionSQL As String)
     mycontrol.DataSource = DMBD.Read_a_Table(expressionSQL)
     mycontrol.ValueMember = DirectCast(mycontrol.DataSource, DataTable).Columns(0).ColumnName
     mycontrol.DisplayMember = DirectCast(mycontrol.DataSource, DataTable).Columns(1).ColumnName
End Sub
 
Last edited:
I think using a variable would make the code more readable:
Dim table = DMBD.Read_a_Table(expressionSQL)
mycontrol.DataSource = table
mycontrol.ValueMember = table.Columns(0).ColumnName
mycontrol.DisplayMember = table.Columns(1).ColumnName

perhaps With block also:
Dim table = DMBD.Read_a_Table(expressionSQL)
With mycontrol
    .DataSource = table
    .ValueMember = table.Columns(0).ColumnName
    .DisplayMember = table.Columns(1).ColumnName
End With

Variable 'table' here uses type inference, which means the type is inferred from the type of the assigned expression, so that function would need to explicitly have return type DataTable or derived.
 
Yes, I am quite new to VB.NET (my past experience was all in MS-Office VBA, which is not so bound to the type concept), so I decided to make all my new code relying on "Option Explicit On", "Option Strict On" and "Option Infer Off". I am aware that this demands me additional care and effort, but it also forces me to learn how to deal with the strongly-typed framework of .NET, and prevents me to fall into some trap of inadequate inference.

All this to say that, like all my custom functions and properties, the "Read_a_Table" function in my Class "DMBD" returns a strongly-typed object (in this case, a DataTable).

Last but not least, I would like to ask this: readability apart, is there some problem or issue about setting mycontrol's DataSource property directly with Read_a_Table's return, with no variable in-between?
 
Last edited:
Option Infer Off will only exercise your keyboarding skills, and will also prevent you from using newer VB features.
Option Infer On is strongly typed by having the compiler decide what type a variable has, hovering that variable will tell you its type, also when using that variable further in code it will behave strongly typed all the way.
Option Infer will actually give you better feedback and improve on learning the type system, meaning that you don't need to know what type an expression is when declaring the variable, compiler will tell you the resulting type always, you just need to pay some attention and have interest in what goes on.
Option Infer also encourages better coding practices like declaring variables within the scope they are needed when assigment takes place.
There is no such thing as "inadequate inference", since type inference is strongly typed any misuse of it by wrong assumptions will generate compiler errors.

Option Strict and Option Explicit are good things to enable and does not conflict with Option Infer. Option Strict will also help Option Infer by enforcing explicit return types for functions and properties, preventing accidental Object type returns.
is there some problem or issue about setting mycontrol's DataSource property directly with Read_a_Table's return, with no variable in-between?
Not at all, my suggestion of using variable was simply for readability of following code, simplifying the code used for repeated referrals to the same object. Generally though, beginners will in many occations use all too many variables, making the code unnecessary 'detached', 'lenghty' and 'wordy', where direct and chained calls using the dot notation would have sufficed.
 
I really thank you for these hints, they are most valuable for one who, like me, is still learning VB.NET.
As I said before, I come from Office/VBA, where I made some nice apps inside Excel and linking with Access and Word. However, and in spite they have bravely served its purpose in my Job for years far beyond I imagined when I started creating them, I am no professional programmer, and therefore I don't have all the skills that only intense coding work could develop. I bought a very nice book about VB.NET (I'm not sure if naming it goes against forum's rules) but even that doesn't clear the whole path.
If I may ask, what exactly will be unleashed when I select "Option Infer Off"?
Thanks again! Kind regards!
 
Option Infer Off with Option Strict On requires you to always declare local variables with As clause. As explained in my opinion that is counter-productive, and will give you great grief when working with Linq/lambda expressions and sometimes will be impossible to do.
 
I had a taste of what you say now, when I assembled some Linq expressions in my new project. All my previous and most of my present data handling rely on SQL-string expressions through ADO. And in spite of having seen this expression here and there on my new book, I'm still unaware of what lambda expressions really are. Shame on me. Do you know if are there in the Web some introductory texts I could read about those things? Thanks again, of course.
 
Back
Top