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
Solved

Copying an element from List (of T0

Posted on 2011-09-19
14
167 Views
Last Modified: 2012-05-12
I have a dataset that is defined as List (of DC_LocationProfile)

When I get this dataset back I want to step through it and when I find the element that I want I want to copy it to List (Of NewLocations)

Can I copy from one List (of T) to another List (of T)

For Each element In DCLoc -- This is a List (OF T)
            If element.ProfileQuestionID = "126" Then
              *** I want to copy one element from DCLoc to another List (of T)**
            End If

        Next

Can I copy from one List (of T) to another List (of T).  When I try to copy to another List (of T) I am getting a cannot convert to 1-dimensional array error.  

0
Comment
Question by:sherbug1015
  • 6
  • 5
  • 3
14 Comments
 
LVL 85

Expert Comment

by:Mike Tomlinson
ID: 36563173
Sure...but how do you convert from a DC_LocationProfile to a NewLocations?
0
 

Author Comment

by:sherbug1015
ID: 36563731
I have tried

If....Then
   DCLoc.CopyTo(NewLocation)


This is where I am getting the converting to 1-dimensional array error.  
0
 
LVL 85

Expert Comment

by:Mike Tomlinson
ID: 36563816
To add to a list, just use the Add() method:

    DCLoc2.Add(element)

*But, you've said that the first list contains type 'DC_LocationProfile' while the second list contains type 'NewLocations'.

...so you'd need to convert one type to the other somehow.
0
Free Tool: Port Scanner

Check which ports are open to the outside world. Helps make sure that your firewall rules are working as intended.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

 

Author Comment

by:sherbug1015
ID: 36563959
Any idea of how to convert from one type to the other.  I need the second type because I only want one element in there.  The original type I must leave intact.  

0
 
LVL 40
ID: 36563961
Without the real declarations, it's always hard to tell, but let's start for the following. Not counting that you have NewLocation (singular) and NewLocations (plural). Is it a typo or are those 2 different classes? It is always better to copy and paste real code than giving code that "shows the concept". Code that shows the concept is almost never right :-)

Here are a few pointers to guide you.

From what you say, one list is a List (of DC_LocationProfile) while the other is a List (Of NewLocations). You won't be able to copy an element from one list to the other, because they are not of the same type.

DCLoc.CopyTo copies the whole list, not a single element. If you want to copy one element, do not use CopyTo. Get the element and Add it to the other list, something such as: List2.Add(List1(23)). The T in each list should be or the same type for this to work, unless you have some way to convert between the types.
0
 
LVL 85

Expert Comment

by:Mike Tomlinson
ID: 36563974
What do the two types have in common?...we'd need to see those classes if you want a better answer.
0
 

Author Comment

by:sherbug1015
ID: 36567910
What I am trying to do is create a unit test that has only one location in it.  I must first call the method that fills the List (of  DC_LocationProfile).  There are 106 records in this List.  Now that I have the List I want to just pull out one element and put it into another List (of DC_LocationsNew) so that I can pass that list to ProcessProfile to get a return value back.   testmethod.txt ProcessProfile.txt ProcessProfile.txt CopyToDataType.txt CopyFromDataType.txt
0
 
LVL 40
ID: 36568034
If you want a unit test to test for only one location, why pass the parameter as a list? Why not pass only the one object?

Then, as far as I can see, you do not seem to make any modification to the object in ProcessProfile. Why pass a copy? Why not pass only a reference. That would make things a lot easier, because you would not have to write a copy method.
0
 

Author Comment

by:sherbug1015
ID: 36568345
OK.  If you were trying to pull one element out of List(Of T) and pass it as one object, then how would you do it.  
0
 
LVL 40

Accepted Solution

by:
Jacques Bourgeois (James Burger) earned 500 total points
ID: 36568528
Forget part of the last one, while revising your code, I grasped a few things I had not seen the last time. I understand that ProcessProfile is not always called with a single element.

But the following still holds if I am not misled: you do not seem to make any modification to the object in ProcessProfile. Why pass a copy? Why not pass only a reference.

In textmethod.txt, simply replace your ****Here I want to copy this one element to DC_LocationsNew (DCLoc1) by DLoc1.Add(element).

Sometimes, specially when you work on somebody else code, the obvious does not jump at your face.
0
 

Author Comment

by:sherbug1015
ID: 36569230
I have already tried that.  I get an error that one type cannot be converted to another.

I found another way around this.

Thanks anyway.
0
 
LVL 85

Expert Comment

by:Mike Tomlinson
ID: 36569236
"I get an error that one type cannot be converted to another. "

Well, yeah...we tried to get you to post your data type declarations so we could help you with that!
0
 

Author Comment

by:sherbug1015
ID: 36569247
Idle Mind --- I did post my data type declarations.  
0
 
LVL 85

Expert Comment

by:Mike Tomlinson
ID: 36569311
I see that now...somehow I missed that post.
0

Featured Post

Free Tool: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
VS.net 2010 11 48
Run a batch file when a Crystal Report is opened 5 49
Crystal reports vb.net 2 54
No Data for DropDown List 2 28
A while ago, I was working on a Windows Forms application and I needed a special label control with reflection (glass) effect to show some titles in a stylish way. I've always enjoyed working with graphics, but it's never too clever to re-invent …
Introduction When many people think of the WebBrowser (http://msdn.microsoft.com/en-us/library/2te2y1x6%28v=VS.85%29.aspx) control, they immediately think of a control which allows the viewing and navigation of web pages. While this is true, it's a…
With Secure Portal Encryption, the recipient is sent a link to their email address directing them to the email laundry delivery page. From there, the recipient will be required to enter a user name and password to enter the page. Once the recipient …
Finds all prime numbers in a range requested and places them in a public primes() array. I've demostrated a template size of 30 (2 * 3 * 5) but larger templates can be built such 210  (2 * 3 * 5 * 7) or 2310  (2 * 3 * 5 * 7 * 11). The larger templa…

840 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