Solved

Use List(of Type)

Posted on 2015-01-10
6
107 Views
Last Modified: 2015-01-11
Below, stying with:

 Dim value As New List(Of Importance)

Or modifying it a bit, I want to print the items in Enum Importance. I want to practice the use List(of Type).

This code doesn't work. Could you please help to make it work possibly using the above objective.
  Enum Importance
        Critical = 4
        Important = 3
        None = 0
        Regular = 2
        Trivial = 1
    End Enum

    Private Sub Enum_Select_TypoOf()
        Dim value As New List(Of Importance)
        Dim i As Integer
        For i = 0 To value.Count
            Console.WriteLine(value(i))
        Next i
        Console.ReadLine()
    End Sub

Open in new window

0
Comment
Question by:Mike Eghtebas
6 Comments
 
LVL 75

Assisted Solution

by:käµfm³d 👽
käµfm³d   👽 earned 200 total points
ID: 40542821
You can't really print an empty list. I'm wondering if you're misunderstanding how a list works...

Let's say I have a book bag. It's desingned to do what? Carry books, right? Well when I buy it from the store, it doesn't come with any books in it, but that doesn't change what it's designed to do, right?

Your list is the same way:  Just declaring it doesn't fill it with anything. You have to actually add items to it in order to be able to use those items later. Just declaring that your list will hold Importance objects doesn't mean that there are any Importance objects in the list. You have to add them. Being that it appears that you are trying to do this programmatically, there isn't really a straigt-forward way to do so. Nevertheless, there are ways to do so. But before I delve into any of those, are you sure you want to stick with the enum as the example?
0
 
LVL 69

Assisted Solution

by:Qlemo
Qlemo earned 50 total points
ID: 40542823
With your DIM you create an empty list which may contain Importance enum values. Of course you need to fill the list first before you can work with it.
0
 
LVL 69

Expert Comment

by:Qlemo
ID: 40542826
kaufmed is thinking the same way as I - the enum itself is no good example. It is like having only prizes, without any article description. If course you can sort them, but what does the result mean then?
0
3 Use Cases for Connected Systems

Our Dev teams are like yours. They’re continually cranking out code for new features/bugs fixes, testing, deploying, testing some more, responding to production monitoring events and more. It’s complex. So, we thought you’d like to see what’s working for us.

 
LVL 33

Accepted Solution

by:
it_saige earned 250 total points
ID: 40542833
Is this what you are after?
Imports System.Linq

Module Module1
	Sub Main()
		Dim value As List(Of Importance) = [Enum].GetValues(GetType(Importance)).Cast(Of Importance)().ToList()
		Dim i As Integer
		For i = 0 To value.Count - 1
			Console.WriteLine("Importance Value = {0}; Importance Name = {1}", i, value(i).ToString())
		Next i

		Console.WriteLine()

		For Each item In value
			Console.WriteLine("Importance Value = " & item & "; Importance Name = {0}", item)
		Next
		Console.ReadLine()
	End Sub
End Module

Enum Importance
	Critical = 4
	Important = 3
	None = 0
	Regular = 2
	Trivial = 1
End Enum

Open in new window

Produces the following output -Capture.JPGBut I want to echo the sentiments of kaufmed and Qlemo.

The benefit of an Enum is that you don't have to use a list item to compare against some variable (if the variable is a type of the enum value).

-saige-
0
 
LVL 33

Author Comment

by:Mike Eghtebas
ID: 40543002
Thanks to all. I had easier solution to print Enum Importance. My goal was to wrap my mind around use of List(Of Type). Kaufmed explanation was helpful.

It_saige's solution brings me to a point where I can see how use of List maybe is not the best use in this case. I will try List(Of Type) with other settings to learn more about it.

Mike
0
 
LVL 33

Author Comment

by:Mike Eghtebas
ID: 40543041
Here is an example of what I was looking for:

Enum Code As Byte
        Small = 0
        Medium = 1
        Large = 2
 End Enum

  Sub Main()
        Enum_Types()     'http://www.dotnetperls.com/enum-vbnet
  End Sub

Private Sub Enum_Types()
        ' The value is represented in a byte.
        Dim value As Code = Code.Medium
        'Console.WriteLine(value)
        For Each i In [Enum].GetValues(GetType(Code))
            Console.WriteLine(i)
        Next
        Console.ReadLine()
    End Sub

Output:
small
medium
large

Open in new window

0

Featured Post

Comprehensive Backup Solutions for Microsoft

Acronis protects the complete Microsoft technology stack: Windows Server, Windows PC, laptop and Surface data; Microsoft business applications; Microsoft Hyper-V; Azure VMs; Microsoft Windows Server 2016; Microsoft Exchange 2016 and SQL Server 2016.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Article by: Kraeven
Introduction Remote Share is a simple remote sharing tool, enabling you to see, add and remove remote or local shares. The application is written in VB.NET targeting the .NET framework 2.0. The source code and the compiled programs have been in…
Creating an analog clock UserControl seems fairly straight forward.  It is, after all, essentially just a circle with several lines in it!  Two common approaches for rendering an analog clock typically involve either manually calculating points with…
Established in 1997, Technology Architects has become one of the most reputable technology solutions companies in the country. TA have been providing businesses with cost effective state-of-the-art solutions and unparalleled service that is designed…
Finds all prime numbers in a range requested and places them in a public primes() array. I've demostrated a template size of 30 (2 * 3 * 5) but larger templates can be built such 210  (2 * 3 * 5 * 7) or 2310  (2 * 3 * 5 * 7 * 11). The larger templa…

770 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question