TheSwamp

Code Red => .NET => Topic started by: Keith™ on January 10, 2011, 10:35:47 AM

Title: My first programming exercise for college
Post by: Keith™ on January 10, 2011, 10:35:47 AM
I have to laugh about this ...

My first programming class (required by the degree track) is VB.Net using the 2008 engine

Anyway, the first project is building a form with a half dozen buttons to display text on a label, different text for each button ...

Personally, the coding style is crap .. they want long descriptors for the objects using Pascal style formatting UGH!!!!

The exercise is technically correct, but it could be so much more efficient, at least code wise. For example, instead of having 5 buttons, each with their own event handler, why not simply use one handler with a switch statement to fill in the text.

Anyway, it is done ... waiting for the instructor to "check" it ...
Title: Re: My first programming exercise for college
Post by: MP on January 10, 2011, 10:44:59 AM
??

Must have missed the thread where you talked about going back to school.

So ... you're back in school pursuing a degree? Computer Science? Are you studying at a physical school or doing the distance learning thing?
Title: Re: My first programming exercise for college
Post by: Keith™ on January 10, 2011, 12:51:02 PM
??

Must have missed the thread where you talked about going back to school.

So ... you're back in school pursuing a degree? Computer Science? Are you studying at a physical school or doing the distance learning thing?

In order ...

I am back in school getting a degree so that I can qualify to get a job doing what I have been doing for the last 20 years. I have applied for hundreds of jobs and I have had a total of 4 ... maybe 5 interviews. The qualifications for every job is a degree in Computer Science .. well la-tee-da ... I guess experience doesn't count ...

The school is a physical school but the classes (at least this semester) are all distance learning ... I still have to attend the campus for all tests and certain other items as deemed by the various instructors.
Title: Re: My first programming exercise for college
Post by: Chuck Gabriel on January 10, 2011, 01:02:03 PM
Good luck with school Keith.  It feels like I will never be done right now.
Title: Re: My first programming exercise for college
Post by: LE3 on January 10, 2011, 01:15:23 PM
Wow ... How many semesters?

I have 5+ months on my job hunt and zero luck... dang, but going back to school can't afford it... and to oldie... :)

All the best for you.!


In order ...

I am back in school getting a degree so that I can qualify to get a job doing what I have been doing for the last 20 years. I have applied for hundreds of jobs and I have had a total of 4 ... maybe 5 interviews. The qualifications for every job is a degree in Computer Science .. well la-tee-da ... I guess experience doesn't count ...

The school is a physical school but the classes (at least this semester) are all distance learning ... I still have to attend the campus for all tests and certain other items as deemed by the various instructors.
Title: Re: My first programming exercise for college
Post by: MP on January 10, 2011, 01:26:01 PM
In order ...

I am back in school getting a degree so that I can qualify to get a job doing what I have been doing for the last 20 years. I have applied for hundreds of jobs and I have had a total of 4 ... maybe 5 interviews. The qualifications for every job is a degree in Computer Science .. well la-tee-da ... I guess experience doesn't count ...

The school is a physical school but the classes (at least this semester) are all distance learning ... I still have to attend the campus for all tests and certain other items as deemed by the various instructors.

Well you certainly don't need any luck <you'll smoke that program> so I'll just say "best wishes for a great experience".

I've been enjoying being unemployed for the last 3 months. Started taking some online python courses (http://www.oreillyschool.com/?79723) a couple days ago, primarily because I want the paper saying I know this sh!t, and I would like a gig that involves python exploitation in addition to the usual engineering automation I've done over the last couple decades. My belief is that intention (formally pursuing python programming) will change my future -- has every time in the past.
Title: Re: My first programming exercise for college
Post by: Keith™ on January 10, 2011, 01:41:27 PM
Well, I took my first quiz just now and I am pissed ... I missed one

I know damn well the answers to each question, but the questions were written in such a way that I had to interpret them in a manner that made sense based on the multiple choice answers available.

The bad thing is that the system doesn't let me see which one I missed. How stupid is that?

Incidently, here is a bit of text from the book used in the class (emphasis mine):

Quote
Good programming practice requires remarks in every procedure and in the Declarations section of a file.

Now while I agree that well written programming is commented in order to make it clear what is happening and to let the subsequent programmer know what is going on, some things are so trivial that commenting isn't required or desirable, in fact, when the code is trivial, it is desirable to NOT comment it.

I'm off to email the instructor requesting the information on the one question.
Title: Re: My first programming exercise for college
Post by: MP on January 10, 2011, 01:56:51 PM
Quote
It is good programming practice to write code that is clearly self describing, supported with comments when and where applicable -- but superfluous documentation fluff should be discouraged.

fixed
Title: Re: My first programming exercise for college
Post by: Keith™ on January 10, 2011, 02:01:36 PM
:lmao: yeah, but I am pretty sure the ego of the author(s) wouldn't allow for my (our) arguing otherwise
Title: Re: My first programming exercise for college
Post by: MP on January 10, 2011, 02:09:50 PM
yep, pick your battles :kewl:
Title: Re: My first programming exercise for college
Post by: Jeff H on January 11, 2011, 09:23:11 PM
Hey Keith,

How do you like the distant learning class?
Is it the type you get a book and assignments or the type where you watch recorded or live classes?

I switched from EE to Computer Science and need to decide for next semester where to finish up.
One college only offers some of the classes during the day and the other option is a further drive but offers the distant learning route.

The one that is distant learning was set up by a mac-humping, java-loving(programming language not coffee), windows-hating, spritzer drinking type. If that makes sense.
Very mac biased.



I hated those True/False questions where the answers is "depends". I had my fair share last semester.
Title: Re: My first programming exercise for college
Post by: uncoolperson on January 11, 2011, 10:03:33 PM
man, you don't want to see some of my CS141 programming assignments.


well, maybe you do (i had fun with this one)

one was write a roman numeral calculator (command prompt) that would accept as a whole line "X + I" and return the answer in roman numerals, and had to follow all the proper roman numeral conventions. In ADA (bleh)
Title: Re: My first programming exercise for college
Post by: Keith™ on January 12, 2011, 12:11:35 AM
Jeff, I have taken these types of classes before, but this is a bit different and takes some getting used to. The problem isn't the classes or the way the material is presented, but rather the fact that the faculty obviously doesn't know how to use the system the college has installed.

For example ...
On Sunday evening I was emailed a syllabus for a database class. It came in my regular email .. not once, but three times, then when I logged into the system, I am required to "read the syllabus online" .. I mean seriously, I already got it in email three times ... then it says to log into the SAM server and start going through the exercises in conjunction with chapter 1 in the book. Ok fine ... WTF is the SAM system? Where is the link? So i send an email to the system admin asking where the link to the SAM system is ... they don't know what the SAM system is ... after an email to the instructor, I get another three emails on Monday telling me about the SAM system ... ok fine ... so I log into the system (not the SAM system) to get the information I need to enter into the SAM server when I log in ... and I am required to read it again ... OK fine ... I guess this is gonna be an everyday thing ... I log into the SAM system get setup and everything is fine ... I select the first assignment, the date it says it is available is NOW ... I work through the exercises .. really simple stuff ... deleting records, compacting databases, creating tables etc ... so I finish and go to select the quiz ... it also says available NOW ... so I select it to go through the quiz (I know I am gonna ace this thing) and there are two buttons on the popup ... "Save" and "Submit" ... when you select "Save" it is supposed to save the current state of the quiz so you can come back to it later and finish it. Well, I didn't get to start on it because I had an appointment to go to so I selected "Save" ... got back from my appointment and selected the quiz and it said "You have already completed this quiz" .. I was like WTF .. checked the score .. friggin zero ... email the instructor ... response is "That quiz isn't available until next week" seriously? why could I do it earlier and why does it say I got a zero?

Now the VB instructor tells the class they have to install Visual Basic 2008 and submit all homework in that version. I asked if they wanted it in the Express version or Standard. I was told to get it from another instructor ... I emailed that instructor for which version I needed and I was told to supply him with a DVD-9 ... WTF? seriously a DVD-9 what the hell is that? Do you mean a dual layer DVD? That is just over 8gb? Then he tells me that is what is required for the VB 2010 edition. So I told him I already have the 2010 edition, but the syllabus says to use the VB 2008 edition, to which I am told "We have a special licensing deal with Microsoft so you have to get it from us" Ok, what version? again VB 2008 ... I ask again Express or Standard? No response ... the book says to use the Express version, so that is what I am going with. It will still open with the standard version.

I am seriously questioning the ineptitude and ignorance of the instructors for this class.

Overall, once I sent a dozen emails to the tech department telling them that they had lots of dead links on the site and pointing out where they are, plus posting in the forum for other students about how to get the VB Express 2008 version from MS without having to go buy an assload of dual layer DVDs (which it doesn't require anyway) things seem to have calmed down a bit.

So to answer your other question .. the training is an interactive watch/try/apply tool written in flash (requires Adobe Flash) and the quizes are similarly written in Adobe Flash. The chapter assessments are handled through simple Javascript and HTML forms sent back to the server for posting (could easily be hacked), but it is effective for the quality of most students. Everything is backed up by a textbook and downloadable examples. Overall not too bad, except for the crap you have to install to be able to do the work.

UCP, I'll bet it would be fun to see some of them
Title: Re: My first programming exercise for college
Post by: Jeff H on January 12, 2011, 12:47:21 AM
Thanks for info Keith,

Here are the 2 colleges and links to CS degree any suggestions?
UWF Fort Walton (http://uwf.edu/catalog/deg.cfm#1)
FSU Panama City (http://www.pc.fsu.edu/Academics/Undergraduate)
Title: Re: My first programming exercise for college
Post by: Keith™ on January 12, 2011, 02:33:31 AM
LOL .. yeah, if I had to choose I'd go with FSU. I know the education you get from UWF will effectively be the same as from FSU, but the prestige that comes from FSU will probably get you farther after the fact. I also don't know anything about the UWF distance learning program, but the FSU one I investigated quite a bit and considered going there ... if only my funding would pay for it ;-)
Title: Re: My first programming exercise for college
Post by: jgr on January 12, 2011, 02:43:20 AM
For some, it seems that programming with visual basic is for donkeys. But it is not. The question is not the programming language, is a question of talent (coder's brain).
Title: Re: My first programming exercise for college
Post by: Keith™ on January 12, 2011, 10:06:49 AM
For some, it seems that programming with visual basic is for donkeys. But it is not. The question is not the programming language, is a question of talent (coder's brain).


I agree, it doesn't matter if you use VB or C# to do your coding it all gets reduced to CLI and behaves similarly.
Title: Re: My first programming exercise for college
Post by: Jeff H on January 12, 2011, 09:06:15 PM
With that email ending .edu you know you get all this(full-version) software for free

https://www.dreamspark.com/default.aspx
Title: Re: My first programming exercise for college
Post by: uncoolperson on January 12, 2011, 09:28:07 PM
the standards I have to go by

Quote
Coding Standards
1. Use meaningful names that give the reader a clue as to the purpose of the thing being
named.
2. Use comments at the start of the program to identify the purpose of the program, the
author and the date written.
3. Use comments at the start of each procedure to describe the purpose of the procedure and
the purpose of each parameter to the procedure.
4. Use comments at the start of each section of the program to explain what that part of the
program does.
5. Use consistent indentation:(insert page and a half of examples)
Title: Re: My first programming exercise for college
Post by: Keith™ on January 12, 2011, 10:16:43 PM
With that email ending .edu you know you get all this(full-version) software for free

https://www.dreamspark.com/default.aspx

Cool, although some of them are free even if you don't have the .edu email address.
Title: Re: My first programming exercise for college
Post by: uncoolperson on January 15, 2011, 12:33:51 AM
finally (geez) hit recursion in my programming class.

and I happened to be wearing my recursion t-shirt....
(like this one)
http://recursivetees.spreadshirt.com/recursion-definition-A4324133
Title: Re: My first programming exercise for college
Post by: It's Alive! on January 15, 2011, 01:57:41 AM
It seems most code houses try to go with self documenting code, and are lite on the comments.
I'm very surprised that the use of VB, since it is the most platform dependant language.  In a world where you're more likely to make a buck writing apps for smart phones, colleges are teaching  VB?  :-o

Title: Re: My first programming exercise for college
Post by: Keith™ on January 15, 2011, 10:45:14 AM
It seems most code houses try to go with self documenting code, and are lite on the comments.
I'm very surprised that the use of VB, since it is the most platform dependant language.  In a world where you're more likely to make a buck writing apps for smart phones, colleges are teaching  VB?  :-o



Not only are they teaching VB, but they are using 2008 Express, even though 2010 Express is available, and VB is a required class ... in fact they won't let you take any higher level classes (C++ is considered a higher level class) until you do the VB class.
Title: Re: My first programming exercise for college
Post by: Jeff H on January 15, 2011, 11:09:11 AM
That is the class I am in now. You have to take the intro C++ class first because it is a preq. to all classes
Title: Re: My first programming exercise for college
Post by: Keith™ on January 15, 2011, 01:21:56 PM
So, my instructor changed the first assignment from the hands on exercise in the book to the basic "Hello World" application.

The instructions are now:
Create the Hello World! application and have at least three buttons two of which display "Hello World!" in a different language. One language must be English.

So, I chose 5 languages -
English, Spanish, French, German, and Klingon

I'm waiting to see how she reacts ;-)
Title: Re: My first programming exercise for college
Post by: A_LOTA_NOTA on January 17, 2011, 08:18:26 AM
That is the class I am in now. You have to take the intro C++ class first because it is a preq. to all classes

I hade to take "Beginning UNIX: Linux" two semesters ago. "C Language" last semester. I just started "C++ Programming Language" last week.
Title: Re: My first programming exercise for college
Post by: jgr on January 17, 2011, 02:25:05 PM
..."Beginning UNIX: Linux" two semesters ago....

With CSharp:
An open source, cross-platform, implementation of C# (It also has a compiler for vb.net)  and the CLR that is binary compatible with Microsoft.NET
http://www.mono-project.com

Commercial add-in for visual Studio that enables developers to write .NET applications for non-Windows platforms. It allows developers to build, debug and deploy .NET applications on Linux (I tested it and seems to work well on red hat and suse).
http://mono-tools.com/
Title: Re: My first programming exercise for college
Post by: Keith™ on January 17, 2011, 04:49:31 PM
So, I had a project to turn in ... Excel ... develop a spreadsheet using supplied information and follow the instructions as written.

Item 8.
Quote
8.   Use the buttons in the Number group on the Home tab on the Ribbon to apply the Accounting Number format to the range B4:G4 and the range B8:G8. Use the buttons in the Number group on the Home tab on the Ribbon to apply the Comma Style to the range B5:G7.

Ok, this is easy ... so I submitted my worksheet and I got -9 on the part requesting to apply the Comma Style. Well, there isn't a true comma style AFAIK, but there is a button on the Number group that has a comma on it and the tooltip says:

Quote
Comma Style
Display the value of the cell with a thousands separator.

This will change the format of the cell to Accounting without a currency symbol.

I have emailed the instructor telling him how stupid this is .. if they wanted Number format with a thousands separator, that is what the damn instructions should have said.
Title: Re: My first programming exercise for college
Post by: uncoolperson on January 19, 2011, 09:02:55 PM
I got a fun one coming up... just gotta wait for it to be assigned (the for me to do it), and I'll post it here as a challenge.

(involves recursion!!!)
Title: Re: My first programming exercise for college
Post by: Keith™ on January 19, 2011, 09:13:49 PM
I got a fun one coming up... just gotta wait for it to be assigned (the for me to do it), and I'll post it here as a challenge.

(involves recursion!!!)

I ain't doin' ya homewerk buddy
Title: Re: My first programming exercise for college
Post by: uncoolperson on January 19, 2011, 09:16:42 PM
I got a fun one coming up... just gotta wait for it to be assigned (the for me to do it), and I'll post it here as a challenge.

(involves recursion!!!)

I ain't doin' ya homewerk buddy

that's why I was going to wait until I was done.
Title: Re: My first programming exercise for college
Post by: Keith™ on January 19, 2011, 09:20:47 PM
:-P  :-P
Title: Re: My first programming exercise for college
Post by: uncoolperson on January 19, 2011, 09:24:01 PM
just trust me dude... it'll be a lot more entertaining then hello world in a couple of languages
Title: Re: My first programming exercise for college
Post by: Keith™ on January 19, 2011, 11:35:10 PM
just trust me dude... it'll be a lot more entertaining then hello world in a couple of languages

Hey now ... I was at least a bit creative and used Klingon (I hope the instructor has a sense of humor)
Title: Re: My first programming exercise for college
Post by: Jeff H on January 19, 2011, 11:44:37 PM
just trust me dude... it'll be a lot more entertaining then hello world in a couple of languages

Hey now ... I was at least a bit creative and used Klingon (I hope the instructor has a sense of humor)

I was going to post that probably some nerd wrote a computer language in klingon, but decided to google it and was right.

Quote
Klingon computer programming language
Var'aq is a programming language for those that speak Klingon. Here's "Hello world" in var'aq:

~ nuqneH { ~ 'u' ~ nuqneH disp disp } name
nuqneH

Roughly translated, it means "What do you want, universe?"

http://kottke.org/03/01/klingon-computer-programming-language

http://www.trektoday.com/news/070800_05.shtml

Title: Re: My first programming exercise for college
Post by: Jeff H on January 19, 2011, 11:59:57 PM
The links are broken moved

http://esolangs.org/wiki/Var'aq
http://www.reocities.com/connorbd/varaq/varaqspec.html
http://www.reocities.com/connorbd/varaq/index.html

Title: Re: My first programming exercise for college
Post by: uncoolperson on January 20, 2011, 12:05:15 AM
just trust me dude... it'll be a lot more entertaining then hello world in a couple of languages

Hey now ... I was at least a bit creative and used Klingon (I hope the instructor has a sense of humor)

I don't care that no one ever see's it, inserting fun is well... just fun.

Code: [Select]
FUNCTION Tales_From_The_Cryption_Keeper
Code: [Select]
Put("Sorry Jack Ryan, you don't have high enough clearance for that");
Title: Re: My first programming exercise for college
Post by: Keith™ on January 20, 2011, 12:39:40 AM
just trust me dude... it'll be a lot more entertaining then hello world in a couple of languages

Hey now ... I was at least a bit creative and used Klingon (I hope the instructor has a sense of humor)

I was going to post that probably some nerd wrote a computer language in klingon, but decided to google it and was right.

Quote
Klingon computer programming language
Var'aq is a programming language for those that speak Klingon. Here's "Hello world" in var'aq:

~ nuqneH { ~ 'u' ~ nuqneH disp disp } name
nuqneH

Roughly translated, it means "What do you want, universe?"

http://kottke.org/03/01/klingon-computer-programming-language

http://www.trektoday.com/news/070800_05.shtml



Well, your source is close ... the actual text would be
Quote
nuqneH qo'

nuqneH: what do you want? Greetings (excl.)

qo':  world, realm (n)


But then I also took the liberty of changing the other text on the form as well ... for example, instead of "Exit" I put

Quote
Qaw' ngoqDe'

Qaw': destroy (v)

ngoqDe': code - encoded message (n)


So, basically telling the user to "destroy encoded message"
It isn't exactly 100%, but any person capable of understanding Klingon would understand the phrase.

Another little thing I added was to change the window title to the language of choice. The instructor said to put our name in the title along with the Hello World phrase. In English it would be "Hello World - By Keith™" but in Klingon it is as so:

Quote
nuqneH qo' - ghajwI' Keith™

nuqneH: what do you want? Greetings (excl.)

qo': world, realm (n)

ghajwI':  one/thing which is/does/   have, possess (v)  


Roughly translated to: Hello World - Owner Keith™

It was quite fun ;-) I think maybe I'll do more Klingon stuff in the future ... as long as this doesn't backfire on me ;-)
Title: Re: My first programming exercise for college
Post by: uncoolperson on February 08, 2011, 07:36:20 PM
just trust me dude... it'll be a lot more entertaining then hello world in a couple of languages


just turned in my recursive soduko solver...
Title: Re: My first programming exercise for college
Post by: Keith™ on February 09, 2011, 12:13:38 AM
So, my instructor gave me 100/100 on the first two assignments - hello world and some other bizarre application that displays different text depending on which radiobutton and checkbox you pick. For my last four projects, I have taken considerable liberty with my programming. I've introduced things that aren't in the scope of the class ... things like GDI printing, structs, enums and <gasp> even data driven error control.

I find it to be laziness when programmers users rely on Try/Catch blocks to prevent invalid data from being entered.

The textbook says to use a Try/Catch block to prevent a user from entering text in a numeric textbox. I prefer to simply find out if the text is numeric before I try to act on it ...
Why would I use
Code: [Select]
Try
  ThisVar = Cint(TextBox1.Text)
Catch ex As Exception
  MsgBox(e.Message)
End Try

When this will give a better result and allow you to filter stuff as you go.

Code: [Select]
If IsNumeric(TextBox1.Text) Then
  ThisVar = Cint(TextBox1.Text)
Else
  MsgBox("TextBox1 can contain only numbers")
End If

Maybe it is just me, but I hate like hell seeing those errors line up in the immediate window ..
Heck, I even use Option Strict and treat all warnings as errors ... in the end, the code is much more stable.

Oh well, such is life.
Title: Re: My first programming exercise for college
Post by: pkohut on February 09, 2011, 01:08:13 AM
Well Keith, have to agree with the book on this one. Since CInt throws an error, it makes sense to use the try catch block. It's by design. Also, the approach you suggest doubles the work load if the value is numeric, by first checking, then converting. If the input data needs validation between a range, then try/catch/finally is the proper solution.
Title: Re: My first programming exercise for college
Post by: dan.glassman on February 09, 2011, 09:26:20 AM
Your code contains a bug.  Enter "2,147,483,648" into the TextBox, and you should get an OverflowException.

It may also be a bug because the value of the TextBox isn't under your sole control, and may change between the If and the CInt.  I'm not sure if that's true in this case, but this (for example) would definitely be a bug:

if (File.Exists(f))
   File.Open(f)



-drg
Title: Re: My first programming exercise for college
Post by: Keith™ on February 09, 2011, 09:41:19 AM
Well Keith, have to agree with the book on this one. Since CInt throws an error, it makes sense to use the try catch block. It's by design. Also, the approach you suggest doubles the work load if the value is numeric, by first checking, then converting. If the input data needs validation between a range, then try/catch/finally is the proper solution.


We will have to agree to disagree here ... the workload is doubled on the programmer perhaps in writing code, but the processor workload is negligibly impacted by the extra call to IsNumeric.

Using that logic, it would be ok to use a loop and then a "On Error Resume Next" simply because .. well, it must not be what we wanted so lets just ignore it.

Your code contains a bug.  Enter "2,147,483,648" into the TextBox, and you should get an OverflowException.

It may also be a bug because the value of the TextBox isn't under your sole control, and may change between the If and the CInt.  I'm not sure if that's true in this case, but this (for example) would definitely be a bug:

if (File.Exists(f))
   File.Open(f)



-drg

It doesn't when the box is limited to 5 characters ... its all about design. Understand what can be entered and then you can understand the minimum code needed to handle it.

Incidently the text actually insists that all numbers are parsed using the Parse method of the expected type ... whether that is Decimal, Integer, Double or Long ..
Title: Re: My first programming exercise for college
Post by: It's Alive! on February 09, 2011, 10:04:58 AM
I would use tryparse, see http://msdn.microsoft.com/en-us/library/f02979c7.aspx
Title: Re: My first programming exercise for college
Post by: pkohut on February 09, 2011, 10:17:27 AM
Well Keith, have to agree with the book on this one. Since CInt throws an error, it makes sense to use the try catch block. It's by design. Also, the approach you suggest doubles the work load if the value is numeric, by first checking, then converting. If the input data needs validation between a range, then try/catch/finally is the proper solution.


We will have to agree to disagree here ... the workload is doubled on the programmer perhaps in writing code, but the processor workload is negligibly impacted by the extra call to IsNumeric.
More along the lines of don't have redundant code. In the provided example, try/catch is the proper way to convert the input to an int. Why fight the language and API?


Title: Re: My first programming exercise for college
Post by: kaefer on February 09, 2011, 01:53:34 PM
if (File.Exists(f))
   File.Open(f)

Would you recommend this instead?
Code: [Select]
    static StreamReader TryOpen (string name)
    {
      if (File.Exists (name))
        return new StreamReader (name, Encoding.ASCII);
      return null;
    }

There's a lot more to worry about, considering that only reads/writes of primitive types T where sizeof<T> <= sizeof<nativeint> are guaranteed to be atomic.
Title: Re: My first programming exercise for college
Post by: Keith™ on February 09, 2011, 02:04:31 PM
More along the lines of don't have redundant code. In the provided example, try/catch is the proper way to convert the input to an int. Why fight the language and API?


I see your point about not having redundant code, I just don't accept that IsNumeric(TextBox1.Text) is any less a valid way to catch user input error than a Try/Catch block.

I just did a simple test to see the difference in execution between a Try/Catch block and an If/Then block using IsNumeric and the results pretty well dispel any notion that one is introducing redundancy, at least in the execution of the code ...

My timing test reveals that in debug mode the execution time is always longer in a Try/Catch block than an If/Then, however, in a release build, the number of ticks for the Try/Catch block is only sometimes longer, but when it is, it is significantly longer. The If/Then is always equal to or less than the time ... at least in this test. YMMV
Title: Re: My first programming exercise for college
Post by: dan.glassman on February 09, 2011, 02:14:12 PM
Quote
Would you recommend this instead?

Sorry, I wasn't trying to exemplify "the Right Thing" in terms of how to open a file in a particular framework.  I was trying to point out that {pseudocode}

if file.exists(f)
   file.open(f)

is a bug anywhere because the file may disappear between the .exists() and .open() calls -- it's not in the programmer's control.  And that I wasn't sure whether a textbox's value was [similarly] vulnerable to changes outside the programmer's control between the IsNumeric() and CInt() calls.

In the file case,

try
   file.open(f)
catch IOError

Is the only proper thing to do -- no pre-checking for existence, permissions, lock state, etc. -- they're all liable to change at _just_ the wrong time.

-drg
Title: Re: My first programming exercise for college
Post by: Keith™ on February 09, 2011, 02:19:22 PM
I didn't mean to suggest that using a Try/Catch block was wrong, but rather than using it as an end-all bug catcher, the programmer should spend a little time thinking through the requirements of the code, the effects of user input and possible scenarios.

Certainly no programmer is capable of recognizing ALL potential pitfalls and as the previous post points out, there are some things that are simply not within control of the code, but my assertion is that when known possible conditions exist, they should be provided for by code.
Title: Re: My first programming exercise for college
Post by: Jeff H on February 09, 2011, 07:24:30 PM
I typically have used a textbox's Validating Event Handler with tryparse


Title: Re: My first programming exercise for college
Post by: It's Alive! on February 09, 2011, 08:33:40 PM
IMHO,  using try catch to validate a textbox is a bit heavy handed, but not unreasonable.  I certainly would use try/catch/finally  when doing file io operations,  as one may need to perform cleanup when the exceptional happens.  but this is comparing apples and oranges. 
The correct approach if you're doing more than one textbox for numeric's would be to subclass textbox as shown here http://msdn.microsoft.com/en-us/library/ms229644.aspx   
Title: Re: My first programming exercise for college
Post by: Jeff H on February 09, 2011, 10:03:08 PM
Or override OnVaildating event

From here http://www.theswamp.org/index.php?topic=36832.msg418314#msg418314



Code: [Select]
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.ComponentModel;
using System.Drawing;
namespace WindowsFormsControlLibrary1
{

    public class IntegerRedTextBox : TextBox
    {
        public IntegerRedTextBox()
        {
            OriginalColor = this.BackColor;
        }

        public Color ChangeColor { get; set; }
        private Color OriginalColor { get; set; }

        private bool ChangeBackColor()
        {
           char[] chars = this.Text.ToCharArray();

            foreach (char c in chars)
            {
                if (!Char.IsNumber(c))
                {
                    this.BackColor = ChangeColor;
                    return true;
                }
           
            }
            this.BackColor = OriginalColor;
            return false;
        }

        protected override void OnValidating(CancelEventArgs e)
        {
            base.OnValidating(e);

            e.Cancel = ChangeBackColor();
        }

    }
}

Title: Re: My first programming exercise for college
Post by: StefanDidak on February 10, 2011, 07:44:20 AM
I am seriously questioning the ineptitude and ignorance of the instructors for this class.

I've just read this thread and I must say, the hair at the back of my neck is standing up and my toes are curled into position in a way that I wonder how long it'll take to uncurl! I'm not surprised, though, I hear things like this quite often. Education isn't what it used to be!

I must say you're quite courageous to enter into a CS "education" (edutainment?) when you're already far ahead of the pack that the curriculum is aimed at. I've seen it over the past 20 years with some other folks and it usually ends up being somewhat frustrating to jump through hoops that you could've designed yourself. The whole "we require a degree" sounds like a brainless robo-parrot policy and indicates that too many companies are no longer interested in real experience and skill but rather want worthless pieces of paper that in the real world mean nothing. Being a rebel at heart I spent much of the 90's hiring and contracting folks that only had proven experience and actively ignored anyone with "papers". If more companies did that and knew how to properly assess and judge someone's abilities they'd be so much better off.

The one thing that keeps coming to mind here is a question, though. Have you ever considered going into business for yourself? It may be easier to contract for those many companies than it is to get a job there and while there are certain risks that always come up when being self employed there's also a lot of benefits. Just a thought...

Cheers,
Stefan.
Title: Re: My first programming exercise for college
Post by: Keith™ on February 10, 2011, 10:15:47 AM

The one thing that keeps coming to mind here is a question, though. Have you ever considered going into business for yourself? It may be easier to contract for those many companies than it is to get a job there and while there are certain risks that always come up when being self employed there's also a lot of benefits. Just a thought...


Actually I have, but if I do in fact become self-employed, I will lose whatever unemployment I have, regardless of the amount of income I generate. In fact, I had to fight with them to get it re-instated after I reported having made $100 doing odd jobs. I was told that I couldn't get unemployment if I earned wages from a self-employment source.

I have a conference call on Wednesday with a marketing research firm out of Olathe, Kansas. I have done freelance work for them in the past and we have been in negotiations for a while over getting an open end contract. If that works out, I'll be travelling to Kansas in March and making recommendations for setting up a data collection center. Fun stuff -NOT - but it WILL be a paycheck.

Plus, there is the chance that I'll have to go to Honolulu in the fall - now that would be terrible, wouldn't it.