Solved

Use List(of Type)

Posted on 2015-01-10
6
104 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 74

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
How to improve team productivity

Quip adds documents, spreadsheets, and tasklists to your Slack experience
- Elevate ideas to Quip docs
- Share Quip docs in Slack
- Get notified of changes to your docs
- Available on iOS/Android/Desktop/Web
- Online/Offline

 
LVL 32

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

How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

Join & Write a Comment

Suggested Solutions

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…
If you need to start windows update installation remotely or as a scheduled task you will find this very helpful.
It is a freely distributed piece of software for such tasks as photo retouching, image composition and image authoring. It works on many operating systems, in many languages.
Sending a Secure fax is easy with eFax Corporate (http://www.enterprise.efax.com). First, Just open a new email message.  In the To field, type your recipient's fax number @efaxsend.com. You can even send a secure international fax — just include t…

743 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

8 Experts available now in Live!

Get 1:1 Help Now