473,326 Members | 2,108 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,326 software developers and data experts.

Class question

I have a class that accepts String and Long datatypes.

Class Menu

Private FirstTier as String
Private SecondTier as Long

Sub New(ByVal FirstTier As String, _
ByVal SecondTier As Long)
....

Ok...great. this works fine but the problem arises as a result of filling
this from a datareader that reads from a stored procedure in a SQL database.

The database allows nulls for these columns, but I cannot add a null to the
class that is expecting a String or Long, right?

For my immediate problem, I can use IIf(IsDBNull(dr.Item("FirstTier"...
approach, and add a zero-length string if the DB returns a null, or a 0 for
the numeric datatype.

I poked around a saw a couple approaches to this:

- Don't allow nulls in the database (I don't have control over this, and
the stored procedures are already written..so I can't change them).
- I can do the above approach.
- I suppose I can change the String and Long to object datatypes, and
fill them with Nulls, right?

If I do the above approach, I am creating a business rule independent to the
database setup. Let's say I add a zero if there's a NULL for the long
datatype. That would work for my immediate problem, because there are no
zeroes in the database, but downstream, let's say the database does begin
using zeroes. My program would then not work.

My question is..how should I handle this? I don't see a "clean" way out
without making rules that might later contradict changes made to the
database, forcing the program to be rewritten. Ideally, I would like this
class to work similarly to the way the database works. A numeric datatype
constraint is applied to a column, but it can also store nothing. That
doesn't seem to transfer well to my numeric-expecting datatype in my class.

How do developers work around this issue?
Mar 28 '06 #1
6 1389
mrmagoo wrote:
I have a class that accepts String and Long datatypes.

Class Menu

Private FirstTier as String
Private SecondTier as Long

Sub New(ByVal FirstTier As String, _
ByVal SecondTier As Long)
...

My question is..how should I handle this? I don't see a "clean" way out
without making rules that might later contradict changes made to the
database, forcing the program to be rewritten. Ideally, I would like this
class to work similarly to the way the database works. A numeric datatype
constraint is applied to a column, but it can also store nothing. That
doesn't seem to transfer well to my numeric-expecting datatype in my class.

How do developers work around this issue?

Try this:

Class Menu

Private FirstTier as String
Private SecondTier as Long

Sub New(optional ByVal FirstTier As String="", _
optional ByVal SecondTier As object = nothing)

Then test for empty string and no object
Your database allows nulls, so now you do also.

T
Mar 28 '06 #2

"tomb" <to**@technetcenter.com> wrote in message
news:LG**********@bignews1.bellsouth.net...
mrmagoo wrote:
I have a class that accepts String and Long datatypes.

Class Menu

Private FirstTier as String
Private SecondTier as Long

Sub New(ByVal FirstTier As String, _
ByVal SecondTier As Long)
...

My question is..how should I handle this? I don't see a "clean" way out
without making rules that might later contradict changes made to the
database, forcing the program to be rewritten. Ideally, I would like this
class to work similarly to the way the database works. A numeric datatype
constraint is applied to a column, but it can also store nothing. That
doesn't seem to transfer well to my numeric-expecting datatype in my
class.

How do developers work around this issue?

Try this:

Class Menu

Private FirstTier as String
Private SecondTier as Long

Sub New(optional ByVal FirstTier As String="", _
optional ByVal SecondTier As object = nothing)

Then test for empty string and no object
Your database allows nulls, so now you do also.

T


And, by default, SecondTier (the private member) is set to 0 when not
initialized in the constructor or the declartion of the member. So, he's
back at square one.

What we do is use typed datasets instead of creating our own classes that we
fill (in actuality, that is all a typed class is really). Using typed
datasets, if a field value is set to null and we try to access that field
(property), we get an exception. There are methods on the datasets that
allow us to text a typed dataset field to see if it's null
(Is<typedfieldname>Null) that we check to see if it's null. When using this
route, you know when the field is null or not. But, the headache of this is
that you always have to check for nulls if the field allows nulls.

HTH,
Mythran

Mar 28 '06 #3
I'm new at this...and I am missing your point on how to implement.

Can you explain this a bit more and/or give me an example?

Bottom line, are you saying I can't use a class? If so, I really want to use
a class so I can fill the listbox with the object. That way I can retrieve
any one of a number of related elements to the selected item. I am
approaching it this way because in VBA (which I have experience in) I would
fill a listbox with many hidden columns and retrieve the contents that way.
I like how listboxes in VB.Net can accept objects for this reason.

Thank you.
"Mythran" <ki********@hotmail.comREMOVETRAIL> wrote in message
news:eG*************@TK2MSFTNGP09.phx.gbl...

"tomb" <to**@technetcenter.com> wrote in message
news:LG**********@bignews1.bellsouth.net...
mrmagoo wrote:
I have a class that accepts String and Long datatypes.

Class Menu

Private FirstTier as String
Private SecondTier as Long

Sub New(ByVal FirstTier As String, _
ByVal SecondTier As Long)
...

My question is..how should I handle this? I don't see a "clean" way out
without making rules that might later contradict changes made to the
database, forcing the program to be rewritten. Ideally, I would like thisclass to work similarly to the way the database works. A numeric datatypeconstraint is applied to a column, but it can also store nothing. That
doesn't seem to transfer well to my numeric-expecting datatype in my
class.

How do developers work around this issue?

Try this:

Class Menu

Private FirstTier as String
Private SecondTier as Long

Sub New(optional ByVal FirstTier As String="", _
optional ByVal SecondTier As object = nothing)

Then test for empty string and no object
Your database allows nulls, so now you do also.

T


And, by default, SecondTier (the private member) is set to 0 when not
initialized in the constructor or the declartion of the member. So, he's
back at square one.

What we do is use typed datasets instead of creating our own classes that

we fill (in actuality, that is all a typed class is really). Using typed
datasets, if a field value is set to null and we try to access that field
(property), we get an exception. There are methods on the datasets that
allow us to text a typed dataset field to see if it's null
(Is<typedfieldname>Null) that we check to see if it's null. When using this route, you know when the field is null or not. But, the headache of this is that you always have to check for nulls if the field allows nulls.

HTH,
Mythran

Mar 28 '06 #4

"mrmagoo" <-> wrote in message news:em**************@tk2msftngp13.phx.gbl...
I'm new at this...and I am missing your point on how to implement.

Can you explain this a bit more and/or give me an example?

Bottom line, are you saying I can't use a class? If so, I really want to
use
a class so I can fill the listbox with the object. That way I can retrieve
any one of a number of related elements to the selected item. I am
approaching it this way because in VBA (which I have experience in) I
would
fill a listbox with many hidden columns and retrieve the contents that
way.
I like how listboxes in VB.Net can accept objects for this reason.

Thank you.

Yes. You'll probably like using typed datasets for this...here is what you
can do:

To create a typed dataset:
Add New Item->DataSet (give the dataset a name, typically the entity name
(table name) of the table...in this example, we will use tblCustomer table,
so the dataset name would be Customer).
In Server Explorer window (View->Server Explorer), create a connection that
points to the database you are using.
Expand the database, then Tables, and drag and drop the table to the dataset
designer.
Save the dataset. Voila, a new typed dataset has been created.

Easiest just to fill the datatable with the data (class created under the
dataset) and use the datatable to bind to dropdown listboxes.

Normally, I use stored procedures and the Microsoft Patterns and Practices
Enterprise Library assemblies to do this, but for simplicity in this case,
I'll just use SQL and a modified version of the Northwind database. Also,
the connection string would go into a config file or somewhere other than
code (so it's configurable on a per-machine basis):

Private Sub PopulateCustomers()
Const CONN_STR As String = _
"server=testing;" & _
"database=dbNorthwind;" & _
"trusted_connection=true;"
Dim conn As SqlConnection = New SqlConnection(CONN_STR)
Dim adap As SqlDataAdapter = New SqlDataAdapter( _
"select * from tblCustomer", _
conn _
)

' Create the datatable to fill
Dim dt As Customer.tblCustomerDataTable
dt = (New Customer()).tblCustomer

' Open the connection to the database.
conn.Open()

Try
' Fill the datatable.
adap.Fill(dt)
Finally
' Cleanup
conn.Close()
End Try

' Bind the data table to the listbox.
lstCustomers.DataTextField = "CompanyName"
lstCustomers.DataValueField = "CustomerId"
lstCustomers.DataSource = dt
lstCustomers.DataBind()
End Sub

HTH! :)

Mythran
Mar 28 '06 #5

"mrmagoo" <-> wrote in message
news:%2****************@TK2MSFTNGP09.phx.gbl...
I have a class that accepts String and Long datatypes.

Sub New(ByVal FirstTier As String, _
ByVal SecondTier As Long)

Ok...great. this works fine but the problem arises as a result of filling
this from a datareader that reads from a stored procedure in a SQL
database. The database allows nulls for these columns, but I cannot add a null
to the class that is expecting a String or Long, right?


Sounds like you need another constructor that takes the datareader
as its argument:

Sub New(ByVal dr As ... )
If source.Item( "FirstTier" ) = DBNull.Value Then
FirstTier = dr.Item( "FirstTier" ).ToString()
Else
FirstTier = Nothing ' say
End If
. . .

Regards,
Phill W.
Mar 29 '06 #6
Thanks everyone for your input...you have all been extremely helpful!
"mrmagoo" <-> wrote in message
news:%2****************@TK2MSFTNGP09.phx.gbl...
I have a class that accepts String and Long datatypes.

Class Menu

Private FirstTier as String
Private SecondTier as Long

Sub New(ByVal FirstTier As String, _
ByVal SecondTier As Long)
...

Ok...great. this works fine but the problem arises as a result of filling
this from a datareader that reads from a stored procedure in a SQL database.
The database allows nulls for these columns, but I cannot add a null to the class that is expecting a String or Long, right?

For my immediate problem, I can use IIf(IsDBNull(dr.Item("FirstTier"...
approach, and add a zero-length string if the DB returns a null, or a 0 for the numeric datatype.

I poked around a saw a couple approaches to this:

- Don't allow nulls in the database (I don't have control over this, and the stored procedures are already written..so I can't change them).
- I can do the above approach.
- I suppose I can change the String and Long to object datatypes, and
fill them with Nulls, right?

If I do the above approach, I am creating a business rule independent to the database setup. Let's say I add a zero if there's a NULL for the long
datatype. That would work for my immediate problem, because there are no
zeroes in the database, but downstream, let's say the database does begin
using zeroes. My program would then not work.

My question is..how should I handle this? I don't see a "clean" way out
without making rules that might later contradict changes made to the
database, forcing the program to be rewritten. Ideally, I would like this
class to work similarly to the way the database works. A numeric datatype
constraint is applied to a column, but it can also store nothing. That
doesn't seem to transfer well to my numeric-expecting datatype in my class.
How do developers work around this issue?

Mar 29 '06 #7

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

8
by: Bryan Parkoff | last post by:
I find an interesting issue that one base class has only one copy for each derived class. It looks like that one base class will be copied into three base classes while derived class from base...
20
by: modemer | last post by:
Question is as in subject. For example: class BaseClass { public: void func() { do something; } // I don't want this function being overloaded in its inherited class };
1
by: Alfonso Morra | last post by:
if I have a class template declared as ff: (BTW is this a partial specialization? - I think it is) template <typename T1, myenum_1 e1=OK, my_enum_2=NONE> class A { public: A(); virtual...
21
by: Jon Slaughter | last post by:
I have a class that is basicaly duplicated throughout several files with only members names changing according to the class name yet with virtually the exact same coding going on. e.g. class...
0
by: Sebastian Hiller | last post by:
Hello, i'm new to .Net (i'm using VB as language and i'm working in the code-behind mode) and i can't solve the following problem: I have a WebForm and want to Add a UserControl...
6
by: rodchar | last post by:
Hey all, I'm trying to understand Master/Detail concepts in VB.NET. If I do a data adapter fill for both customer and orders from Northwind where should that dataset live? What client is...
2
by: Chris Puncher | last post by:
Hi. I have a RCW class that was generated by the VS.NET2003 IDE when I added a COM dll reference to my project. This all works fine when I call methods on it. My initial problem is that in...
9
by: needin4mation | last post by:
I have a .aspx file and it's src. I also have a third file, a class that the src references. Everything is in the same directory/folder. Everything has the same namespace. How do I reference the...
43
by: Tony | last post by:
I'm working with GUI messaging and note that MFC encapsulates the message loop inside of a C++ class member function. Is this somehow inherently less robust than calling the message loop functions...
2
by: K Viltersten | last post by:
Suppose there is the following class structure. class S {...} class A : S {...} class B : S {...} class A1 : A {void m(){...}} class B1 : B {void m(){...}} class B2 : B {...} Just to clarify...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
1
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.