Results 1 to 5 of 5

Thread: Variable initialization

  1. #1

    Thread Starter
    PowerPoster gavio's Avatar
    Join Date
    Feb 2006
    Location
    GMT+1
    Posts
    4,462

    Variable initialization

    This is somewhat a theoretical question.

    I had this:
    Code:
    Private _oFields As ListEx(Of FieldStruct) = New List(Of FieldStruct)
    ListEx is my "extension" for List. How can this compile really? It's true that ListEx inherits from List, but still. Should it compile or do I have some wrong settings?

    It took me like two hours to find this...

  2. #2
    PowerPoster techgnome's Avatar
    Join Date
    May 2002
    Posts
    34,687

    Re: Variable initialization

    it should compile... no reason it shouldn't. you're taking something generic (List) and putting it into something more specific (ListEx) ... it'll fit just fine... what you CAN'T do (or more to the point, what you SHOULDN'T do) is put something more specific into something generic.


    Think of it like this - List is like Form... it's generic and somewhat meaningless on its own. ListEx is like Form1 ... a Form that has had some customization to it, tasking it for a specific purpose. It's still a Form under the hood though. Your ListEx is still a List underneath, so what you've done there is perfectly valid. May not be right but it's valid.

    Is there any reason you just didn't do it like this:?
    Code:
    Private _oFields As New ListEx(Of FieldStruct)
    -tg
    * I don't respond to private (PM) requests for help. It's not conducive to the general learning of others.*
    * I also don't respond to friend requests. Save a few bits and don't bother. I'll just end up rejecting anyways.*
    * How to get EFFECTIVE help: The Hitchhiker's Guide to Getting Help at VBF - Removing eels from your hovercraft *
    * How to Use Parameters * Create Disconnected ADO Recordset Clones * Set your VB6 ActiveX Compatibility * Get rid of those pesky VB Line Numbers * I swear I saved my data, where'd it run off to??? *

  3. #3

    Thread Starter
    PowerPoster gavio's Avatar
    Join Date
    Feb 2006
    Location
    GMT+1
    Posts
    4,462

    Re: Variable initialization

    In my case, ListEx still is completely generic. I just like to redefine most of system classes I use in case I need to extend theme later. I KNOW I shouldn't put something specific into something generic, that's why ListEx is only but this more then List:

    Code:
    Public Sub New()
      ' Empty constructor.
    End Sub
    
    Public Sub New(ByVal oFirstItem As T)
      Me.Add(oFirstItem)
    End Sub
    This is for the purpose of creating an object of ListEx "on the fly" (e.g.: for a parameter), where you only need one item quickly.

    I've heard about extensions, but didn't find the time to dig more into theme. Perhaps another time.

    Anyhow, a little bit off topic...

    I still don't think what I've done (and compiler let me) is valid. Type is a type, except in case of interfaces.

    Quote Originally Posted by techgnome View Post

    Is there any reason you just didn't do it like this:?

    Code:
    Private _oFields As New ListEx(Of FieldStruct)
    No reason really. Just a VB6 legacy

  4. #4
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,221

    Re: Variable initialization

    Your code will compile with Option Strict Off but it will fail at run time. With Option Strict On (as it should always be) it will refuse to compile.

    If you derive the ListEx class from the List class then you can assign a ListEx object to a List variable because every ListEx is a List. You can't assign a List object to a ListEx variable though, because not every List is a ListEx. As is always the case with OOP, just look at the real world for examples. Every Dog is an Animal so you can assign a Dog object to an Animal variable but you can;t assign an Animal to a Dog variable because it's not necessarily a Dog.

    Quite frankly, this:
    I just like to redefine most of system classes I use in case I need to extend theme later.
    is silly. If you need to extend them later then extend them later. Why put wings on your car just in case you might want to fly later? Put the wings on if and when you want to fly. A far more sensible approach would be to simply create a helper class, e.g.
    vb.net Code:
    1. Public Class ListHelper
    2.  
    3.     Public Shared Function Create(Of T)(ParamArray items As T()) As List(Of T)
    4.         Dim list As New List(Of T)
    5.  
    6.         list.AddRange(items)
    7.  
    8.         Return list
    9.     End Function
    10.  
    11. End Class
    Now you are still going to be dealing with vanilla List objects.
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

  5. #5

    Re: Variable initialization

    Alternately, a List could be initialized with an array [or it can in VS2010, not sure about VS2008]:

    Code:
    Dim _array() As String = {"dog", "cat", "fish"}
    Dim _list As New List(Of String)(_array)

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width