Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

Use List(of Type)

Posted on 2015-01-10
6
Medium Priority
?
120 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 800 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 71

Assisted Solution

by:Qlemo
Qlemo earned 200 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 71

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
Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 35

Accepted Solution

by:
it_saige earned 1000 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 34

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 34

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

[Webinar] Cloud and Mobile-First Strategy

Maybe you’ve fully adopted the cloud since the beginning. Or maybe you started with on-prem resources but are pursuing a “cloud and mobile first” strategy. Getting to that end state has its challenges. Discover how to build out a 100% cloud and mobile IT strategy in this webinar.

Question has a verified solution.

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

I think the Typed DataTable and Typed DataSet are very good options when working with data, but I don't like auto-generated code. First, I create an Abstract Class for my DataTables Common Code.  This class Inherits from DataTable. Also, it can …
If you're writing a .NET application to connect to an Access .mdb database and use pre-existing queries that require parameters, you've come to the right place! Let's say the pre-existing query(qryCust) in Access takes a Date as a parameter and l…
Video by: ITPro.TV
In this episode Don builds upon the troubleshooting techniques by demonstrating how to properly monitor a vSphere deployment to detect problems before they occur. He begins the show using tools found within the vSphere suite as ends the show demonst…
Exchange organizations may use the Journaling Agent of the Transport Service to archive messages going through Exchange. However, if the Transport Service is integrated with some email content management application (such as an anti-spam), the admin…

916 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