VB.Net - Creating Objects Question

R

Richard Thornley

Hello,



I need some clarification in creating objects. Consider the following
code... (note: The function InitializeListCombo initializes the combobox)



Private daLists As New OleDbDataAdapter

Private dsLists As New DataSet

Private dvLists As New DataView



msSQL = "SELECT * FROM Lists WHERE Active=True ORDER BY ListName,
ListItem"



'Create the DataAdapter for Categories Recordset

daLists = New OleDbDataAdapter(msSQL, cnADOConverse)



'Fill the Categories Recordset

daLists.Fill(dsLists, "Lists")



'Bind and Initialize cmbPreferredCurrency

dvLists = New DataView(dsLists.Tables("Lists"), "ListName =
'preferredcurrency'", "ListItem", DataViewRowState.CurrentRows)

InitializeListCombo(cmbPreferredCurrency, dvLists, "ListItem",
"ListItem")



'Bind and Initialize cmbHitCounterStyle

dvLists = New DataView(dsLists.Tables("Lists"), "ListName =
'hitcounterstyle'", "ListItem", DataViewRowState.CurrentRows)

InitializeListCombo(cmbHitCounterStyle, dvLists, "ListItem", "ListItem")



'Bind and Initialize cmbDuration

dvLists = New DataView(dsLists.Tables("Lists"), "ListName = 'duration'",
"ListItem", DataViewRowState.CurrentRows)

InitializeListCombo(cmbDuration, dvLists, "ListItem", "ListItem")



'Bind and Initialize cmbAutoRelists

dvLists = New DataView(dsLists.Tables("Lists"), "ListName =
'autorelists'", "ListItem", DataViewRowState.CurrentRows)

InitializeListCombo(cmbAutoRelists, dvLists, "ListItem", "ListItem")



daLists.Dispose()

daLists = Nothing

dsLists.Dispose()

dsLists = Nothing

dvLists.Dispose()

dvLists = Nothing



Did I just create ONE Dataview object named dvLists or did I create FOUR
DataView objects all named dvLists? I am very confused about the New
keyword. I declare dvLists New in the Private statement and then four other
times.



If anyone could make this clear I would really appreciate it. Also... can
anyone tell me if the above code is structured correctly or a better way to
write the code if it is not correct?



Thanks,

Richard
 
C

Cor Ligthert

Richard,

When you have a question with code, please paste it first in a notebook,
copy it back and paste it than in a message, otherwise it is almost
impossible without more work for the one who tries to help you, than for you
to get quick a good idea about your problem.

However comming to your question see it as this
dim a as new dataview(dt)
This create a new dataview object

dim b as dataview
b = a

Now are a and b referencing to the same object which was original a.

I hope this helps?

Cor
 
O

One Handed Man \( OHM - Terry Burns \)

dvList is a DataView Type identifier, when you assign the reference to the
object, the old object referenced by dvList will be disposed of provided no
other reference points to it.

HTH
--
OHM ( Terry Burns ) * Use the following to email me *

Dim ch() As Char = "ufssz/cvsotAhsfbuTpmvujpotXjui/OFU".ToCharArray()
For i As Int32 = 0 To ch.Length - 1
ch(i) = Convert.ToChar(Convert.ToInt16(ch(i)) - 1)
Next
Process.Start("mailto:" & New String(ch))
 
M

Mohamoss

hi Richard

You shouldn't be using the new keyword more that once with the same object
what the new words do is that it allocate memory with the specified type
size to the variable that it is used against . so what you are doing here
is allocating memory place for the dataview while using the same reference
of an old one . if the GC is clever enough it will collect the old memory
item ( that now has no reference , no variable is used to access that
memory ) , however , it is by no mean a good practice. My suggestion to you
is that you either use a different variable name for each view , or you
use the properties of the dataview object to set it new pointed data.
Hope this helps

Mohamed Mahfouz
MEA Developer Support Center
ITworx on behalf of Microsoft EMEA GTSC
 

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