TheDeveloperBlog.com

Home | Contact Us

C-Sharp | Java | Python | Swift | GO | WPF | Ruby | Scala | F# | JavaScript | SQL | PHP | Angular | HTML

<< Back to VBNET

VB.NET Nullable

Use nullables to allow an integer to be null. Call GetValueOrDefault and use the Value property.
Nullable. Consider an Integer in VB.NET. It cannot be null—it can only have a numeric value. But we can wrap the Integer inside a nullable Structure, which can logically be Nothing.NothingStructure
With properties and functions, we can test a Nullable. We specify a nullable type by adding a question mark after the type name. For the value, we can access Value.Property
An example. Here we see a nullable Integer named "element." We use the Integer in various ways. We use the Nothing value to indicate "null" in the VB.NET language.

HasValue: This property returns true or false. It indicates whether an underlying value is contained in the nullable.

Boolean

GetValueOrDefault: This function safely returns the underlying value. If the nullable has no value, it returns the value type's default.

Value: This property will throw an exception if the nullable has no value. Prefer GetValueOrDefault to avoid exceptions.

VB.NET program that uses nullable integer Module Module1 Sub Main() ' A nullable can be Nothing. Dim element As Integer? = Nothing Console.WriteLine("HASVALUE: {0}", element.HasValue) Console.WriteLine("GETVALUEORDEFAULT: {0}", element.GetValueOrDefault()) ' Use an Integer value for the nullable Integer. element = 100 Console.WriteLine("VALUE: {0}", element.Value) Console.WriteLine("GETVALUEORDEFAULT: {0}", element.GetValueOrDefault()) End Sub End Module Output HASVALUE: False GETVALUEORDEFAULT: 0 VALUE: 100 GETVALUEORDEFAULT: 100
Notes, null. In VB.NET the keyword "Nothing" is used to indicate null. This makes types like nullable somewhat confusing, because they are based on C# terms.
A summary. What is a null Integer or other null value type? A null thing can be used to indicate an uninitialized value—we can have uninitialized, or an actual number set.
In this way, nullable types can help eliminate ambiguous parts of programs. We do not need to use -1 as a special value meaning "not set." We can use null.
© TheDeveloperBlog.com
The Dev Codes

Related Links:


Related Links

Adjectives Ado Ai Android Angular Antonyms Apache Articles Asp Autocad Automata Aws Azure Basic Binary Bitcoin Blockchain C Cassandra Change Coa Computer Control Cpp Create Creating C-Sharp Cyber Daa Data Dbms Deletion Devops Difference Discrete Es6 Ethical Examples Features Firebase Flutter Fs Git Go Hbase History Hive Hiveql How Html Idioms Insertion Installing Ios Java Joomla Js Kafka Kali Laravel Logical Machine Matlab Matrix Mongodb Mysql One Opencv Oracle Ordering Os Pandas Php Pig Pl Postgresql Powershell Prepositions Program Python React Ruby Scala Selecting Selenium Sentence Seo Sharepoint Software Spellings Spotting Spring Sql Sqlite Sqoop Svn Swift Synonyms Talend Testng Types Uml Unity Vbnet Verbal Webdriver What Wpf