Solved

Use List(of Type)

Posted on 2015-01-10
6
106 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 68

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 68

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
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 
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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
WPF - Tooltips for ComboBox items 5 40
C# Single Form 8 29
Ajax Panel Not Updating When Triggered By Timer 4 46
Regex validation 2 0
This tutorial demonstrates one way to create an application that runs without any Forms but still has a GUI presence via an Icon in the System Tray. The magic lies in Inheriting from the ApplicationContext Class and passing that to Application.Ru…
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…
Along with being a a promotional video for my three-day Annielytics Dashboard Seminor, this Micro Tutorial is an intro to Google Analytics API data.
Many functions in Excel can make decisions. The most simple of these is the IF function: it returns a value depending on whether a condition you describe is true or false. Once you get the hang of using the IF function, you will find it easier to us…

864 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

Need Help in Real-Time?

Connect with top rated Experts

19 Experts available now in Live!

Get 1:1 Help Now