Problems with GetType in VB.NET

R

robear

Hi All.

I have been converting a C# to VB.NET Web Project and I have one last
issue to contend with.

I have included relevant code below and an explanation of the error..

Public Function LoadMenuFromFile(ByVal filePath As String) As
MenuDataSet
Dim cacheKey As String = filePath

Dim ds As MenuDataSet = CType(Cache(cacheKey), MenuDataSet)
If ds Is Nothing Then
'bind the menu
Dim serializer As XmlSerializer = New
XmlSerializer(Type.GetType(MenuDataSet))

***** error above line Type.GetType(MenuDataSet) ******

The error is a compiler error stating that MenuDataSet is a type and
cannot be used as an expression.

MenuDataSet is a class as shown below:

Public Class MenuDataSet
Implements ICloneable

Public Sections() As MenuSection


'/ <summary>
'/ Merges the provided data set with this dataset placing the
sections before or after
'/ the existing data.
'/ </summary>
'/ <param name="ds">The data set to merge with.</param>
'/ <param name="opt">The location where to merge the provided
data.</param>
Public Sub Merge(ds As MenuDataSet, opt As MergeLocation)
'check for nulls
If ds.Sections Is Nothing OrElse ds.Sections.Length = 0 Then
Return
End If
'check for nulls
If Sections Is Nothing OrElse Sections.Length = 0 Then
Sections = CType(ds.Sections.Clone(), MenuSection())
Return
End If

'create new array
Dim newItems(Sections.Length + ds.Sections.Length) As
MenuSection
Select Case opt
Case MergeLocation.Before
Array.Copy(ds.Sections, 0, newItems, 0,
ds.Sections.Length)
Array.Copy(Sections, 0, newItems, ds.Sections.Length,
Sections.Length)

Case MergeLocation.After
Array.Copy(Sections, 0, newItems, 0, Sections.Length)
Array.Copy(ds.Sections, 0, newItems, Sections.Length,
ds.Sections.Length)
End Select

Me.Sections = newItems
End Sub 'Merge


'/ <summary>
'/ Copy all data set elements
'/ </summary>
'/ <returns>A copy of this data set</returns>
Public Function Clone() As Object Implements System.ICloneable.Clone
Dim ds As New MenuDataSet
If Not (Sections Is Nothing) Then
ds.Sections = New MenuSection(Sections.Length) {}
Dim i As Integer

While i < Sections.Length
ds.Sections(i) = CType(Sections(i).Clone(), MenuSection)
i += 1
End While
End If
Return ds
End Function 'Clone
End Class 'MenuDataSet


Has anyone got any ideas as to why it wont accept the class for the
GetType area?

Thanks,
Robert
 
A

Armin Zingler

Dim serializer As XmlSerializer = New
XmlSerializer(Type.GetType(MenuDataSet))

***** error above line Type.GetType(MenuDataSet) ******

The error is a compiler error stating that MenuDataSet is a type
and cannot be used as an expression.

MenuDataSet is a class as shown below:


Dim serializer As XmlSerializer = New
XmlSerializer(GetType(MenuDataSet))


GetType is a VB keyword returning a System.Type object representing the
MenuDataSet type.

Armin
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top