none
Hints and Tips to programmers when they get into sticky situations RRS feed

  • Question

  • Afternoon, I've used visual basic for nearly a year now and whenever I get into sticky situations I dont know what I can do and where to start. That is why I want to hear out from all of you online who have better experiences handling programs especially when errors occur. I really want to be good at this and try solve problems independently even when they're very complex before going to my teacher or friends when I cant solve it alone. 

    In addition, how do any of you analyse code effectively? Do you use the code in a new form to scrutinise how it operates. Feel free to share any of your techniques which could benefit me. 

    • Changed type Waliur Rahman Thursday, March 23, 2017 6:55 PM Want to mark comments as answers
    Sunday, March 5, 2017 1:03 PM

Answers

  • Wal,

    Its really just a trial and error process.

    30 years from now you will be better at it than you are today.

    When you learned to walk did you read the instructions and then instantly start walking perfectly or did you practice walking for a few years and then get better at it and then find more instructions how to walk better?

    You need to learn how Microsoft lays our their documents. How search engines work. How the Visual Studio intellisense works. etc. Like Option Strict On.

    You need to understand what the code statements do. You need to learn what causes certain errors and how to fix it. You learn this method is fast this is slow.

    You develop a toolbox of things you know. Each time you learn something you have a new tool.

    You need to learn the Visual debug tools like break points, single step code execution, press F1, search.

    And on and on...

    It takes time and practice and hard work is all. Just like anything else. Just take one step at a time. Dont try to learn programming 430 until you understand 102. Every day read the instructions again and practice some more. You will learn something new everyday.

    • Marked as answer by Waliur Rahman Thursday, March 23, 2017 6:56 PM
    Sunday, March 5, 2017 1:29 PM
  •  As has been said,  it will take time to advance your skill set.  I only have a few tips that are a MUST if you want to program.  The first can be debated but,  in my opinion there is no good reason not to use it.  That would be to turn Option Strict on so that it will not let you make Type Conversion errors in your code.

     These are errors that are usually made by beginners that don`t really understand there is a difference between data types.  For example,  using a String type like it is a numeric type data such as an Integer,  Double, or other.  Knowing the different data types and how/when to use them and how to convert one type to anther type is a MUST.

     You can turn it on in the VS options so that it will be used for all code that you work on in every project.  On the VS Menu click (Tools) and select (Options).  Then expand the (Projects and Solutions) node and the settings will be in the (VB Defaults).  I always keep the setting as shown below and have never found a situation that i had to set them any different.

     

     The second MUST is to learn at least some basics of using the Debugging tools that are included in VS as Tom has mentioned.  It will save you a lot of head scratching when things are not working the way you want or are giving you unexpected results.  There are several tutorials around the internet you can search for.  Below is one that i had a link handy.

    Debugging Express - Vb.Net

     


    If you say it can`t be done then i`ll try it

    • Edited by IronRazerz Sunday, March 5, 2017 5:04 PM
    • Marked as answer by Waliur Rahman Thursday, March 23, 2017 6:56 PM
    Sunday, March 5, 2017 4:59 PM
  • Afternoon, I've used visual basic for nearly a year now and whenever I get into sticky situations I dont know what I can do and where to start. That is why I want to hear out from all of you online who have better experiences handling programs especially when errors occur.

    In addition, how do any of you analyse code effectively? Do you use the code in a new form to scrutinise how it operates. Feel free to share any of your techniques which could benefit me. 

    Hi,

    one thing to learn is to "take a step back" when you run into problems where you dont know its cause. Going away from the computer and maybe take some sleep helps in getting new points of view for the problem(s). I never learned that and so I'm often sitting for hours at a problem with hurting eyes, and then the next day I quickly see that eg I just had a wrong index or a "i" for a "j" variable or so...

    Regards,

      Thorsten

    • Marked as answer by Waliur Rahman Thursday, March 23, 2017 6:56 PM
    Sunday, March 5, 2017 6:17 PM
  • Is looking at old programs and analysing them a good idea. Someone might have mentioned it here. Apologies if I just repeated what somebody else has said. 

    Hi

    In my opinion it is a good thing. When I first started using VB, I embarked on a large application as my first project. I managed to get it up and running with lots of help - mostly from this forum. I chose to ignore some of the advice given here about the manner of displaying the data of my application - after all, I had it working more or less as needed.

    *

    Once I got used to it, I decided to go back and look at some of those suggestions I had ignored as I was now more able to follow/understand the suggested changes - version 2 was born. I rewrote the entire project implementing the changes. As an example, my application was able to display my data in 14 seconds compared to 2 minutes - quite an improvement.

    *

    I now have version 3 and although not such dramatic improvements, there are many many smaller but just as important improvements.

    *

    So, to answer your question, I do think it is productive to 'rethink' ideas based on gained experience. 'Experience' is a long term thing.


    Regards Les, Livingston, Scotland


    • Edited by leshay Sunday, March 5, 2017 6:18 PM
    • Marked as answer by Waliur Rahman Thursday, March 23, 2017 6:56 PM
    Sunday, March 5, 2017 6:17 PM
  • Hi,

    one thing to learn is to "take a step back" when you run into problems where you dont know its cause. Going away from the computer and maybe take some sleep helps in getting new points of view for the problem(s). I never learned that and so I'm often sitting for hours at a problem with hurting eyes, and then the next day I quickly see that eg I just had a wrong index or a "i" for a "j" variable or so...

    Regards,

      Thorsten

    Couldn't agree with that more - in fact just yesterday (and the day before) I went marathon crazy over a problem with reading and updating a data file, going over and over the JSON code, rewriting and testing the code to determine if a line of data already exists (not a simple match at all), etc etc. Finally just walked away, and came back to the simple answer, I was appending to the existing file rather than rewriting it, took 30 seconds with fresh eyes and a rested brain.
    • Marked as answer by Waliur Rahman Thursday, March 23, 2017 6:57 PM
    Friday, March 10, 2017 4:15 PM

All replies

  • Waliur,

    Your question is simple. However, the answer can be very diverse. 

    It depends on where and for whom the programs are used. 

    However, the first thing is to avoid errors. Some make code and if it does what they think is the solution, than it is the solution. Fut that is not enough it should do what is needed in all situations which can come and very important it need to be maintainable. 

    Maintainability takes direct care that you avoid errors.

    A simple way to reach good maintainability is to take a program you made 6 months ago and never did take a look anymore at it.

    Would you do that program now in the same way?

    I've seen kids here are telling I'm not always giving the answer that they want to hear. But avoiding errors is better than repair them.

    As more direct answer on your question. Read as much as possible about maintainability of programs on Internet.  

    https://en.wikipedia.org/wiki/Maintainability


    Success
    Cor


    Sunday, March 5, 2017 1:21 PM
  • Its a good thing, people should hear what others have to say. I know critiques isn't something I like but it makes it beneficial in the future. I know a friend who told me off quite a lot for a lack of maintainability of my code and giving basic names like "textbox1" he said the examiner will know you are an amateur. I didn't like it but it was the truth. 

    On the whole he didn't do so just to lower my self esteem on being a programmer but he did it so I wouldn't make the mistake again in future cases. 

    In other words Cor, you do give the direct answers, perhaps some people find it hard to accept the truth thats the problem

    Sunday, March 5, 2017 1:28 PM
  • Wal,

    Its really just a trial and error process.

    30 years from now you will be better at it than you are today.

    When you learned to walk did you read the instructions and then instantly start walking perfectly or did you practice walking for a few years and then get better at it and then find more instructions how to walk better?

    You need to learn how Microsoft lays our their documents. How search engines work. How the Visual Studio intellisense works. etc. Like Option Strict On.

    You need to understand what the code statements do. You need to learn what causes certain errors and how to fix it. You learn this method is fast this is slow.

    You develop a toolbox of things you know. Each time you learn something you have a new tool.

    You need to learn the Visual debug tools like break points, single step code execution, press F1, search.

    And on and on...

    It takes time and practice and hard work is all. Just like anything else. Just take one step at a time. Dont try to learn programming 430 until you understand 102. Every day read the instructions again and practice some more. You will learn something new everyday.

    • Marked as answer by Waliur Rahman Thursday, March 23, 2017 6:56 PM
    Sunday, March 5, 2017 1:29 PM
  • I love that advice there thank you so much you touched me in my heart :)
    Sunday, March 5, 2017 2:00 PM

  • In addition, how do any of you analyse code effectively? Do you use the code in a new form to scrutinise how it operates. Feel free to share any of your techniques which could benefit me. 

    Learn classes. It's a lot to take in at first, but over time, create and work with them until it's second-nature.

    Also, learn to separate data from logic from presentation. That makes each of those parts of your code much more flexible/maintainable and usually much more reusable.


    "One who has no vices also has no virtues..."

    Sunday, March 5, 2017 3:55 PM
  • One of the best and most difficult methods to use is Test-driven development TDD. 

    In simple terms, let's look at this as a Windows form solution e.g. a project for the user interface, a project for data operations and perhaps a project for business logic.

    Now create within the solution a unit test project. In this case we want to write a test for accessing database tables. We might start of with something like this.

    Imports System.Text
    Imports Microsoft.VisualStudio.TestTools.UnitTesting
    <TestClass()> Public Class UnitTest1
    
        <TestMethod()> Public Sub TestMethod1()
            Dim ops As New Operations
    
        End Sub
    
    End Class

    Where Operations class does not exists, Visual Studio offers to create the class. When doing so have it create the class in the data operations project.

    Next write code to get data e.g.

    Imports System.Text
    Imports Microsoft.VisualStudio.TestTools.UnitTesting
    Imports DataRoutines
    <TestClass()> Public Class UnitTest1
    
        <TestMethod()> Public Sub TestMethod1()
            Dim ops As New Operations
    
        End Sub
    
    End Class

    Note GetCustomersByCountry does not exists, allow Visual Studio to create it. Now write code in it to return data. You would then use Assert to verify data is being returned e.g.

    Imports System.Text
    Imports Microsoft.VisualStudio.TestTools.UnitTesting
    Imports DataRoutines
    <TestClass()> Public Class UnitTest1
    
        <TestMethod()> Public Sub TestMethod1()
            Dim ops As New Operations
    
            Dim Customers = ops.GetCustomersByCountry("USA")
            Assert.IsTrue(Customers.Count > 0)
        End Sub
    
    End Class


    Now were does the data come from? A good practice is to create methods to populate the table(s) prior to the test then after the test destroy the data. This can be done with events in the test class for init and destroy.

    The above is super simple and tedious but when something goes wrong with the code within the project you can run the test to see exactly what is going on.

    My current work solution contains well over 200 test in a team environment so each morning I run the test and see if all is working as expected and if not can zero in on the failing test for the problem.

    Although there are many benefits there is of course the down side on getting good at writing test before writing code and with that it take longer to finish a project.

    Before doing the above one must write out stories with task. A story is a small part of what you need to do using Gherkin. From the Gherkin write small task to complete the story. 

    Microsoft Team Foundation Server allows you to create stories, task and place them into iterations.

    Another tip, practice working with SQL from the database e.g. first write your query in MS-Access or SQL-Server then place the SQL into your code.  Most databases such as SQL Server offer SQL Server Management Studio where you want to write a INSERT. Select the table, right click and select "Script Table as"

    We get

    USE [NorthWindAzure]
    GO
    
    INSERT INTO [dbo].[Customers]
               ([CompanyName]
               ,[ContactName]
               ,[Address]
               ,[City]
               ,[Region]
               ,[PostalCode]
               ,[Country]
               ,[Phone]
               ,[Fax]
               ,[ContactTypeIdentifier])
         VALUES
               (<CompanyName, nvarchar(40),>
               ,<ContactName, nvarchar(30),>
               ,<ADDRESS, nvarchar(60),>
               ,<City, nvarchar(15),>
               ,<Region, nvarchar(15),>
               ,<PostalCode, nvarchar(10),>
               ,<Country, nvarchar(15),>
               ,<Phone, nvarchar(24),>
               ,<Fax, nvarchar(24),>
               ,<ContactTypeIdentifier, int,>)
    GO
    
    
    

    I would use the above and alter it then test it in the editor e.g.

    USE [NorthWindAzure]
    GO
    
    DECLARE @CompanyName NCHAR(40)
    DECLARE @ContactName NCHAR(30)
    
    SET @CompanyName = 'My company'
    SET @ContactName = 'Karen Payne'
    
    INSERT INTO [dbo].[Customers] ([CompanyName],[ContactName])
         VALUES (@CompanyName,@ContactName
    )
    GO
    
    
    
     

    Once I'm satisfied take

    INSERT INTO [Customers] ([CompanyName],[ContactName]) VALUES (@CompanyName,@ContactName

    Place it in my code and use AddWithValue method of the command objects Parameter collection to set the values for @CompanyName and @ContactName.


    Please remember to mark the replies as answers if they help and unmark them if they provide no help, this will help others who are looking for solutions to the same or similar problem. Contact via my Twitter (Karen Payne) or Facebook (Karen Payne) via my MSDN profile but will not answer coding question on either.
    VB Forums - moderator
    profile for Karen Payne on Stack Exchange, a network of free, community-driven Q&A sites

    Sunday, March 5, 2017 3:55 PM
    Moderator
  • What do you mean by what you quoted seen this repetitively 
    Sunday, March 5, 2017 4:07 PM
  • What do you mean by what you quoted seen this repetitively 

    Who are you talking to?

    We don't all see this forum the same way, so please make it obvious who you're addressing.


    "One who has no vices also has no virtues..."

    Sunday, March 5, 2017 4:11 PM
  • Is looking at old programs and analysing them a good idea. Someone might have mentioned it here. Apologies if I just repeated what somebody else has said. 
    Sunday, March 5, 2017 4:15 PM
  •  As has been said,  it will take time to advance your skill set.  I only have a few tips that are a MUST if you want to program.  The first can be debated but,  in my opinion there is no good reason not to use it.  That would be to turn Option Strict on so that it will not let you make Type Conversion errors in your code.

     These are errors that are usually made by beginners that don`t really understand there is a difference between data types.  For example,  using a String type like it is a numeric type data such as an Integer,  Double, or other.  Knowing the different data types and how/when to use them and how to convert one type to anther type is a MUST.

     You can turn it on in the VS options so that it will be used for all code that you work on in every project.  On the VS Menu click (Tools) and select (Options).  Then expand the (Projects and Solutions) node and the settings will be in the (VB Defaults).  I always keep the setting as shown below and have never found a situation that i had to set them any different.

     

     The second MUST is to learn at least some basics of using the Debugging tools that are included in VS as Tom has mentioned.  It will save you a lot of head scratching when things are not working the way you want or are giving you unexpected results.  There are several tutorials around the internet you can search for.  Below is one that i had a link handy.

    Debugging Express - Vb.Net

     


    If you say it can`t be done then i`ll try it

    • Edited by IronRazerz Sunday, March 5, 2017 5:04 PM
    • Marked as answer by Waliur Rahman Thursday, March 23, 2017 6:56 PM
    Sunday, March 5, 2017 4:59 PM
  • IR,

    Good points. :)


    "One who has no vices also has no virtues..."

    Sunday, March 5, 2017 5:18 PM
  • Is looking at old programs and analysing them a good idea. Someone might have mentioned it here. Apologies if I just repeated what somebody else has said. 

    It really depends on many things. For instance, looking at a solution that was migrated from classic VB but you don't know that is one case. Another is the developers may have been told they can only do something a specific way. And the list goes on.

     


    Please remember to mark the replies as answers if they help and unmark them if they provide no help, this will help others who are looking for solutions to the same or similar problem. Contact via my Twitter (Karen Payne) or Facebook (Karen Payne) via my MSDN profile but will not answer coding question on either.
    VB Forums - moderator
    profile for Karen Payne on Stack Exchange, a network of free, community-driven Q&A sites

    Sunday, March 5, 2017 5:37 PM
    Moderator
  • Afternoon, I've used visual basic for nearly a year now and whenever I get into sticky situations I dont know what I can do and where to start. That is why I want to hear out from all of you online who have better experiences handling programs especially when errors occur.

    In addition, how do any of you analyse code effectively? Do you use the code in a new form to scrutinise how it operates. Feel free to share any of your techniques which could benefit me. 

    Hi,

    one thing to learn is to "take a step back" when you run into problems where you dont know its cause. Going away from the computer and maybe take some sleep helps in getting new points of view for the problem(s). I never learned that and so I'm often sitting for hours at a problem with hurting eyes, and then the next day I quickly see that eg I just had a wrong index or a "i" for a "j" variable or so...

    Regards,

      Thorsten

    • Marked as answer by Waliur Rahman Thursday, March 23, 2017 6:56 PM
    Sunday, March 5, 2017 6:17 PM
  • Is looking at old programs and analysing them a good idea. Someone might have mentioned it here. Apologies if I just repeated what somebody else has said. 

    Hi

    In my opinion it is a good thing. When I first started using VB, I embarked on a large application as my first project. I managed to get it up and running with lots of help - mostly from this forum. I chose to ignore some of the advice given here about the manner of displaying the data of my application - after all, I had it working more or less as needed.

    *

    Once I got used to it, I decided to go back and look at some of those suggestions I had ignored as I was now more able to follow/understand the suggested changes - version 2 was born. I rewrote the entire project implementing the changes. As an example, my application was able to display my data in 14 seconds compared to 2 minutes - quite an improvement.

    *

    I now have version 3 and although not such dramatic improvements, there are many many smaller but just as important improvements.

    *

    So, to answer your question, I do think it is productive to 'rethink' ideas based on gained experience. 'Experience' is a long term thing.


    Regards Les, Livingston, Scotland


    • Edited by leshay Sunday, March 5, 2017 6:18 PM
    • Marked as answer by Waliur Rahman Thursday, March 23, 2017 6:56 PM
    Sunday, March 5, 2017 6:17 PM
  • Debugging is generally overlooked by many developers. If you own a version of Visual Studio over than Community Edition for VS2015 look at IntelliTrace.

    For instance, if I want to look at data operations while the app is running we can see code which was executed. In the case of the image below I used a parameterized command and can see what was sent to the database.

    Conditional break points are great too. Set the condition as shown below and a action.

    Working with Exception Settings to assist in finding issues e.g. in form load data is not loading and no exception is thrown. You can instruct Visual Studio to break on to expose an issue such as the database failed to open and other issues.


    Please remember to mark the replies as answers if they help and unmark them if they provide no help, this will help others who are looking for solutions to the same or similar problem. Contact via my Twitter (Karen Payne) or Facebook (Karen Payne) via my MSDN profile but will not answer coding question on either.
    VB Forums - moderator
    profile for Karen Payne on Stack Exchange, a network of free, community-driven Q&A sites

    Sunday, March 5, 2017 6:26 PM
    Moderator
  • Yeh I get it the debugger technique whenever I cant find the precise error in my code. its a red record button on the side of the code and I step over and make sure each line of code is being read. When I find lines of code are skipped thats where I find the error straight away and will be able to deal with it from there.

    Im real glad I open this discussion, not only helped me enforce basic things but new methods from well experienced coders who know what they are doing. Thank you all and wish me luck I do well in my coursework. 

    I only have 3 weeks from tommorrow and I plan to finish the coding before end of next week because I need to do the last two stages: Testing with evaluation and evaluation on the whole. In addition I'll have to revisit the design stage because of major changes including to restructure my pseudocode since there are many changes now. 

    Computer science coursework is really long and crazy. 

    Sunday, March 5, 2017 7:05 PM
  • Frank what does the quote even mean?

    "One who has no vices also has no virtues..."

    Sunday, March 5, 2017 7:06 PM
  • Frank what does the quote even mean?

    "One who has no vices also has no virtues..."

    I'm not really sure that I understand what you're asking.

    Do you mean to ask the definition of 'vice' and 'virtue'?

    The etymologies of the words are old and if you take it literally then it's all about morality, but it's really meant as a pithy expression - that without some 'bad', there's likewise no 'good' either. ;-)

    Originally I had the quote showing that it's a quote of Abraham Lincoln but then I found another reference showing that Winston Churchill said it, so I don't know who originated it.


    "One who has no vices also has no virtues..."

    Sunday, March 5, 2017 8:29 PM
  • Frank what does the quote even mean?

    "One who has no vices also has no virtues..."

    I'm not really sure that I understand what you're asking.

    Do you mean to ask the definition of 'vice' and 'virtue'?

    The etymologies of the words are old and if you take it literally then it's all about morality, but it's really meant as a pithy expression - that without some 'bad', there's likewise no 'good' either. ;-)

    Originally I had the quote showing that it's a quote of Abraham Lincoln but then I found another reference showing that Winston Churchill said it, so I don't know who originated it.


    "One who has no vices also has no virtues..."

    Frank,

    I assume you don't mean with Winston Churchil one of the first ones from 1600. If it is about Winston Churchill (known from WW1 and WW2) then he is far to young to compare with quotes from Abraham Lincoln, (or you should mean an younger version than mostly is spoken about) 

    https://sites.google.com/site/swanezine/vice-virtue-quotes

    :-) 


    Success
    Cor


    Sunday, March 5, 2017 8:50 PM
  • Cor,

    Like I said -- I don't know who originated it. I found it in reading about Abe Lincoln but then later edited it because I found another reference pointing to someone entirely different.

    *****

    History is written by the victor, so who knows the real answer. ;-)


    "One who has no vices also has no virtues..."


    Sunday, March 5, 2017 8:58 PM

  • History is written by the victor, so who knows the real answer. ;-)




    Frank,

    We agree about many things, a pity we discuss often the minor things.

    :-)


    Success
    Cor

    Sunday, March 5, 2017 9:16 PM
  • I just want to know does anyone get an error when they post threads such as you have exceeded your limit for today?
    Friday, March 10, 2017 3:26 PM
    • Your number of posts today has exceeded the limit.

    thats what it exactly says even though I haven't posted in past two days???

    Friday, March 10, 2017 3:29 PM
  • Hi,

    one thing to learn is to "take a step back" when you run into problems where you dont know its cause. Going away from the computer and maybe take some sleep helps in getting new points of view for the problem(s). I never learned that and so I'm often sitting for hours at a problem with hurting eyes, and then the next day I quickly see that eg I just had a wrong index or a "i" for a "j" variable or so...

    Regards,

      Thorsten

    Couldn't agree with that more - in fact just yesterday (and the day before) I went marathon crazy over a problem with reading and updating a data file, going over and over the JSON code, rewriting and testing the code to determine if a line of data already exists (not a simple match at all), etc etc. Finally just walked away, and came back to the simple answer, I was appending to the existing file rather than rewriting it, took 30 seconds with fresh eyes and a rested brain.
    • Marked as answer by Waliur Rahman Thursday, March 23, 2017 6:57 PM
    Friday, March 10, 2017 4:15 PM
  • That is actually good news to hear :) Perhaps that is the case for me even if I feel pressure this term with mock exams, got this thing called Extended Project Qualification to finish of and my coursework to hand in at the end of this term hence before April begins. 

    I just hope I can finish the development stage before then so I can finish the testing with evaluation and evaluate on the whole then my coursework is complete. 

    Wednesday, March 15, 2017 8:35 PM
  • How do i mark people comments answers on here?

    WRA

    Thursday, March 23, 2017 4:53 PM
  • How do i mark people comments answers on here?

    WRA


    Go to your initial post and change the type from a discussion to a question, then you can mark one or all as the answerer.

    "A problem well stated is a problem half solved.” - Charles F. Kettering

    Thursday, March 23, 2017 5:04 PM
  • ahh I see. Thanks Frank :)

    WRA

    Thursday, March 23, 2017 6:54 PM
  • ahh I see. Thanks Frank :)

    WRA


    :)

    "A problem well stated is a problem half solved.” - Charles F. Kettering

    Thursday, March 23, 2017 7:04 PM