4 Replies - 516 Views - Last Post: 02 October 2016 - 05:52 AM Rate Topic: -----

#1 IxGRpPafwang  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 11
  • Joined: 11-January 13

Couple of questions concering VB.net

Posted 30 September 2016 - 05:08 AM

I'm trying to get a better understanding of vb.net so I have several fairly easy questions for the vb.net pro's here.

- I often see people declare the variable type behind each Dim, I think it's called strong typing. I'm guessing the benefit is that it allows for less bugs because variables won't accept other invalid values. Are there any other benefits to this?
- What is the best way for me to debug my application? I've been using msgbox, which can be very intrusive and even influence the way the code works at times. I know about console.writeline, but this only shows the results after I close the application. Is there a better way to debug?'
- I notice people use try, catch, final in their code a lot. When is it appropriate for me to use this? I don't think I've ever needed this for anything but perhaps i'm missing out on something.
- What is the best way for me to organise code? Right now I have made some classes so I have some code in them. And I've made a framework class with general helper functions but apart from that all my code happens in a single form which is now filled with event handlers, initializing the application and basically everything that happens. Surely there's a better way to organise the code?

Is This A Good Question/Topic? 0
  • +

Replies To: Couple of questions concering VB.net

#2 modi123_1  Icon User is online

  • Suitor #2
  • member icon



Reputation: 13567
  • View blog
  • Posts: 54,127
  • Joined: 12-June 08

Re: Couple of questions concering VB.net

Posted 30 September 2016 - 06:48 AM

re #1: Code clarity, strong typing, and stricter writing skills. Yes those are all important benefits.
re #2: Breakpoints, variable watch windows, and so on. There's a tutorial on that.
http://www.dreaminco...4249-debugging/
re #3: I typically use them in areas prone to exceptions.. database actions, file read write, tricky validation, etc. Areas you cannot remove concern for an exception.
re #4: It depends on the project. Patterns like MVM or MVVM are becoming popular. At a minimum it would be best if you separate your UI logic from your app's data shuffling logic.. so you are not using a textbox as a string variable or datagrid as a container. UI elements are for displaying. Outside of that there are plenty of books and resources on just software design.
Was This Post Helpful? 2
  • +
  • -

#3 IronRazer  Icon User is offline

  • Custom Control Freak
  • member icon

Reputation: 1460
  • View blog
  • Posts: 3,703
  • Joined: 01-February 13

Re: Couple of questions concering VB.net

Posted 01 October 2016 - 07:09 AM

There are a few good reasons to use strong typing. If you have been programming long, you have probably heard of Option Strict which will enforce strong typing in your code. I highly recommend everyone to turn it on when learning to program so that you do not get use to using sloppy/bad coding habits.

If you read the Option Strict link i gave above, you will see the below information about why it is recommended.

Quote

When you set Option Strict to On, Visual Basic checks that data types are specified for all programming elements. Data types can be specified explicitly, or specified by using local type inference. Specifying data types for all your programming elements is recommended, for the following reasons:

  • It enables IntelliSense support for your variables and parameters. This enables you to see their properties and other members as you type code.

  • It enables the compiler to perform type checking. Type checking helps you find statements that can fail at run time because of type conversion errors. It also identifies calls to methods on objects that do not support those methods.

  • It speeds up the execution of code. One reason for this is that if you do not specify a data type for a programming element, the Visual Basic compiler assigns it the Object type. Compiled code might have to convert back and forth between Object and other data types, which reduces performance.


For debugging, VS has lots of ways to follow the execution of your code and/or view the values of your variables in real time. Below is another nice debugging tutorial that andrewsw has written.
Debugging Express

The Try...Catch...Finally Statement is used when you execute code that could throw exceptions that you don`t have any way to test for before executing the code. For example, if you tried opening a file that is already locked open by another program, you have no way to test for that so, an exception would be thrown causing your app to crash on the user.

If you used the code to open that file in the Try part and it threw the exception, you could catch that exception in the Catch part. You could then alert the user with a MessageBox and/or write the exception information to a log file. Your program would then continue running and would not crash on the user (if your code is set up right).

The Finally part is optional, it is for cleaning up after the code has executed, successfully or not. If you created any New objects in the code prior to the Finally part that require to be Closed and/or Disposed, the Finally part is where you would do it.

As far as how to set up your code, i am guessing you might understand Object-Oriented Programming (OOP). Just make sure that you understand Separation of concerns.
Was This Post Helpful? 1
  • +
  • -

#4 IxGRpPafwang  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 11
  • Joined: 11-January 13

Re: Couple of questions concering VB.net

Posted 02 October 2016 - 05:46 AM

Thanks for all the help, I'm gonna do some reading! About the OOP, I know how to work with objects but I dont think I have the proper organization in which I use them. I try to do as much as possible within the objects themselves but still most of the game logic and all the events are all handled in the code at form1, the code that gets executed when the application initializes. Are there any examples of simple applications where I can peek at how others organize their code?
Was This Post Helpful? 0
  • +
  • -

#5 IronRazer  Icon User is offline

  • Custom Control Freak
  • member icon

Reputation: 1460
  • View blog
  • Posts: 3,703
  • Joined: 01-February 13

Re: Couple of questions concering VB.net

Posted 02 October 2016 - 05:52 AM

You could go through modi123_1`s tutorial at the link below.
OOP with Video Game Basics Part 1
Was This Post Helpful? 1
  • +
  • -

Page 1 of 1