|
Next time, PUT ALL THE DETAILS IN THE POST, like WHY you're doing something, or WHY you think you need to do this!! I answered your question to the specifications you put it in the original post.
If you want to find out if a number is an integer or not, simply do a little conversion:
Shared Function IsInteger(ByVal num As Decimal) As Boolean
' This will only work when num is inside the limits
' of a Long number type. That's between:
' -9,223,372,036,854,775,808 and 9,223,372,036,854,775,807
Return CDec(CLng(num)) = num
End Function
Dave Kreskowiak
Microsoft MVP - Visual Basic
|
|
|
|
|
Thankyou and i am sorry, but you don't have to be so agressive.
Posted by The ANZAC
|
|
|
|
|
Sorry man! I had the worst day at work and ended up taking it out on you! I'll try to leave work at work next time.
Dave Kreskowiak
Microsoft MVP - Visual Basic
|
|
|
|
|
Wow Harsh!
Post by THE ANZAC 'I need a function' (to test if a value is an integer)
reply by Dave Kreskowiak 'Why not just force it to an Integer?'
reply by THE ANZAC 'forcing it would defeat the purpose'
reply by Dave Kreskowiak 'Next time, PUT ALL THE DETAILS IN THE POST, like WHY you're doing something, or WHY you think you need to do this!! I answered your question to the specifications you put it in the original post.'
er no, you asked a new question and showed how to do that.
Sure THE ANZAC could have explained why he needed a function, but he did state that he needed one, you just assumed he was mistaken and that he didn't need a function and that he could just force it.
If you had answered his question to what specifications there were, you would have provided a function.
Not that this detracts from your point about including as much info as possible in the original post, when I have a question I shall certainly do so.
I don't want to get bawled out - forewarned is forearmed as they say.
I imagine that when you spend as much time, providing as much help, as you do, it must be frustrating to realise that you have given an answer, that with a little more information you would not have wasted your time giving.
I have written this post because I could see myself having asked a question and thinking (mistakenly) that I had provided enough information, only to find myself quite strongly rebuked. As I have said, I appreciate that it must be frustrating for you, but those of us with less experience may well not be aware that we have omitted something you perceive as obvious and vital.
|
|
|
|
|
I've apologized to him. It was not a good day for me...
Alex@UEA wrote: but he did state that he needed one, you just assumed he was mistaken
No, I didn't.
BTW: In your version, the function will only work for values with the limits of an Integer. Which is probably fine for most cases, but if there is no specification, you're making an assumption that it will work for ALL of his cases. That leads to hard to detect bugs and why I put the note in the code that I did.
Alex@UEA wrote: As I have said, I appreciate that it must be frustrating for you
The questions on CP aren't frustrating. Well, most of the time! We all know there are some people who come here asking us to write code for them and ask "What's wrong with my code?", with no explanation of what the code is supposed to do, no error messages and no code snippets. The quality of the answer is a direct reflection of the quality of the question.
My problem was that I got so frustrated with my job at work that I let it spill out here and at home. And for that, I apologize.
Dave Kreskowiak
Microsoft MVP - Visual Basic
|
|
|
|
|
Dave Kreskowiak wrote: BTW: In your version, the function will only work for values with the limits of an Integer. Which is probably fine for most cases, but if there is no specification, you're making an assumption that it will work for ALL of his cases. That leads to hard to detect bugs and why I put the note in the code that I did.
thank you for the pointer (of the non coding variety)
|
|
|
|
|
Use the Double.TryParse method with NumberStyles.Integer.
---
b { font-weight: normal; }
|
|
|
|
|
Private Function IsInt(ByVal value As Object) As Boolean
If IsNumeric(value) Then
If CInt(value) = CDbl(value) Then Return True
End If
Return False
End Function
|
|
|
|
|
Passing the parameter as an Object isn't recommended. This forcing boxing and unboxing operations that are quite expensive. If this function is used very frequently, this can cause serious performance issues.
It may be more code, but writing overloaded versions of this function for Decimal, Double, ..., would result in MUCH faster code.
Dave Kreskowiak
Microsoft MVP - Visual Basic
|
|
|
|
|
We live and learn, I seem to have done more of the former than the latter.
I chose to pass as an Object because the IsNumeric function that THE ANZAC originally mentioned does so and because it enabled one function to be broadly applied in a similar way - I lack the underpinning knowledge that would have told me that 'Passing the parameter as an Object isn't recommended'.
However, now I do know this and, should the need ever arise in my code, I will follow the overloaded versions route.
Thank you
PS I suppose that including IsNumeric in my function or just using it in general is undesirable for the same reasons?
-- modified at 11:54 Sunday 17th September, 2006 - I spotted a typo
|
|
|
|
|
Passing as Object causes a value type to be "boxed" inside a reference type and stored in the garbage collected heap. That reference type is an instance of an Object class. So, instead of just pushing the value onto the call stack, you're allocating and constructing a new object, copying the value into it, then the address of the Object is pushed onto the call stack. This takes about 20 times longer than just pushing the actual value type onto the stack or making a simple assignment, like a=10 .
Once the Object is passed, there is an unboxing penalty. This time, the value has to be copied out of the Object to be used in the IsNumeric, CInt and CDbl functions. This is because you can't pass an address to a reference type to a function that is expecting a value type. This unboxing allocates a new local value type and copies the value in the Object to it. This also takes time - I think it's about half as much as boxing, but to compared to pushing and popping a value on/off the stack, it's still an eternity.
Now, the saving grace in your code is that IsNumeric, CInt and CDbl are all compiled in-line. This means that in the compiled code, there is a performance benefit from not having to push values onto the stack, making a JMP to the function code, popping those values off the stack, running the function code, pushing the return value back on the stack, returning to the caller and, finally, popping the return values off the stack.
Dave Kreskowiak
Microsoft MVP - Visual Basic
|
|
|
|
|
I make a control in VB.Net and i want to use it in Internet Explorer.
This control connect a socket in special port on a server.
My control in local exe is ok but when i use it in IE i have an error about system.net.socketpermission
I add this lines in my code
Dim IPermSocket As New System.Net.SocketPermission(Security.Permissions.PermissionState.Unrestricted)
IPermSocket.AddPermission(Net.NetworkAccess.Connect, Net.TransportType.All, strIP, System.Net.SocketPermission.AllPorts)
IPermSocket.Demand()
and where cursor is on IPermSocket.Demand() i have the same error: autorisation failed
Is it possible to help me
|
|
|
|
|
Hello,
I am doing a application that will monitor up to 10 different tasks. Each task will start, run, and stop at different times.
I have to display each task in a label. I have 2 buttons start and stop.
l enter the task number in the text box and press start, I can do this will all the tasks. When l want to stop, I will enter the task number in the text box and press stop.
So I can start and stop any task and see the time of all the tasks that are currently running.
I have done this for 1 stopwatch object, but not sure how to create an array and monitor all the task the are currently running at the same time.
Here is my code so far so you can see what I am doing.
Start button
<br />
Dim taskID As String<br />
taskStopWatch.Start()<br />
Stop button
<br />
Dim taskID As Integer<br />
taskStopWatch.Stop()<br />
<br />
Private Sub tmrTaskLog_Tick(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles tmrTaskLog.Tick<br />
Dim ts As TimeSpan<br />
<br />
If (taskStopWatch.IsRunning) Then<br />
ts = taskStopWatch.Elapsed<br />
taskDuration = String.Format("{0:00}:{1:00}:{2:00}", ts.Hours, ts.Minutes, ts.Seconds)<br />
TimeTaskID.Text = taskDuration<br />
End Sub<br />
Thanks very much in advance
Steve
|
|
|
|
|
Create a HashTable and you can add all of your StopWatch objects to it. Ideally, you'd want to create a class that can track one process, complete with it's own StopWatch, and have each instance of that class watch one process. You can then add those to a HashTable to keep track of them all.
Dave Kreskowiak
Microsoft MVP - Visual Basic
|
|
|
|
|
Hi
How can i maximize the printpreviewdialog during runtime.
Thanxs
quinton.nel
|
|
|
|
|
The PrintPreviewDialog doesn't expose any methods to do this. You can either use it's .SetDesktopBounds() method to set the size as large as you want, or you'll have to go through Win32 API calls to find the window handle and send an appropriatly formatted window message to the form to get it to maximize itself.
Dave Kreskowiak
Microsoft MVP - Visual Basic
|
|
|
|
|
Hi all,
I am doing an desktop application in vb.net. For forms i have used the
property formborderstyle=none. So what happens when i minimize the form into
the task bar,and again right click on the form i could not the maximize or
minimize or restore. So please let me know how to do this.
regards
ravindra
|
|
|
|
|
Since you're form no longer has a control box in the now non-existant title bar, you don't have Minimizie, Maximize and Restore in the context menu either. But, just clicking on the icon in the TaskBar should restore the form. If not, the solution to restoring the context menu on the TaskBar is surprisingly easy.
Handle the form's Resize event. In that handler, check to see what the WindowState of the form is. If it's Minimized, set the FormBorderStyle to anything but None, otherwise reset it to None.
Private Sub Form1_Resize(blah, blah) Handles Me.Resize
If Me.WindowState = FormWindowState.Minimized Then
Me.FormBorderStyle = Windows.Forms.FormBorderStyle.Sizable
Else
Me.FormBorderStyle = Windows.Forms.FormBorderStyle.None
End If
End Sub
Now, there's a small problem that develops. Changing the FormBorderStyle like this also changes the size of the form. When it's restored, the form shows up smaller than before it was minimized. To get around this, set the form's MinimumSize property to the current size of the form BEFORE the form is minimized by your code:
Private Sub Button1_Click(blah, blah) Handles Button1.Click
Me.MinimumSize = Me.Size
Me.WindowState = FormWindowState.Minimized
End Sub
Dave Kreskowiak
Microsoft MVP - Visual Basic
|
|
|
|
|
Hi all,
Using StreamReader I opened a text file and reading line by line. In that I want replace the text say (File "604116mg_c.abc") to (File "604116g.abc") and update back in the text file. Please suggest me how to solve
sonj
|
|
|
|
|
You can not update part of a text file. You have to rewrite the entire file.
---
b { font-weight: normal; }
|
|
|
|
|
Hi
I've created a app for monitoring Internet access on workstations. I've created the project so that the main form's visible = false, when it detects the iexplore process it sets the visible = true and prompts the user for a session password. The problem is that my apps process is running in the Task Manager and the user can stop it from there. Thus dissabling my app.
I want to create a service that runs in the background and then starts my app to prompt user for session password. Is this possible and how can I implement it. Should I create a seperate windows service that starts my app or should I incorporate the service into my app.
quinton.nel
|
|
|
|
|
Hi all,
I have a problem with file List box,
I have a code like this
Private Sub Command1_Click()
Form1.FileListBox.Path = "d:\temp"
Form1.FileListBox.Pattern = "*.lnk"
MsgBox Form1.FileListBox.List(0)
End Sub
Suppose I have a shortcut d:\temp\hai.lnk
When I click on Command1 Button
Then "MsgBox Form1.FileListBox.List(0)" will get the value "d:\temp\hai.lnk"
Then I have deleted "hai.lnk" Manually and Again Click on Command1 Button,but "MsgBox Form1.FileListBox.List(0)" again showing "d:\temp\hai.lnk"
How can I Rectify it.
Please Help Me
Thanks IN Advance
George K Jolly
|
|
|
|
|
After you delete the file, call the Refresh method on the FileListBox:
FileListBox.Refresh()
Dave Kreskowiak
Microsoft MVP - Visual Basic
|
|
|
|
|
|
Hi Members,
I'm stuck in a project where I need to connect a scanner to a program(VB.net),i.e., on click of a button the scanner should start functioning.
How can I achieve that?
Regards,
Member.
|
|
|
|
|