Results 1 to 5 of 5

Thread: Need a solution for this

  1. #1

    Thread Starter
    Lively Member
    Join Date
    Aug 2008
    Location
    Palm Desert, CA
    Posts
    91

    Need a solution for this

    Hello, I am adding a new object to a list of objects like this.

    Locations.Add(New Location)


    How can I add the new object while its structure is filled out.
    Here is the class

    Public Class Location
    Public ID As Integer
    Public LName As String
    Public LDesc As String
    End Class

    How can I have a Location filled out and added to a list instead of just creting a blank object.
    Last edited by kbrizs; Oct 29th, 2008 at 09:43 PM.

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

    Re: Need a solution for this

    I have to say, I really don't think you've thought about this very hard.
    vb.net Code:
    1. Dim loc As New Location
    2.  
    3. loc.ID = id
    4. loc.LName = lName
    5. loc.LDesc = ldesc
    6.  
    7. Locations.Add(loc)
    You've almost certainly done the very same same thing in the past. It's just logical: create the object, populate its members, then add it to the list.

    A couple more points to make.

    1. That type should be a structure, not a class, unless you have some specific reason you need it to be a reference type.

    2. Class or structure, if you'd used properties instead of fields then you could have used initialiser syntax:
    vb.net Code:
    1. Locations.Add(New Location With {.ID = id, .LName = lName, .LDesc = lDesc})
    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

  3. #3

    Thread Starter
    Lively Member
    Join Date
    Aug 2008
    Location
    Palm Desert, CA
    Posts
    91

    Re: Need a solution for this

    OK I have this solved now. I am uploading the source so it's easier to look at what I need help with.

    In the program, I have the back forward and add running. I would like to implement an autonumber feature. right now, the location class has an ID field but its not being used. how can I implement an id field. here is what i am looking for.

    It needs to auto increment. It has to skip ids that are already in the list.

  4. #4

    Thread Starter
    Lively Member
    Join Date
    Aug 2008
    Location
    Palm Desert, CA
    Posts
    91

    Re: Need a solution for this

    I am having trouble with attachements. here I go again ...
    Attached Files Attached Files

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

    Re: Need a solution for this

    This new questions has nothing to do with the old so it should have been asked in a new thread. One thread per topic and one topic per thread please.

    Are you storing the data in a database? If so then you should let the database generate the IDs. Pretty much all databases provide that functionality, e.g. SQL Server allows you to make a column an identity, which will generate sequential IDs as data is inserted.
    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

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