I have a class named Account so I have a question. In the declarations of a new instance

Dim Newaccount as Account -----but I can declare this like..
Dim Newaccount As Account = New Account

which are the differences between both declarations?? are the same or not?


excuse me english...

besides I have another question

I have 3 functions in mi clasee

writename()
writeaccount()
writesave()

the first 2 functinos are string..so i have this.

IF name = "" then
Console.writeline("name is empty")

is the same with account and save
but the problem with save is DOUBLE so if is ="" ..I have this error.....saying....Conversion string "" in TYPE DOUBLE is invalid.............

what I can do??? because I want textmessage saying "save is empty" IF is empty

Dani AI

Generated

Two separate issues here: variable declaration/instantiation, and the Double/empty-string error.

As and pointed out, declaring a variable of a reference type only creates a reference; it does not create an object. That reference will be Nothing until you create or assign an instance, and calling members on Nothing throws a NullReferenceException. See the VB Dim rules and the Nothing literal for details: Dim statement, .

The Double problem comes from trying to use or pass an empty string where a Double is expected. If you call a Sub declared as Sub Save(ByVal sav As Double) with a TextBox.Text that is "" (or use Convert.ToDouble on ""), VB/.NET will attempt a conversion and throw a FormatException. The correct place to handle this is before conversion: validate and parse the user string, then call the Sub with a valid Double (or change the Sub to accept a nullable Double or a string and parse inside).

A simple, robust approach is to use Double.TryParse to avoid exceptions and to detect invalid/empty input. For example:

Dim raw As String = TextBox1.Text.Trim()
Dim sav As Double

If Not Double.TryParse(raw, sav) Then
    MessageBox.Show("Save is empty or not a valid number")
    Exit Sub
End If

mainsave = sav

Notes and alternatives:

  • Do not treat 0.0 as "empty" unless 0 is invalid for your domain; users may legitimately enter 0.
  • If you need to allow "no value", use Nullable(Of Double) (Double?) and check HasValue.
  • You can also perform the validation at the caller (before invoking the Sub) or change the Sub to accept a String and do parsing inside.

For parsing guidance see the .NET docs on TryParse: Double.TryParse.

Recommended Answers

All 7 Replies

The first just declares a variable as the class. The second creates an instance of it.
Without seeing your code I would say you need to put in "Exit Sub" after your If statement.
If it is a subroutine:

IF name = "" then Console.writeline("name is empty") : Exit Sub

If it is a function:

IF name = "" then Console.writeline("name is empty") : Return Nothing

Sorry..the 3 are not function the 3 are SUb.....so how can I fix the error with double type..string is not a problem....but when is double...a exception..appears..

beside the diffrences between

use a instance of the class with using a variable.to use the class...like my first post

hi,

Dim Newaccount as Account -----but I can declare this like..
Dim Newaccount As Account = New Account

First one is just a Reference not Referring any object. With out referring any object using reference (Like first one) is an exception (NullPointerException)

Second one is a reference and referring an Account object.

And then

Are you mean how to check when double valued field is left out? I think so

when no value given to double (that is default value ) is 0.0

So

Dim Save As Double
.....
  If Save = 0.0 Then
        MessageBox.Show ("Empty Double")
  End If

hi,

First one is just a Reference not Referring any object. With out referring any object using reference (Like first one) is an exception (NullPointerException)

Second one is a reference and referring an Account object.

And then

Are you mean how to check when double valued field is left out? I think so

when no value given to double (that is default value ) is 0.0

So

Dim Save As Double
.....
  If Save = 0.0 Then
        MessageBox.Show ("Empty Double")
  End If

yeah but I am creating the error message in the sub..beside..is not working.neither...if the user doesnt write nothing and the field is empty like this = "" a exception appears I try the 0.0 but nothing the same.. string "" cant convert to double

Public Sub save(ByVal sav As Double)
If sav= 0.0 Then
Console.WriteLine("Is not valid")

Else

mainsave = sav
End If

i need sample project using vb.net if u know anything please send me

i need vb.net sample projects

Public Sub save(ByVal sav As Double)
If sav= 0.0 Then
Console.WriteLine("Is not valid")

Else

mainsave = sav
End If

what I understand that the user will enter the value in textbox , after you check that user did not leave it empty you can use it .

Dim save As Double
        If TextBox1.Text = "" Then  ' check if the textbox is empty
            save = 0.0
            MessageBox.Show("Empty Double")
        Else
            save = Convert.ToDouble(TextBox1.Text) 'if it is not empty convert the string to double and store it in save variable 
            
        End If

also you can validate the user's input before you store it in save variable.
I hope that help you

i need sample project using vb.net if u know anything please send me

it is better you open new thread, and i suggest to search here since many ask same thing , Good Luck

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.