PDA

View Full Version : MSVB help....


DukeRick81
01-31-2005, 09:51 AM
This is what I have: 2 textboxes and 1 commandbutton. I need to enter 2 number (at least one of them has to be float or double) so this is the code:

Private Sub Command1_Click()
Dim a, b, c As Double

a = Val(Text1.Text)
b = Val(Text2.Text)
c = a * b
MsgBox "r=" & c
End Sub

Well everything looks fine but if, for example, I enter 2.2 and 2 I will get in the message box "r=4,4", which is right, but I do need to display "r=4.4" instead.

Thanks in advance .

rsmedts
01-31-2005, 01:37 PM
Your regional settings are probably set to the comma as the decimal seperator. You could try fiddling with that.
The straightforward way to fix your problem is the replace function though:
MsgBox "r=" & Replace(c,",",".")

DukeRick81
01-31-2005, 02:07 PM
Thank you that really worked good.

Nexus_sa
01-31-2005, 04:58 PM
Dim a, b, c As Double


Will not do what you expect it do. You're actually declaring a and B as variants and c as a double.

Without VB.NET you have to do it as follows:

Dim a as double
dim b as double
dim c as double


Edit: I think that'll take care of your 4,4 problem as well but I'd have to check.

Nex

Ares
02-27-2005, 03:28 PM
Nexus_sa said:
Dim a, b, c As Double


Will not do what you expect it do. You're actually declaring a and B as variants and c as a double.

Without VB.NET you have to do it as follows:

Dim a as double
dim b as double
dim c as double


Edit: I think that'll take care of your 4,4 problem as well but I'd have to check.

Nex



most of time i declare like that and it is all going fine, if you say a,b,c as string then they are all strings etc

Little Conqueror
02-28-2005, 12:16 PM
http://forums.3drealms.com/ubbthreads/images/graemlins/ted.jpg

YOU CAN DIMENSION THEM ALL IN THE SAME LINE!

Nexus_sa
02-28-2005, 07:50 PM
most of time i declare like that and it is all going fine, if you say a,b,c as string then they are all strings etc




Most of the time it works fine because it treats a and b as a variant type. It is poor practice, however, and a/b are variants whether you want them to be strings or not http://forums.3drealms.com/ubbthreads/images/graemlins/wink.gif


if you say a,b,c as string then they are all strings etc



100% false. Sorry. Only C is a string type.

Nex

Nexus_sa
02-28-2005, 07:59 PM
MSDN article on declaring variables. Please note the vb6 annotation. (http://support.microsoft.com/default.aspx?scid=kb%3Ben-us%3B311331)

For those too lazy to click the link:


Multiple Declarations on the Same Line of Code
In previous versions of Visual Basic, when multiple declarations appear on the same line, you must explicitly declare all variables with an AS clause to explicitly type those variables. To type all variables explicitly on the same line in Visual Basic .NET, you can declare a type for only the last variable.

In Visual Basic 6.0, the declaration Dim x,y as String

results in variable x as type Variant and variable y as type String.

In Visual Basic .NET, the declaration Dim x,y as String

results in variable x as type String and variable y as type String.



It's a pretty rookie mistake and it troubles me that there are those arguing against the facts. If you're going to program, understand the constraints of the language.

Nex

Little Conqueror
02-28-2005, 09:13 PM
You assumed he wasn't using VB.NET. How did you know which one he was using then?

Because IIRC, most VB6 functions/methods are also accepted by VB.NET, except it does dimension all of the "a,b,c, etc." as the variable type specified in the same line.

I don't see any VB6-specific stuff in that code he posted.

Nexus_sa
02-28-2005, 10:16 PM
You're right, there's no reference of the language version (other than VB.Net is traditionally referred to as such) and the code he's using would compile in either version. I do however make explicit reference to the difference between VB.NET and VB6 in my prior post regarding variable declaration so it's not rocket science to know that if he's using VB.NET the variable declaration was performing as expected and if using VB6 it wasn't.

What became a topic of discussion, though, was that some people believed that VB6 performed the same way as .NET when it come to declaring variables. That is simply wrong and that is what I'm addressing here.


I don't see any VB6-specific stuff in that code he posted.



I don't see any .NET specific references in your previous rebuttle. http://forums.3drealms.com/ubbthreads/images/graemlins/wink.gif

Nex

NetNessie
02-28-2005, 10:24 PM
Little Conqueror said:
http://forums.3drealms.com/ubbthreads/images/graemlins/ted.jpg
YOU CAN DIMENSION THEM ALL IN THE SAME LINE!



That was really funny... the Ted face made it hilarious.