Empty class to use with LINQtoCSV - vb.net - vb.net

I have the following in C# and I'm trying to convert it to vb.net.
internal class MyDataRow : List<DataRowItem>, IDataRow
{
}
The rest of the code I've managed to convert myself it's just the empty class that I'm struggling with. This is so I can read in files with any columns and display them.
Edit:
Looking at the comment given and online I've got to the following:
Class MyDataRow
Inherits List(Of DataRowItem)
Implements IDataRow
End Class
I'm getting an error on the implements section.
This gives the following errors:

If you are just trying to convert this class, you can use below codes:
Friend Class MyDataRow
Inherits List(Of DataRowItem)
Implements IDataRow
End Class
And you have to implement IDataRow interface methods as well

Related

Reorder linq list within a inherited collection in vb .net

I have a class which contains the different zone
Public Class CollectionZone
Inherits System.Collections.Generic.List(Of ClassZone)
' ....
End Class
and the second class :
Public Class ClassZone
Public Property debutXMetres As Double
End Class
I'd like to reorder with that property, I tryed
dim collection as new CollectionZone
'init collection
collection = cType(collection.OrderBy(Function(obj) obj.debutXMetres).ToList(), CollectionZone)
but I have a invalid cast. How can i recover the CollectionZone class ?
Regards
The problem is that you are trying to cast List(Of ClassZone) to CollectionZone which is not possible.
But rather than creating a new collection you can sort the one you have:
collection.Sort(Function(x, y) x.debutXMetres.CompareTo(y.debutXMetres))

How to get the class type in a inherited shared method

Folks;
Code looks like:
Public Class MasterA
Inherits Underling
End Class
Public Class MasterB
Inherits Underling
End Class
Public Mustinherit Class Underling
Sub DoSomething()
Me.GetType 'Using the instance, I can get the class.
end sub
Shared function() as ???? 'How can I define the return type based on the class that inherited me?
'Me.GetType 'Won't work as this is a shared function with no instance 'Me'
End Function
End class
OK. The question is: is there a way to get at the class type from within a shared function that was inherited by another class?
What I'm building is an XML serializer/desrializer as an inheritable class so that classes that inherit it can be serilized to an XML file, and back again. Rather than writing a serializer/deserializer for each type of class I want to do this with, I'd like to just inherit the functionality.
To do that, though, requires that I be able to ascertain the clas that inherited me in the shared function.
You could get the desired behavior with a generic base class, my VB is a little rusty so you might find stray parens or brackets. This would really be the only way to get a type reference to an inheriting class in a shared base class function.
Public Mustinherit Class Underling(Of T)
Sub DoSomething()
Me.GetType 'Using the instance, I can get the class.
end sub
Shared function() As T
' GetType(T) should get the type at this point
End Function
End class
Public Class MasterA
Inherits Underling(Of MasterA)
End Class
Public Class MasterB
Inherits Underling(Of MasterB)
End Class
As a side note it does seem like a rather weird solution to handle XmlSerialization rather than through your own serializer implementation or XmlSerializer

Factory method for generics in VB.NET

I want to create a factory for generic classes in VB.NET and I am running into issues.
What I have are two interfaces:
IPersistentObject and IPManagerBase(Of T as IPersistentObject)
The logic is that for each type of peristent object I have a corresponding manager class handling query logic.
Now I have a base class like this:
public class PManagerBase(Of T as IPersistentObject) Implements IPManagerBase(of T)
So, now in the real world I have a persistent type "PUser" and a corresponding manager declared like this:
public class PUserManager implements PManagerBase(Of PUser)
I have about 100 of those persistent objects and corresponding manager classes.
Now I want to have a factory, which I would invoke like this (removing the details):
MyFactory.CreateManager<PUserManager>()
I am creating my Factory like this
public class MyFactory
public shared function CreateManager(Of T as {PManagerBase(Of IPersistentObject), New}) as T
return new T()
end function
end class
Looks great.
Now I want to invoke it:
Dim myManager = MyFactory.CreateManager<PUserManager>()
What happens?
I get a compile error: "PUserManager does not implement/inherit PManagerBase(Of IPersistentObject)". I get the message in German so this is a free tranlation.
What would I need to change to make this running?
It works if I declare my factory like this:
public class MyFactory
public shared function CreateManager(Of T as {PManagerBase(Of PUser), New}) as T
return new T()
end function
end class
But then the benefit is gone, since it works only for Managers of the PUser object.
A better solution is
public class MyFactory
public shared function CreateManager(Of T as {PManagerBase(Of U), New}, U as IPersistentObject) as T
return new T()
end function
end class
This works, but I have to call my factory method like this now:
Dim myManager = MyFactory.CreateManager<PUserManager, PUser>()
I don't like this since this is redundant and I don't need U at all in the function. In it's declaration PUserManager is tied to PUser.
Is there a better way? Why is PUserManager not inheriting from PManagerBase(Of IPersistentObject)?
This is a problem with generics, if you are using VS 2010 you may want to take a look at covariance and contravariance and modify your IPManagerBase definition accordingly.

How Can I Expose Private Fields using a Partial Class in VB.NET?

This is my first post on Stack Overflow so please exuse (and feel free to point out) any n00b mistakes.
I am trying to implement transactions across multiple TableAdapters in VB.NET (using Visual Studio 2010) by extending the partial class as described in the following examples:
http://blah.winsmarts.com/2006/06/18/the-definitive-tableadapters--transactions-blog-post.aspx
madprops.org/blog/typed-datasets-and-sqltransaction/
stackoverflow.com/questions/2342289/net-tableadapter-to-dataadapter
However, when I attempt to expose any of the private fields created by the designer they are underlined in the editor with the following error:
'_adapter' is not declared. It may be inaccessible due to its protection
level.
Searching this site as well as google has not revealed anything useful, but perhpas I'm searching the wrong keywords.
Here is the code in MyDataset.vb
Partial Public Class MyTableAdapter
Public Property MyTransaction() As SqlTransaction
Get
Return _adapter.SelectCommand.Transaction
End Get
Set(ByVal value As SqlTransaction)
If _adapter Is Nothing Then
InitAdapter()
End If
Connection = value.Connection
_adapter.InsertCommand.Transaction = value
_adapter.UpdateCommand.Transaction = value
_adapter.DeleteCommand.Transaction = value
End Set
End Property
End Class
and here is the designer code:
Partial Public Class MyTableAdapter
Inherits Global.System.ComponentModel.Component
Private WithEvents _adapter As Global.System.Data.SqlClient.SqlDataAdapter
Private _connection As Global.System.Data.SqlClient.SqlConnection
Private _transaction As Global.System.Data.SqlClient.SqlTransaction
Private _commandCollection() As Global.System.Data.SqlClient.SqlCommand
Am I missing something, or is this not possible in VB (all of the examples I've seen are in C#)?
TIA for any help!
JE
Can you access any Public properties/methods from the Adapter class?
I think you may have created your partial class in the wrong namespace. It has to be in Namespace MyDataSetTableAdapters

Type parameters cannot be used as qualifiers

VB.Net2005
Simplified Code:
MustInherit Class InnerBase(Of Inheritor)
End Class
MustInherit Class OuterBase(Of Inheritor)
Class Inner
Inherits InnerBase(Of Inner)
End Class
End Class
Class ChildClass
Inherits OuterBase(Of ChildClass)
End Class
Class ChildClassTwo
Inherits OuterBase(Of ChildClassTwo)
End Class
MustInherit Class CollectionClass(Of _
Inheritor As CollectionClass(Of Inheritor, Member), _
Member As OuterBase(Of Member))
Dim fails As Member.Inner ' Type parameter cannot be used as qualifier
Dim works As New ChildClass.Inner
Dim failsAsExpected As ChildClassTwo.Inner = works ' type conversion failure
End Class
The error message on the "fails" line is in the subject, and "Member.Inner" is highlighted. Incidentally, the same error occurs with trying to call a shared method of OuterBase.
The "works" line works, but there are a dozen (and counting) ChildClass classes in real life.
The "failsAsExpected" line is there to show that, with generics, each ChildClass has its own distinct Inner class.
My question: is there a way to get a variable, in class CollectionClass, defined as type Member.Inner? what's the critical difference that the compiler can't follow?
(I was eventually able to generate an object by creating a dummy object of type param and calling a method defined in OuterBase. Not the cleanest approach.)
Edit 2008/12/2 altered code to make the two "base" classes generic.
Dim succeeds as OuterBase.Inner
.net does not have C++'s combination of template classes and typedefs, which means what you are trying to do is not possible, nor does it even make sense in .net.
ChildClass.Inner and SomeOtherChildClass.Inner are the same type. Here's a short but complete program to demonstrate:
Imports System
MustInherit Class InnerBase
End Class
MustInherit Class OuterBase
Class Inner
Inherits InnerBase
End Class
End Class
Class ChildClass
Inherits OuterBase
End Class
Class OtherChildClass
Inherits OuterBase
End Class
Class Test
Shared Sub Main()
Dim x as new ChildClass.Inner
Dim y as new OtherChildClass.Inner
Console.WriteLine(x.GetType())
Console.WriteLine(y.GetType())
End Sub
End Class
The output is:
OuterBase+Inner
OuterBase+Inner
What were you trying to achieve by using "parameterised" nested classes? I suspect that either it wouldn't work how you'd want it to, or you can achieve it just by using OuterBase.Inner to start with.
Now if each of your child classes were declaring their own nested class, that would be a different situation - and one which generics wouldn't help you with.