Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

Use List(of Type)

Posted on 2015-01-10
6
Medium Priority
?
117 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
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
Industry Leaders: 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 34

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

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!

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 …
Introduction As chip makers focus on adding processor cores over increasing clock speed, developers need to utilize the features of modern CPUs.  One of the ways we can do this is by implementing parallel algorithms in our software.   One recent…
This course is ideal for IT System Administrators working with VMware vSphere and its associated products in their company infrastructure. This course teaches you how to install and maintain this virtualization technology to store data, prevent vuln…
This is my first video review of Microsoft Bookings, I will be doing a part two with a bit more information, but wanted to get this out to you folks.
Suggested Courses

670 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