logo VB.Net - Chemical Elements
Guide Contents
DigitalDan Home Page

This page demonstrates using lists and dictionaries to find information. It is based on a list of chemical elements. To simplify the examples, they use "hard coded" entrries instead of reading files
 
Get Element Name from Atomic Number
Private Shared Function AtomicNumber_To_ElementName(atomicNumber As Integer) As String
Dim ListName As New List(Of String)
If atomicNumber < 1 Or atomicNumber > 118 Then Return ""
ListName.Clear()
' This block loads the list of elemets
' atomic numbers start at 1 but lists start at zero. The leading comma creates dummy first record to start list at 1
ListName.AddRange(",hydrogen,helium,lithium,beryllium,boron,carbon,nitrogen,oxygen,fluorine,neon,sodium,magnesium".ToLower.Split(","c).ToList)
ListName.AddRange("aluminium,silicon,phosphorus,sulfur,chlorine,argon,potassium,calcium,scandium,titanium,vanadium".ToLower.Split(","c).ToList)
ListName.AddRange("chromium,manganese,iron,cobalt,nickel,copper,zinc,gallium,germanium,arsenic,selenium,bromine".ToLower.Split(","c).ToList)
ListName.AddRange("krypton,rubidium,strontium,yttrium,zirconium,niobium,molybdenum,technetium,ruthenium,rhodium".ToLower.Split(","c).ToList)
ListName.AddRange("palladium,silver,cadmium,indium,tin,antimony,tellurium,iodine,xenon,caesium,barium,lanthanum".ToLower.Split(","c).ToList)
ListName.AddRange("cerium,praseodymium,neodymium,promethium,samarium,europium,gadolinium,terbium,dysprosium,holmium".ToLower.Split(","c).ToList)
ListName.AddRange("erbium,thulium,ytterbium,lutetium,hafnium,tantalum,tungsten,rhenium,osmium,iridium,platinum,gold".ToLower.Split(","c).ToList)
ListName.AddRange("mercury,thallium,lead,bismuth,polonium,astatine,radon,francium,radium,actinium,thorium,protactinium".ToLower.Split(","c).ToList)
ListName.AddRange("uranium,neptunium,plutonium,americium,curium,berkelium,californium,einsteinium,fermium,mendelevium".ToLower.Split(","c).ToList)
ListName.AddRange("nobelium,lawrencium,rutherfordium,dubnium,seaborgium,bohrium,hassium,meitnerium,darmstadtium".ToLower.Split(","c).ToList)
ListName.AddRange("roentgenium,copernicium,nihonium,flerovium,moscovium,livermorium,tennessine,oganesson".ToLower.Split(","c).ToList)
' This returns the approriate element form the list
Return ListName.Item(atomicNumber)
End Function

 
Get Element Symbol from Atomic Number
Private Shared Function AtomicNumber_To_Symbol(atomicNumber As Integer) As String
Dim ListSymbol As New List(Of String)
If atomicNumber < 1 Or atomicNumber > 118 Then Return ""
ListSymbol.Clear()
' atomic numbers start at 1 but lists start at zero. The leading comma creates dummy first record to start list at 1
' this block loads the list of symbols
ListSymbol.AddRange(",H,He,Li,Be,B,C,N,O,F,Ne,Na,Mg,Al,Si,P,S,Cl,Ar,K,Ca,Sc,Ti,V,Cr,Mn,Fe,Co,Ni,Cu,Zn".Split(","c).ToList)
ListSymbol.AddRange("Ga,Ge,As,Se,Br,Kr,Rb,Sr,Y,Zr,Nb,Mo,Tc,Ru,Rh,Pd,Ag,Cd,In,Sn,Sb,Te,I,Xe,Cs,Ba,La,Ce".Split(","c).ToList)
ListSymbol.AddRange("Pr,Nd,Pm,Sm,Eu,Gd,Tb,Dy,Ho,Er,Tm,Yb,Lu,Hf,Ta,W,Re,Os,Ir,Pt,Au,Hg,Tl,Pb,Bi,Po,At".Split(","c).ToList)
ListSymbol.AddRange("Rn,Fr,Ra,Ac,Th,Pa,U,Np,Pu,Am,Cm,Bk,Cf,Es,Fm,Md,No,Lr,Rf,Db,Sg,Bh,Hs,Mt,Ds,Rg".Split(","c).ToList)
ListSymbol.AddRange("Cn,Nh,Fl,Mc,Lv,Ts,Og".Split(","c).ToList)
' this returns the approriate entry form the list
Return ListSymbol.Item(atomicNumber)
End Function

 
Get Atomic Number from Element Name
Private Shared Function ElementName_To_AtomicNumber(name As String) As Integer
' The name must exactly match entry in list and be in lower-case.
' Load list of elements into a list - it will be easier to understand copying from list
' to a dictionary than loads of lines adding to dictionary one entry at a time
Dim ListName As New List(Of String)
ListName.Clear()
' this block loads the list of names
ListName.AddRange("hydrogen,helium,lithium,beryllium,boron,carbon,nitrogen,oxygen,fluorine,neon,sodium,magnesium".ToLower.Split(","c).ToList)
ListName.AddRange("aluminium,silicon,phosphorus,sulfur,chlorine,argon,potassium,calcium,scandium,titanium,vanadium".ToLower.Split(","c).ToList)
ListName.AddRange("chromium,manganese,iron,cobalt,nickel,copper,zinc,gallium,germanium,arsenic,selenium,bromine".ToLower.Split(","c).ToList)
ListName.AddRange("krypton,rubidium,strontium,yttrium,zirconium,niobium,molybdenum,technetium,ruthenium,rhodium".ToLower.Split(","c).ToList)
ListName.AddRange("palladium,silver,cadmium,indium,tin,antimony,tellurium,iodine,xenon,caesium,barium,lanthanum".ToLower.Split(","c).ToList)
ListName.AddRange("cerium,praseodymium,neodymium,promethium,samarium,europium,gadolinium,terbium,dysprosium,holmium".ToLower.Split(","c).ToList)
ListName.AddRange("erbium,thulium,ytterbium,lutetium,hafnium,tantalum,tungsten,rhenium,osmium,iridium,platinum,gold".ToLower.Split(","c).ToList)
ListName.AddRange("mercury,thallium,lead,bismuth,polonium,astatine,radon,francium,radium,actinium,thorium,protactinium".ToLower.Split(","c).ToList)
ListName.AddRange("uranium,neptunium,plutonium,americium,curium,berkelium,californium,einsteinium,fermium,mendelevium".ToLower.Split(","c).ToList)
ListName.AddRange("nobelium,lawrencium,rutherfordium,dubnium,seaborgium,bohrium,hassium,meitnerium,darmstadtium".ToLower.Split(","c).ToList)
ListName.AddRange("roentgenium,copernicium,nihonium,flerovium,moscovium,livermorium,tennessine,oganesson".ToLower.Split(","c).ToList)
' Load a dictionary with all the element-names as key and atomic-numbers as value
Dim DictNames As New Dictionary(Of String, Integer)
DictNames.Clear()
For i As Integer = 1 To 118
DictNames.Add(ListName.Item(i - 1), i)
Next
' Use dictionary to look up element name
Dim atomicNUmber As Integer = -1
DictNames.TryGetValue(name, atomicNUmber)
If atomicNUmber < 1 Then Return 0
Return atomicNUmber
End Function

 
Get Atomic Number from Element Symbol
Private Shared Function Symbol_To_AtomicNumber(symbol As String) As Integer
' The symbol must exactly match entry in list.
' Element symbols must start with upper case Letter. Any other letters are lower case
' Load list of elements into a list - it will be easier to understand copying from list
' to a dictionary than loads of lines adding to dictionary one entry at a time
Dim ListSymbol As New List(Of String)
ListSymbol.Clear()
' this block loads the list of symbols
ListSymbol.AddRange(",H,He,Li,Be,B,C,N,O,F,Ne,Na,Mg,Al,Si,P,S,Cl,Ar,K,Ca,Sc,Ti,V,Cr,Mn,Fe,Co,Ni,Cu,Zn".Split(","c).ToList)
ListSymbol.AddRange("Ga,Ge,As,Se,Br,Kr,Rb,Sr,Y,Zr,Nb,Mo,Tc,Ru,Rh,Pd,Ag,Cd,In,Sn,Sb,Te,I,Xe,Cs,Ba,La,Ce".Split(","c).ToList)
ListSymbol.AddRange("Pr,Nd,Pm,Sm,Eu,Gd,Tb,Dy,Ho,Er,Tm,Yb,Lu,Hf,Ta,W,Re,Os,Ir,Pt,Au,Hg,Tl,Pb,Bi,Po,At".Split(","c).ToList)
ListSymbol.AddRange("Rn,Fr,Ra,Ac,Th,Pa,U,Np,Pu,Am,Cm,Bk,Cf,Es,Fm,Md,No,Lr,Rf,Db,Sg,Bh,Hs,Mt,Ds,Rg".Split(","c).ToList)
ListSymbol.AddRange("Cn,Nh,Fl,Mc,Lv,Ts,Og".Split(","c).ToList)
' Load a dictionary with all the element-symbols as key and atomic-numbers as value
Dim DictNames As New Dictionary(Of String, Integer)
DictNames.Clear()
For i As Integer = 1 To 118
DictNames.Add(ListSymbol.Item(i - 1), i)
Next
' Use dictionary to look up element symbol
Dim atomicNUmber As Integer = -1
DictNames.TryGetValue(symbol, atomicNUmber)
If atomicNUmber < 1 Then Return 0
Return atomicNumber
End Function

DigitalDan.co.uk ... Hits = 110