Using 'magic Numbers' In Code Is Not Good Practice And Makes Maintenance Difficult
Feb 26, 2012
"First of all please TURN OPTION STRICT ON.I make no apology for the capitals - I consider it that important.When you do you will see that you are still using HorizontalAlignment instead of ContentAlignment.VB is converting from one to the other for you.Although that works fine here there are many cases when it will not be what you intended and can cause some very hard to find bugs.Under Tools | Options | Projects and Solutions | VB Defaults Set: Option Strict ON, Option Explicit ON, Option Infer OFF".I did do this but it showed no errors, so I placed 'Option Strict On' at the start of the code, it then showed HorizontalAlignment with the squiggly line. I did edit my code to show this but I think you must have read it before I did. All other suggestions I will note and from now try to implement.Using "magic numbers" in the code is not good practice and makes maintenance difficult.Declare all such numbers at the top and use the names in the code.Is this what you mean? Const SpinTime As Integer = 500
Now when ever I refer in code to SpinTime it will always be 500..Change the NumberOfBoxes to 10 and run the app.In mine, to change boxes to 10 I needed to alter the code in 5 places, in yours 1 place! Talk about proof of the pudding etc.Now to my question, in the program when the start button is clicked and the five random numbers are displayed I have to stop and restart the program to generate five more. So I thought I would create another button 'Reset' this I did and all worked but it seemed a waste so I then just kept the start button in place and changed the text to Reset eg [code] This is fine the one thing I can't figure is how to replace the five numbers that were removed so that it is picking 5 from 500 every time. Now I'm at the end of all this writing I have it! just comment out the line 'Numbers.Remove(Number).
When editing my code in vb.net, my cursor is a flashing box and it makes it difficult to change one character. I would like to get the i-beam back instead of the flashing box when editing code. Can't seem to find a setting anywhere.
If I got Dim myRect As Rectangle = New Rectangle(0,0,100,100)Is it necessary or just fine to later do this: myRect = Nothing Or it isn't necessary?IF it is necessary, are there other cases it isn't for my variables?
For my project, I need to develop an app, which will have MS-SQL db with 4 tables a>FileHeader b>FileFooter c>Extension d>Detail about the file. This is all to do with the Magic Numbers, or File header and footer, found in all files. The first 4-byte and last 4-bytes.
Example: Suppose a word DOC file when read using this app should return and match the values D0 CF 11 E0 So, how can I read the files using vb.net, fileIO, FileStream/StreamClass/FileReader? I just need to open the file and read the first 4 and last 4 bytes of any given file. And then it should connect back to the SQL database and lookup for the matching values from the tables as I had mentioned. And return a match found or not found.
With all the smarts of actually loading images being done by the .net framework, seems like I shouldn't have to repeat it all in my code by checking for magic numbers, or using a hack like this:[code]Am I missing something obvious, like System.Drawing.IsImage(stream)?
how to make a program that makes all the possible characters and numbers (A-Z & a-z & 0-9) from 5-20 characters, and put them in a list or something?
Someone may think this is bruteforce hacking, but I don't see why I should do that? If I want to find a password on 13 characters, it would be: 62^13 = 200028539268669788905472 possibilities... in other words... it would take forever!
be on topic but seems the best place to post since most code examples from users are included into their posts within this forum.Where is latest link for best practice of inserting code for Forums?here was an article and possible a plug-in or executable code regarding the issue of users posting code, (mainly mangled or unmanageable code) within the question body, I can't find the article and was hoping it was still available. I have seen some posts where the inserted code maintained the indention,
I just tried fooling around with a "windows forms" project, and I would like to see the code that creates the form. How would I go about looking at that?I can only edit the code that would occur from GUI events.
I'm have coded some code so that when Ball1 hits objects extra1 or extra2 or extra3 or extra4 or extra5 then that extra what ever value was hit would disappear. But for some reason when ball1 hits one of them they all seem to go.[code]
Well, i was thinking of making a Tetravex solving program in order to practice my code writing skills (language will propably be Visual Basic) and I need help finding an algorithm for solving it. For those that don't know what tetravex is see this http:[url]... . The only algorithm I can come up with is the brute force way, place a tile randomly in one corner and try every possible tile next to it and continue the same process, if it reaches a dead end revert to a previous state and place a different tile.
I came to a company that already has a fully grown project... but coders that worked here before me didn't follow conventions and didn't use parametrized SQL queries... as a result there is over 1000 places in a very huge project that can possibly be vulnerable to SQL injection...
I need to find a solution that will automatically detect if there is an SQL injection in the code. So, for example there is a form which allows user to enter comments regarding a product, which will be sent to database on submit... how can we make sure that a user didn't enter a harmfull query instead of a normal text?
Is there any advanced code/regular expression/magic that can detect if this text contains a piece of SQL query instead of normal harmless text? I will accept any links, pieces of code in any language or even commercial software that will do that for me.
Is there a good tool available (preferably opensource or free) that will take C# or VB.NET source code, prettify it (format it consistintly) and convert it into HTML source code (so I can post it on the web)?
I have the following code in "Custom Code" in SSRS 2008. It works fine in VS2008 but it won't run on the server:
Public Shared Function Ns(ByVal num as Object, ByVal def as Object) as Object if IsNothing(num) OrElse Not IsNumeric(num) OrElse System.Double.IsNaN(num) then return def
I've been trying to get some help for this in another thread,but it looks like it's a task that is going to need a wider audience to try and find someone who knows something about writing/creating procedural textures at the pixel level in VB.Net. The VB6 example I'm trying to convert to VB.Net is attached to this post.
I'm in the process of converting an Access 2007 database to vb.net (in visual studio 2010). I want the user to be able to maintain the items in these comboboxes....adding, updating, and deleting any item they choose. In Access, I had a button which would display the combobox items and allow the user to maintain. Upon leaving this display, the combobox would be refreshed. Is this the best practice for vb.net? Would it be any different than what I'm doing now in Access?
I have a program where I want to stop the "www" service at night to keep users out of a website while some night jobs are running.
I know how to stop and start the service from VB.Net but I need to put the computer in maintenance mode before stopping the service to prevent work tickets from being automatically generated.
Is there a way to put a computer in maintenance mode from VB.Net? And also to take it out of maintenance mode?
I have been learning VB.NET since the summer and I was wondering if it makes sense for me to skip learning the things in VB.NET that I do not know and move on to C#.
I think I may be more comfy in C# because I have been using Actionscript 3 for over two years and have toyed with Java and C.
On the other hand, I've never done anything serious with C or Java.
It seems very difficult to find information on the use of DirectX with Visual Basic. MSDN gives a full list of methods, properties, enums etc but very little in the way of code examples. C++ and C# are better served.Why is Visual Basic (a terrific coding platform) so neglected in this area?
I'm writing an application; I have an Advanced Tree control and a ListBox control. When I populate the ListBox programmatically, using a function that returns a ListBoxItem, it adds the field fine, but displaying incorrectly; it doesn't just add say "My item," it adds "ListViewItem: {My item}"
When I add the item using just a string, it adds fine but I need to use the return function to set the tag data and such, but when I use that function, it adds the above which is really annoying.
I have a VB.NET (2005) application designed by my boss that uses a over 100 input and display controls (textboxes, comboxes, etc.), many with labels associated. My boss asked me to set the tab order, and then trap keypress so the user can use enter to navigate controls. I have 2 apps like this to work on.For the tab order, I could not use the View/TabOrder feature, since there are so many blue little tabindex popups showing, that I can not see any of the controls on the form to click. I tried clicking 'through' the popup with some success, but it still did not give me the tab order I wanted. The tab assistant that came with CodeSMART did not give me the result I wanted, either, so I could use it.
The controls on this usercontrol are spread among many groups, which in turn are spread among several splitcontainer panels. The 1st panel in the main splitcontainer has 3 main groupboxes which are selected via a toolstripbutton ( which sets the desired groupbox to visible and turning the others to invisible.) Example:
Private Sub ShowMainPanel() 'Note: Panels are groupboxes Me.MaterialPanel.Visible = False
How can I place a BG image on the tabs only (title area of tab)? I am also finding it dificult to change the tab text font, since it is changing the tab body font not the tab titles........??
I have an app that I'm trying to keep single threaded, mostly for reasons to do with simplicity of ongoing maintenance. I've structured it so that I can eventually multi-thread it if need be but I'd rather not at this point. It's semi-real time and does perpetual looping to display a number of different graphs and do various work with both the graph display and the work being hosted in their own respective objects. If you're familiar with the Windows dispatch manager it's that general paradigm run synchronously. And yee haw, it pretty much works at this point and runs fast enough to meet requirements by a happy margin. I have a cosmetic problem that I'd like to solve if I could. When I left-click and move any form around, my graphs stop updating until I release the left click button. I was thinking about a doevents somewhere in the click event catalog.
I save a lot of settings in an INI file as 1 (enabled) and 0 (disabled), all around the project's module i have this check If setting = 1 then ' do somethingEnd If ' OR If setting = 0 then ' do somethingEnd If
My question, is it good for performance to declare public constants to hold 1 & 0 and use them in such above checks instead of using real number?
In general, if i use value like 8, 9, "", "$", etc... in just two different places, should i declare constant for it?
I use the Magic Mouse, it's fantastic in OSX but more so because of MagicPrefs which extends the multi-touch gestures its capable of. I'd like to know if its possible for me to write drivers for the Magic Mouse in VB? to extend it's functionality. Or would this be better suited for C++? I don't even know where to start.
I want to make an application which turns on my computer. A Wake-on-Lan application should do the trick. I allready created something which sends data over UDP, i can see that it sends the data (with wireshark) but it does not send the correct data.
The goal in this assignment was to create a Magic 8 Ball that would display answers via array.Here's what I got down so far, now I apologize once again, I forgot most of the stuff so I don't even know if it is REMOTELY close to what it's suppose to be
Public Class Form1 Dim ArrayRes(7) As String Private Sub btnASK_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnASK.Click
I'm designing a 3 x 3 Magic Square, where the 3 x 3 tables are made of up three adjacent list boxes. Upon starting the program, the 3 x 3 square consists only of the character "?" to represent a variable that has yet to be defined. The user will input through text boxes and button presses the characters 1 to 9, replacing the highlighted undefined "?' with the desired number. I'm required to include a Function procedure that can, at any time scan the 3x3 table and return an integer value of -1 if an undefined character "?" is present, a value of 0 if the rows, columns and diagonals do not sum to the same value, or the sum common to all rows columns and diagonals.
To start with I defined a 2D array and undefined. Dim mSquare(2,2) As String Dim undefined As String = "?"
Then I used a Sub to set the 3x3 table as undefined which I did not show to save space. Next I used a Sub to update the highlighted characters, allowing only one list box to be selected at a time and which I hope also assigns the the values to the array as they are updated. I removed parts of the code so this post isn't too big.
If lstCol2.SelectedIndex = -1 And lstCol3.SelectedIndex = -1 Then lstCol1.Items(lstCol1.SelectedIndex) = input txtInput.Text = "" End If For i As Integer = 0 To 2 [Code] .....
What I'm stuck on is the Function design. I'm trying to design something to scan the array, and give back an integer depending on the results, which are to return an integer value of -1 if an undefined character "?" is present, a value of 0 if the rows, columns and diagonals do not sum to the same value, or the sum common to all rows columns and diagonals.
I'm currently trying Function checkSquare(ByRef mSquare As String) As String For i As Integer = 0 To 2 [Code] .....
At the moment I keep getting the error "Public Readonly Default Property Chars(index As Integer) As Char". I keep getting an error like this even using a single variable and defining each j individually, and I'm wondering if its possible to pass arrays to functions or do I need to do something else. Is what I'm trying to do even possible? I did a longer method in which I just check all the entries individually but I'm hoping there is a more efficient way. Its my plan B for now.