Hey,
Can anyone help me solve this, i am trying to read the value of Console.Readline into a varible declared as Double using vb 2008. As shown below:

Private Sub ReadQuantity()
        ' Read ask customer to input many liters 
        Private quantity As Double
        Console.Write("How many liters (only whole liters please ") "? ")
        Console.ReadLine()
        
       quantity = Double.Parse(Console.ReadLine())
        quantity = Convert.ToDouble(Console.ReadLine())

I tried both coversion function above but got FormatException errors. Is there a way around this or can i use a different approach.

Hey,
Can anyone help me solve this, i am trying to read the value of Console.Readline into a varible declared as Double using vb 2008. As shown below:

Private Sub ReadQuantity()
' Read ask customer to input many liters
Private quantity As Double
Console.Write("How many liters (only whole liters please ") "? ")
Console.ReadLine()

quantity = Double.Parse(Console.ReadLine())
quantity = Convert.ToDouble(Console.ReadLine())

I tried both coversion function above but got FormatException errors. Is there a way around this or can i use a different approach.

Hi!

Guess this is a conversion from string to double... so can you check "." or ","

Regards

Thanks Dix,
But i'm not understanding what i should try,
and yes i am trying to do a conversion. Could you expand on your response please thanks.

Thanks Dix,
But i'm not understanding what i should try,
and yes i am trying to do a conversion. Could you expand on your response please thanks.

Can you tell me what is your input?
eg :

dim input as string = console.readline()

if input has a "." and throw formatException then replace the "." by a ","
and if input has "," and throw formatException then replace "," by a "."

Let me know if it works.

Regards

Dim temp, quantity As Double      
Console.Write("How many liters (only whole liters please ) ? ")
temp = Console.ReadLine()
quantity = Double.Parse(temp)

can you show me what is the content of "TEMP"?
just put a breakpoint after

temp = console.readline()

and paste the content of temp here.

Regards

also Temp has to be be DECLARED AS STRING

regards

My input is coming from:
Console.Write("How many liters (only whole liters please ") "? ")

I did not declare a string variable to store the input because i need it to be store as a Double for use in a calculation method. if that answer your ?
Do you know of a way i can Read an input line and some how change the returned value data type from String?

No...

what i meant is to retrieve your KEYBOARD INPUT

When you ask user to input how many liters

Just store the "CONSOLE.READLINE" in a STRING VARIABLE to check his content.

and then we can see why you have a formatexception when you did a convert.todouble.

Regards

Could the ToString method be use to slove this in anyway? I am not that experience in VB.Net

i just create a console project and put your code and it works...

Ok Dnx,
So did that, but how do i check if its "." or "," i could only deplay the value in the program at run time.

3dees

it worked???
Did you convert it to double as well?

yes, i didn't change anything in your code

maybe check what you enter... and regional settings?

ok Dnx
Tried it again and it worked, will have to check to understand why it didn't worked before.

Dim Liters As String = Console.ReadLine()
quantity = Convert.ToDouble(Liters)

Thanks very much Dnx.

No problem, glad i could help even if your code was good since the beginning ;)

Regards.

Hey sometimes it just takes a 2nd pair of eyezzzzzz + just this week load VB2008 so now sure if i have all the ms updates.

hay,
the below method is the method to use type casting and it will execute
u check it (if u want to cast as value use below or else use type cast(source,typename))
Dim a, quantity As Double
Console.Write("How many liters(only whole liters please )? ")
a = Console.ReadLine()
quantity = CType(a, Double)
Console.WriteLine(quantity)
reply pls.,,

I know of the "Convert "class in vb and i think you can try this

dim quantity as double
quantity=convert.toDouble(Console.Readline)
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.