I am installing visual studio 2010 right now. I’m excited to try the new database features. I heard directly from a Microsoft source that you won’t need to use SQL management studio anymore. I hope it’s true. I’ve also read that VB.NET can now do EVERYTHING that C# can do. They are now a fully merged language. I’m also excited about no longer having to put an underscore (_) at the end of your code line to do line continuation, instead the system recognizes that your line is continuing automatically. That should be really cool. Now I have even more reasons to brag about how VB is better then C#. I’ve always hated semicolons but the C# people would win the argument by saying that line continuation was a hassle in VB, well take that C#, I don’t need a semicolon or a underscore, my language is smarter then yours!
Archive for the ‘VB.NET’ Category
Visual Studio 2010
Posted by Daryl on 05/04/2010
Posted in .NET, C#, SQL, VB.NET, Visual Studio | Leave a Comment »
VB vs C#
Posted by Daryl on 01/05/2010
So I thought that I would throw my opinion out into the C# vs VB.NET debate. Let me say that professionally I have had to program in both languages on a scale of 1 to 10 my skills in VB are a solid 10, but in C# I’d only consider myself a 6 or 7. The thing I usually say when people ask me if I like C# is “No, There are too many semicolons.” It’s funny to me and usually to whomever I’m talking to. But really I have yet to run into a scenario where there is something you can do in C# but you can’t in VB. There are a number of translators on the internet that will translate back and forth from C# to VB and vise versa. So whenever I have to write in C# if I get stuck I write the code out in VB and then translate it to C#.
Since it is easy to call a C# DLL from VB and vise versa, personally I think it does not matter what language you write in. If you have a good multi-tiered architecture you should be able to let one programmer write his tier in VB and another tier in C#, but I have yet to be at a company who thinks this is a good idea. I’m not sure why I guess it’s adding a layer of complexity that project managers do not understand or want to deal with.
So at the end of the day my opionion is that both languages are equal, and you should just write in whatever language you like. I have been writing in BASIC code my entire life, and switching over to C# just seems pointless to me, unless there is a paycheck in it.
Posted in .NET, C#, VB.NET, Visual Studio | Leave a Comment »
Multi-Tiered Architecture
Posted by Daryl on 01/01/2010
Muti-Tiered Architecture is a very important software concept. Whenever I am givien someone elses code to fix my brain explodes when I see All three of the major teirs in the same function. The three tiers are: Presentation, Application, and Database. In my opinion these three areas should never mix. Your UI should never call the database, and your Application Layer should never interact with the UI. Sometimes you will find yourself in a situation where you have to do some business logic in the UI or inside that database tier, but you should never, ever have to mix the Presentation with the database.
Personally I prefer what I call a 4 tier model. I break down the Development into 4 distinct Areas:
UI – (User Interface or Presentation)
BL – (Business Logic or Application, or I even prefer calling it the Logic Layer)
DL – (Data Layer)
DB – (Database)
I break out the database Layer into two parts, the Data Layer, and the Data Base, this is not the actual database that would be a different layer if anything. The purpose of the data Layer is to convert all database objects into your custom business objects. The database layer only returns datasets, data tables, and other database related objects, but not SQL objects. The Database Layer uses SQL objects or whatever type of database your are connecting to, but only passes back the common database objects like datatables and the sort. This allows TRUE database portability. If you want to change from SQL to mySQL or any other database technology you only have to change one small layer of your code. Your UI has NO idea that underneath it all there is a SQL database or a mySQL database, or an XML flat file, it does not know, nor does it care.
I allways create a common library that has the base objects that I use to transfer data. I use the namespace ‘common’. So I use somthing like ‘vbguru.Common.User’ as my common user object. This way all layers can have access to a common object and pass data back and forth using it. I also separate my tiers into at least different files, I don’t mix tiers in one code file, I even prefer to put them into their own projects for what I would call ‘ultimate portability.’ By breaking down your logic into small chunks it makes it easier to reuse. I love the idea of perfecting some logic and creating a DLL and never looking at it’s code again, just add it to a new project as the dll.
Here is an example of some good muti-tiered code. I am putting it all in one file so that it is easy to read here. Normally, I would put each piece in it’s own file.
Namespace vbGuru
Public Class UI
Public Sub UI_Function()
Dim Obj As vbGuru.Common.MyData()
Dim AL As vbGuru.Logic = New vbGuru.Logic
Obj = AL.GetObj
‘Add UI Stuff
End Sub
End Class
End Namespace
Namespace vbGuru.Common
Public Structure MyData
Public id As Integer
Public Name As String
Public Value As String
End Structure
End Namespace
Namespace vbGuru
Public Class Logic
Public Function GetObj() As vbGuru.Common.MyData()
Dim Data As vbGuru.Data = New vbGuru.Data
Dim Obj As vbGuru.Common.MyData() = Data.GetObj()
‘Add any logic that you need to manipulate the object
Return Obj
End Function
End Class
End Namespace
Namespace vbGuru
Public Class Data
Public Function GetObj() As vbGuru.Common.MyData()
Dim DB As vbGuru.DB = New vbGuru.DB
Dim dt As DataTable = DB.GetObj()
If dt Is Nothing Then Return Nothing
‘I don’t think this second line is needed
‘But I allways put it in just in case.
If dt.Rows.Count = 0 Then Return Nothing
Dim Que As Queue(Of vbGuru.Common.MyData) = New Queue(Of vbGuru.Common.MyData)
‘I like to use Queues to avoid for next loops and i’s
For Each drow In dt.Rows
Dim obj As vbGuru.Common.MyData
obj.id = drow("id")
obj.Name = drow("Name")
obj.Value = drow("Value")
Que.Enqueue(obj)
Next
Return Que.ToArray
End Function
End Class
End Namespace
Namespace vbGuru
Public Class DB
Public Function GetObj() As DataTable
Dim cn As System.Data.SqlClient.SqlConnection = New System.Data.SqlClient.SqlConnection("CNSTRING")
Dim cmd As System.Data.SqlClient.SqlCommand = New System.Data.SqlClient.SqlCommand("SELECT * FROM TMP", cn)
Dim ds As System.Data.SqlClient.SqlDataAdapter = New System.Data.SqlClient.SqlDataAdapter(cmd)
Dim dt As DataTable = New DataTable
ds.Fill(dt)
Return dt
End Function
End Class
End Namespace
It may seem like a lot of steps just to do a simple function but by separating it all out it makes your code easier to maintain, easier to make a database change, and easily reusable.
Posted in .NET, VB.NET | Leave a Comment »
Shutting up the warnings
Posted by Daryl on 12/15/2009
Ok ever have some code like this:
Visual studio will warn you:
Warning 2 Function ‘GetMine’ doesn’t return a value on all code paths. A null reference exception could occur at run time when the result is used.’
But your thinking “But I want it to return nothing if there is no object with that ID, so stop bugging me!”
Well here is my little trick to fix that:
Posted in VB.NET | Leave a Comment »
If Not obj is nothing
Posted by Daryl on 12/14/2009
I happen to love “If Not” it even got me in trouble at work. I don’t think it was available in VB6 but I might have just not known about it. Back in my VB6 Days I would often write an if statement like so:
This solves the issue of there not being an ‘isnot’ operator, but it looks bad. Well In .NET you can use “If Not” to solve this:
I got into trouble at work when I used it this way:
My code reviewer made me rewrite it to:
I think it was a fine line of code but I may have just been addicted to If Not, but I admit it is a little confusing.
Posted in VB.NET | Leave a Comment »
Sending An Email using SSL & Comcast
Posted by Daryl on 12/11/2009
Here is something I just figured out, it took me a while because I was trying to use the some older settings that I had found before. Port 587 is the one you want to use with Comcast & .net.
This code works with my comcast account.
Private Sub SendEmail()
Dim smtp As System.Net.Mail.SmtpClient
smtp = New System.Net.Mail.SmtpClient("smtp.comcast.net", 587)
smtp.UseDefaultCredentials = False
'NOTE: USER_NAME does not include the @comcast.net part
smtp.Credentials = New System.Net.NetworkCredential("USER_NAME", "PASSWORD")
smtp.EnableSsl = True
smtp.DeliveryMethod = Net.Mail.SmtpDeliveryMethod.Network
smtp.Send("USER_NAME@COMCAST.NET", "USER@USER.COM", "TEST SUBJECT", "TEST MESSAGE")
End Sub
Posted in VB.NET | Leave a Comment »
Stupid LINQ Error
Posted by Daryl on 12/10/2009
Ok in the end this was my fault, I’m not sure how I messed it up, but I just had to pull my hair out trying to figure out why I was getting this error using LINQ.
System.NotSupportedException: Method ‘System.Object CompareObjectEqual(System.Object, System.Object, Boolean)’ has no supported translation to SQL.
The problem was that for some reason I deleted all my types in my function setting this fixed the problem. This is not something that I ever do, I just messed up a copy and paste process where I renamed the variables, in the process of which I accidentally forgot to give all my variables types, and this makes LINQ puke.
Posted in SQL, VB.NET | Leave a Comment »
The Awesomeness that is WebServices
Posted by Daryl on 12/09/2009
During Bill Gate’s Keynote address in Comdex of the year 2000 he talked a lot about the future of the interenet being found in smart servers powered by web services.
http://www.microsoft.com/Presspass/press/2000/nov00/comdex2000keynotepr.mspx
Now that I have spent many years developing various web services I have to say that I could not agree more with him. When I think of Web Services I think of websites that are designed for computers (not people) to read, with content and decision making power just waiting for another machine to request it.
I once went to a company with a consulting firm, They showed me how they were passing data from their client apps to their server. They were using HTTP POST commands to push tab delimited text data from their client software to their server. I tried really hard to tell them that they were spending countless hours writing communication protocols that were already built into the .NET framework. At the end of the day I lost, I don’t know why they didn’t get it. I think they didn’t want to admit they were wasting all that time. But as for you and me, web services are AWESOME. You can send data from one computer to another with very little effort. You treat the web service just like it’s any other referenced function, passing it a few variables, and getting back the result. No worrying about parcing data or formatting errors.
Another time I was hired by a company to help them create a project which they had designed that relied heavly on Web Services. Well the people they had designing it thought that you could only pass back one value from a web service. So they had designed method after method to return integers or strings, it was a mess. I told them to use Structures to encapsulate as many variables as they wanted in on Web Service Response. They kept me around for a while.
Here is a sample line of code that uses a web service I created, like I said it’s just like calling a function
Posted in VB.NET | Leave a Comment »
Debugging Web Services
Posted by Daryl on 12/08/2009
Have you ever had a project that you are working on that has a web service that it connects to that you are also developing? Have you found debugging the web service a hassle? It’s annoying to have to change the web service reference to your local project every time you need to debug something, but it seems like the only way to do it?
Well here is a simple solution that I came up with where you don’t have change the reference. I bet if I was even more creative I could put it into the web.config file and have it switch servers automatically.
Currently I just add this line and then comment it out once I’m done with it. I imagine the downfall to this approach is if you forget about it your production code is going to crash.
To find the right URL right click on your web service and select “View in Browser”
Posted in VB.NET | Leave a Comment »
The Singleton Pattern
Posted by Daryl on 12/07/2009
Singletons are very nifty. They are objects that can only exist once in memory. I mentioned them before when I was talking about threading. They are the perfect object to act as a a buffer to store the results of a bunch of threads that can also be accessed by the UI. You still need to think about thread safety but because only one copy of the object can exist inside your application all of the threads can use it to share information. You could also use it as a old fashioned ”Global Variable” from your VB6 days, but personally I think that using it for that is bad software design. The main thing that makes something a singleton is making it’s constructor private. How do you create an object with a private constructor? Well check out this code:
I added a property so you could see something you could use this for, and I usually add a SyncLock to make the construction Thread Safe. The SyncLock command will only allow one tread into it at a time. I double up the IF statement to avoid hitting the SyncLock after the Object has been created.
Now there are two easy ways to access this object in your code, I kinda like the second one better because you don’t have to create another variable.
Posted in VB.NET | Leave a Comment »