Click here to Skip to main content
15,890,579 members
Please Sign up or sign in to vote.
2.33/5 (2 votes)
See more:
Firstly, sorry if I don't know the right question to ask but I've tried various google, Code Project and MSDN searches on this one.

I have been learning VB.Net (VS2010) and have come across a couple of walk-throughs that show very different ways of constructing Properties of various Classes. Indeed in this one walk-through the author uses two approaches to class construction without obvious explanation.

http://msdn.microsoft.com/en-AU/library/ms171892%28v=vs.100%29.aspx[^]

That is for the "Customer" class he uses only strings:

VB
Public Sub New(ByVal customerId As String,
                   ByVal companyName As String,
                   '...
                   ByVal phone As String,
                   ByVal fax As String)
        customerIDValue = customerId
        '... country
        phoneValue = phone
        faxValue = fax
    End Sub


For the "Order" Class he uses the actual variable type that reflects the database fields:

VB
Public Sub New(ByVal orderid As Integer,
                   ByVal customerID As String,
                 '...
        shipCountryValue = shipCountry
    End Sub

I can see that genericising the construction like that could save some hassles with, for example, dates. However, can someone help explain why you would use one approach over another.

Even guidance on how to frame a google/ MSDN search on the topic would be appreciated.
Posted
Updated 21-Jul-13 16:16pm
v8
Comments
[no name] 21-Jul-13 21:19pm    
I must admit that I have read this several times and cannot for the life of me figure out what it is that you are asking here. You talk about declaring properties but show examples of class constructors. What do you mean "actual variable type"? A string is a variable type... The author is probably using a integer for orderid because it's an integer. Strings are used for phone and fax numbers because they can contain non digit characters (xxx-xxx-xxxx).

ThePhantomUpvoter is right (I refer to his comment to the question): this is not a valid question, and the code sample is irrelevant to properties. But I wan to make one general note: these days, one absolute fallacy became very typical for the beginners: a string is taken as some universal data type, so people try to work with strings representing data, instead of data itself. This is a really, really bad thing, even in the cases when it works. It actually means deep misunderstanding of programming in general.

In you need integer type, use integer, and so on.

—SA
 
Share this answer
 
Comments
Ron Beyer 21-Jul-13 21:51pm    
+5, never generalize a data type because its easier to read (our brains are wired to work with text, so we tend to push data into text thinking its easier to convert back later), software works much better when you type data how it should be typed from the start.
Sergey Alexandrovich Kryukov 21-Jul-13 22:06pm    
Thank you, Ron. You note makes a good point.
—SA
First of all, don't update your question by adding something as a solution.

I have not spent a lot of time in either of the C# or VB.NET worlds, but am a bit confused with the code. The constructors are not consistent. It doesn't seem to be the same between the C# and VB.NET versions... but anyway, I think the datatypes are the way they are because they make sense (as you've heard here already). The customerID is a string I suppose because it can contain characters, but the orderid is an int. Phone numbers are string because (1) they can require more than 10 digits and (2) They can sometimes contain non-numeric characters.

You could probably rewrite your own version of this with a numeric customerID for example and not raise any eyebrows. All of this just seems to be design decisions (not unusual).

P.S. I really dislike VB.NET and consider it a deficient language. If you are learning it on your own as an arbitrary decision, I'd suggest a more modern language such as C#, Java or C++.
 
Share this answer
 
Comments
Ron Beyer 21-Jul-13 22:56pm    
"consider it a deficient language"... You and me both brother. Many a quality of VB program can be evaluated simply by turning Option Explcit on and attempting to compile... +5.
Woogie2 22-Jul-13 3:58am    
Thanks - I think you're right on that. I've started a bit of reading on C# and really I've only been learning VB as an old fart who was brought up on things like QuickBasic and Fortran. Luckily I make my money elsewhere.
I meant (and have changed) - the actual variable type as used in the database and the associated TableAdapter.

I'm sorry for saying 'declare' instead of 'construct' (I did mention that I'm learning). However, I had assumed that, since this was an MSDN walk-through, and not some beginner, that there might be valid reasons why one might take such a generic approach to constructing the properties.
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900