-1

For Instance, I created a sub routine defined as follows:

Private Sub PCboPl(pList As List(Of Object), dt As DataTable)

    pList = New List(Of Object)
    dt = oDB.GSD()
    For Each dr In dt.Rows
        pList.Add(New Object(dr))
    Next

    pList = pList.OrderBy(Function(x) x.dName).ToList()
    cboP.ItemsSource = pList 
End Sub

The following causes a Too many arguements to 'Public Overloads Sub New()' error message

pList.Add(New Object(dr))

I suspect this is because I am trying to pass a parameter to a constructor that doesn't exist.

Any help would be appreciated.

gdir
  • 922
  • 2
  • 17
  • 25
oisaac
  • 1
  • 2
  • 1
    dr is already an object... simply pList.Add(dr) – Trevor_G Feb 24 '17 at 19:25
  • @Trevor you should use that as an answer instead of a comment – Matt Feb 24 '17 at 19:26
  • 1
    I would @Matt.. but I'm really not sure that is what he is asking.... – Trevor_G Feb 24 '17 at 19:27
  • This has nothing to do with C#. Please only choose relevant tags in the future. – itsme86 Feb 24 '17 at 19:36
  • @Trevor That wouldn't work because dr is used by the object that I plan on passing accepts dr as a parameter in its constructor. – oisaac Feb 24 '17 at 19:56
  • Then create a new class instead with a constructor that required Dr as a parameter e.g. pList.Add(New YourClassName(dr)) – Trevor_G Feb 24 '17 at 19:58
  • @itsme86 C# and VB both are both in the .Net framework.. There are many times where I find C# solutions and then convert them to VB using http://converter.telerik.com/ .... and vice versa.... So therefore it does relate to C#... for my purpose anyway... – oisaac Feb 24 '17 at 20:00
  • @Trevor I see what you're saying, but wouldn't that defeat the purpose of attempting to use a generic object? – oisaac Feb 24 '17 at 20:01
  • @Trevor I may be confused. If I use your proposed solution, would I still be able to pass three separate objects to the function that require Dr to instantiate them? – oisaac Feb 24 '17 at 20:03
  • THe constructor for your class can have as many parameters are you like. But I'm now kind of lost as to where you want to take this.. your question really isn't very clear where your target is. – Trevor_G Feb 24 '17 at 20:05
  • 1
    I am pretty sure you dont need to take the DataRows out of a DataTable, down cast them to Object and store them in a new collection in order to use an ItemSource/DataSource even in WPF. If you did, `pList = Dt.AsEnumberable().ToList()` is all you need – Ňɏssa Pøngjǣrdenlarp Feb 24 '17 at 20:06
  • Thank you everyone for your efforts by the way! @Trevor Essentially I am using WPF to populate a combobox, in which it is relying on two fields in the dataset, which is detected by SelectedValuePath and DisplayMemberPath – oisaac Feb 24 '17 at 20:19
  • @Plutonix I essentially inherited this code and am trying to make it more efficient in a sense. There are "wrapper" classes that rely on Dr, in which the data in Dr populates the attributes in the class, which are then added to a list that serves as the datasource for the combobox. I like your idea, I am going to try it and see if it works – oisaac Feb 24 '17 at 20:19
  • All you need is the DataTable. No extra processing required. – Ňɏssa Pøngjǣrdenlarp Feb 24 '17 at 20:23
  • @Plutonix that isn't going to work. The wrapper class that accepts dr in its constructor is necessary in my solution. I am afraid that there isn't an answer for what I am trying to accomplish. Thanks again. – oisaac Feb 24 '17 at 20:28
  • @Plutonix I agree to a certain extent, the pList object contains logic that is necessary to the application. So the dr is required. Unless I rewrite the application – oisaac Feb 24 '17 at 20:30
  • What constructor? The Q just shows a common, ordinary (void) method. The dr used there is a local loop iterator and is not able to be used elsewhere. – Ňɏssa Pøngjǣrdenlarp Feb 24 '17 at 20:32
  • @oissac F# is a .Net language too. The tags are about what's associated with the question. Since this question is about VB.Net, it shouldn't have the C# tag. The reason is because this site serves society as a whole. Someone stumbling on this question when they're looking for C# material is a waste of their time. – itsme86 Feb 24 '17 at 20:56

2 Answers2

0

If I get you right, your pList has to be declared as a List(Of Object) per legacy code requeriments, but it actually is a list of another type (the "wrapper" class you mentioned in comments, but nowhere on the question) which receives a DataRow as a parameter in your constructor.

If that's the case, you just have to add a new instance of that wrapper class to your list.

pList.Add(New Wrapper(dr))

Now, be careful with this:

pList = pList.OrderBy(Function(x) x.dName).ToList()

x is object, and Object type doesn't have a dName property. Visual Basic allows you to do this kind of... "dirty" stuff, but for the sake of clarity you should at least CType your x variable to the Wrapper type.

pList = pList.OrderBy(Function(x) CType(x,Wrapper).dName).ToList()
Josh Part
  • 2,154
  • 12
  • 15
  • I apologize for not spelling out all the specifics in the original post. I can see that I caused confusion. This is my first ever question asked on Stack Overflow, so please bare with me. – oisaac Feb 28 '17 at 15:22
  • The caveat that I did not mention is that there are three possible wrapper classes, that utilizes the same Dr – oisaac Feb 28 '17 at 15:23
  • Being a list of `Object`, `pList` can have instances of any class. You just have to do a type check every time you use its elements. – Josh Part Mar 01 '17 at 15:37
0

The answer to my question ultimately was found in this link

Create instance of generic type?

I converted the code to VB.Net

Instead of using a generic object in my Sub Routine definition, I used a generic type and was able to instantiate a generic type, while passing a parameter to it's constructor.

Community
  • 1
  • 1
oisaac
  • 1
  • 2