|
Disable the close button?? Nope. You just have to be very clever in the way you handle the form being closed.
|
|
|
|
|
I have to fix a defect raised for an old legacy VB6.0 application. I am seeing really weird behavior when subtracting two double values.
I have two double variables. d = 2.3434543434E+15 and d1 = 2.3434543434E+15 . Both have same values. Weird thing is, d1 - d gives me 2 instead of 0! Just to see where I am going wrong, I ported this piece of code to C# and everything worked fine there. If I do d1 / d , I am getting 1 in VB which is correct. But what's wrong with the subtraction?
Any ideas?
|
|
|
|
|
Hi,
there most likely is nothing wrong. Have a look here[^].
It is inherent to floating-point numbers: they trade some accuracy for a much larger range.
A floating-point number stores a mantissa, an exponent and a sign.
the exponent indicates a power of 2, not 10, and uses anywhere between 8 and 16 bits.
the mantissa indicates a magnitude in a limited range, say in [0.5, 1) and uses some 23 to 50 bits
(details vary from one CPU family to the next, IEEE did some standardization attempts, not relevant here).
You have chosen to show the number as d = 2.3434543434E+15, now why didn't you chose d = 2.343454343411111111111E+15? I added some ones to emphasize that you stopped the representation hoping the decimal digits shown, exactly correspond to the binary bits present in the mantissa, which will not be true in general.
So one number could be 2.343454343411116E+15 whereas the other could be 2.343454343411114E+15 (I truncated where the decimal point would be when looking at integer values). So now the difference is exactly 2, whereas the quotient is 1 + epsilon, epsilon being a very small number (around 1/1.17E15);
a double would be able to hold epsilon very accurately, however 1+epsilon would not be discernible from 1.
It is a well known fact in numeric analysis subtracting two large and similar numbers increases the relative errors. Example: if you want to measure the thickness of a wall, you could measure the wall-to-wall distance of a room on the inside, do the same on the outside, and subtract. If you make a 1% error on those dimensions, your calculated wall thickness could be off by 10% or more.
If you want to see the details, try to have a look at the bit patterns used by d1 and d2; in .NET BitConverter.GetBytes() would give you access to them, I don't recall if and how it could be done in ancient VB.
A much simpler example illustrating the same principles would be to calculate 1/3 or 1/7, then multiplying again by 3 or 7; the result will not always be 1!
PS: the above is true in many/all programming languages; some do offer extra data types (such as decimal) to get yet another trade of of accuracy and range.
Luc Pattyn [Forum Guidelines] [My Articles]
The quality and detail of your question reflects on the effectiveness of the help you are likely to get.
Show formatted code inside PRE tags, and give clear symptoms when describing a problem.
|
|
|
|
|
You really ARE the man, Luc! I very much enjoy reading your responses...
|
|
|
|
|
Ditto to that! Almost like Luc knew something about big numbers
"There's no such thing as a stupid question, only stupid people." - Mr. Garrison
|
|
|
|
|
big numbers don't fit in a double.
I have been busy creating my own BigInteger library, where arbitrary length integers can be stored and operated upon. Got somewhat stuck on efficient division and square rooting, and now .NET 4.0 is finally offering such class.
FWIW: we humans don't know any big number; whatever number we come up with, it is always part of a really tiny miniscule fraction of the numeric universe, almost all existing numbers happen to be larger... and less popular.
Luc Pattyn [Forum Guidelines] [My Articles]
The quality and detail of your question reflects on the effectiveness of the help you are likely to get.
Show formatted code inside PRE tags, and give clear symptoms when describing a problem.
|
|
|
|
|
Thanks Luc. That was a mind blowing answer. A well deserved 5.
|
|
|
|
|
you're welcome.
Luc Pattyn [Forum Guidelines] [My Articles]
The quality and detail of your question reflects on the effectiveness of the help you are likely to get.
Show formatted code inside PRE tags, and give clear symptoms when describing a problem.
|
|
|
|
|
I have done some more analysis with other languages. Here is what I found,
double d1 = 2.343454343411114E+15;
double d2 = 2.343454343411116E+15;
double sub = d2 - d1;
double div = d2 / d1; In C++ (compiled with gcc on LINUX) gives sub = 2 and div = 1 . In C# the same code gives sub = 2 and div = 1.0000000000000009 .
|
|
|
|
|
Hi,
on the same hardware, one would expect identical results, except for one thing: when you let the code show a floating-point value without specifying explicitly how many digits should be shown, then (the designers of the run-time library of) some languages may choose to show as many digits as are likely to be significant (e.g. always 16 digits, possibly truncating all the zeroes to the right), while other languages might choose to show a much lower number of digits by default (trying to show "rounder" numbers, i.e. hiding the anomalies to some extent).
Most languages also allow explicit specification of the output format (as in %22.16d in C); when such is done equivalently in different languages on the same hardware, I would expect the results to be identical.
Luc Pattyn [Forum Guidelines] [My Articles]
The quality and detail of your question reflects on the effectiveness of the help you are likely to get.
Show formatted code inside PRE tags, and give clear symptoms when describing a problem.
|
|
|
|
|
hi everyone .. well i am truing 2 make a button when i click it would truncate the data in table , i am trying 2 do this by vb.net 2005.
so if anyone got the code or an example 4 it would b nice .
thanx 4 all.
|
|
|
|
|
Firstly you will need to define what you mean by truncate.
Secondly you need to show the code that you have tried, together with details of where it fails, and any error messages.
Henry Minute
Do not read medical books! You could die of a misprint. - Mark Twain
Girl: (staring) "Why do you need an icy cucumber?"
“I want to report a fraud. The government is lying to us all.”
|
|
|
|
|
ok , first i want to clear all data in specific table in sql2005 data base , and i need 2 execute the order " truncate table xxxxx " not from the sql query analyzer , but from vb.net code , which i am trying 2 make here :
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
Dim strSQL As String
strSQL = "TRUNCATE TABLE names"
Using connection As New SqlConnection(constring5)
Dim command As New SqlCommand(strSQL, SqlConnection1)
command.Connection.Open()
Command.ExecuteNonQuery()
End Using
End Sub
it gives me error message in code
command.Connection.Open()
<b>"The ConnectionString property has not been initialized."</b>
so i dont know what i should do
thanx
|
|
|
|
|
OK.
Using connection As New SqlConnection(constring5)
Dim command As New SqlCommand(strSQL, SqlConnection1)
this is where I suspect your problem lies.
In the first line you set up connection , then in the second line, instead of using connection you use SqlConnection1 . Why? Try replacing SqlConnection1 by connection .
Henry Minute
Do not read medical books! You could die of a misprint. - Mark Twain
Girl: (staring) "Why do you need an icy cucumber?"
“I want to report a fraud. The government is lying to us all.”
|
|
|
|
|
thank you
|
|
|
|
|
Is it possible to export data to Excel and export Excel Formula to designated columns from a VB 2008 application ( And if so how)
|
|
|
|
|
There seem to be a number of articles in CP on the subject. Here[^] is what my search found. And this one[^] in particular looks very well done. Hope this helps you get started.
|
|
|
|
|
Thanks kschuler that seems to answer all my qiestions
|
|
|
|
|
I'm trying to read a datagridview and post the changes back to the SQL database. Does anyone have some straightforward code that will work. I'm a newbie in VB.net.
|
|
|
|
|
Hi Roger,
When people ask for "Straightforward code" it's kind of impossible to respond. All code is tailored to an individual need and no one is going to have EXACTLY the code you want. (Nor will they create it for you if you are not willing to put some effort into it yourself.) The best way to start a project like this is to read some articles about what you are doing. You can search codeproject for articles, like this.[^]. Then as you implement code you've found in the article, if you run into problems post your code and error messages and a description of what your program is doing and what you want it to do. Then we can help you. Hope this gives you a good place to start.
|
|
|
|
|
I think it's a fair question, and one that has recently been much on my mind.
In VB6, making an grid you could update was completely trivial, under VB.NET, after several hours, I'm completely baffled.
Are we supposed to hand-code an add / update / insert statement, somehow connect it to something? Is a "CommandBuilder" somehow involved?
Is there some "do it automatically" switch somewhere I'm not pushing?
adapter.update(dataset) doesn't throw an error, and doesn't do anything either.
You'd think someone would have posted a sample project, where you put the query in a textbox, click 'execute', and get a datagridview you can edit, like you could do in 5 minutes in VB6. There are DataGrid based examples from VS2005, but I haven't seen any for the DataGridView. At present, my guess is because this is next to impossible with the DataGridView.
Near as I can tell, I'm doing what the instructions say, but nothing happens. Maybe NPGSQL doesn't work?
|
|
|
|
|
I agree with you, that the question is valid. I just felt like it was too broad a topic and requires more information to decently answer in the forums as is.
I haven't really ever used VB6, so I'm not aware of the differences between it and VS2003 or above. As for how to add the add/update/insert statement easily, it also depends on the data provider you are using. If you use OleDb or SqlClient you should be able to use a CommandBuilder. You just need to write the Select SQL Statement, set it to the DataAdapter, and then put this statement (if using SqlClient provider):
Dim cb As New SqlClient.SqlCommandBuilder(myDataAdapter)
before you call the update method of the DataAdapter object. It's also a good idea to clean up after the update with this statement:
cb.Dispose()
I'm not sure if it works exactly the same way if you are working primarly in design mode, as I mostly code all my database stuff instead of using the visual studio designer to drag and drop DataAdapters, Connections, etc.
Since there are so many possible scenarios, it's hard to give more advice than this. Which is why it's so much better to post your code when asking for help. Please post what you have for your update and we can give you some help and advice on what is not working.
|
|
|
|
|
Aha!
That did it. I added:
dim cb as new NpgsqlCommandBuilder
cb = New NpgsqlCommandBuilder(m_da)
Where:
m_da is a NpgsqlDataAdapter control in the design view.
m_cmd is a NpgsqlCommand in the design view.
m_ds is a System.Data.Dataset in the design view.
New code is after putting the SQL Select statement into m_cmd, and before m_da.fill(m_ds)
Thanks very much, this is *much* simpler than some sites made it look.
These seems more complicated than it was in VB6. Then, you had a datacontrol and a grid. Load the datacontrol's connection string, set the datasource to the query, 'refresh', all done.
Now I have something that works like the demo I described, but it's probably not a very good example, don't know which of the things I've pulled from the web actually contribute to it working yet.
At the risk of maybe should have clicked "Rant..."
I've only recently started using VB.NET. Started with VB in VB3. Stopped in VB6, because the upgrade wizard crashed on my 300 form, 250 modules/classes application. As of VS2008, upgrade wizard didn't crash, but it's looking like an 80% rewrite; what the upgrade wizard left looks like a transporter accident. data binding (in the converted application) is demolished, usercontrols didn't port for beans, deep heavy sigh.
My impression at this point is that VB has lost it's direction - it used to be going toward 'Simpler', ever easier to build stuff. Now it's trying to compete (and doing well at it) with Java and C++. I spent a decade programming in each of those, and chose VB6 for my own projects, because I like results more than tinkering with strongly typed stuff. I liked the direction VB was going, but it took an abrupt left turn with VB.NET.
Hopefully I'll get used to it. Probably slightly easier than relearning Java.
|
|
|
|
|
In one of the regular Spreadsheets I am asked to do, I need a Column that does a complicated SUMming.
There are a number of columns of data, between 2 and, say, 10 columns (Depending on other factors).
In Each Column there is a set of data such as Land Price, Build Cost Prelims etc. At the Head of Each column is a number representing How Many of Each Type.(The column data is shown for the cost of 1 of each type).
Currently I am having to type in the cell the following...
=($G$5*G8)+($H$5*H8)+($I$5*I8) etc (not too bad with three columns, but if there are 10 or more it takes a while!
(Because of the $'s I can Copy Down with impunity)
So at the end column for each ROW I get a total cost for the project on each of the Parts.
I would like to do away with having to waste my life and that of my keyboard by having a Function that I can put into the Totals Column.
Something like =FACTOREDTOTAL(5,("G5"),6)
This being there are 5 columns of data, locked at G5 for the first column (but moving 1 column along each iteration), starting 6 columns to the left.
Does that all make sense?
OK, I have tried, and tried, I have failed and failed.
I know it must be possible either as a function or as a Macro, but either I am dim or my brain is fried, but has anyone got any pointers to this?
MOD - I can do it as a macro, but I would rather have it as a function!
------------------------------------
"When Belly Full, Chin Hit Chest"
Confucius 502BC
modified on Tuesday, June 2, 2009 10:28 AM
|
|
|
|
|
You could build a function to do this, but why ?
Just keep the complicated formula as a text file on your desktop and monthly type it in. Then copy it down for the number of rows in your spreadsheet.
my 2 cents
|
|
|
|
|