admin管理员组

文章数量:1201147

I am new to realm and can't figure out how to open and read from database. I tried using provided demo database from realm:

Dim r1 as Realm
Dim config As RealmConfiguration

config = New RealmConfiguration("demo-v24.realm")
config.Schema = New Type() {GetType(RealmTestClass0), GetType(RealmTestClass1), GetType(RealmTestClass2)}
config.SchemaVersion = 1
config.IsDynamic = True
'open database
r1 = Realm.GetInstance(config)   
'read from database
Dim people As String = r1.All(Of RealmTestClass0)().Where(Function(p) p.IntegerValue > 0 And p.IntegerValue < 100000).ToString

I used this class definition:

 Public Class RealmTestClass1
     Inherits RealmObject

   ...

     <MapTo("arrayReference")>
     Public ReadOnly Property ArrayReference As IList(Of RealmTestClass0)
         Get
           Return Nothing 'GetList(Of RealmTestClass0)("arrayReference")  // does not work!
         End Get
     End Property
 End Class

(of course I have also a RealmTestClass0 definition)

Problem: I was not able to convert the class definition from c# to vb correctly. C#:

  public class RealmTestClass1 : RealmObject
    {
    ...
        [MapTo("arrayReference")]
        public IList<RealmTestClass0> ArrayReference { get; }
    }

Error Message: Realms.Exceptions.RealmMigrationNeededException: "Migration is required due to the following errors:

  • Property 'RealmTestClass1.arrayReference' has been removed."

Question 1: How can I translate the IList reference object request correct into vb?

Question 2: Is there an easier way to open and read from realm file? (using existing schema from realm file without knowing it before?)

I tried different class definition like

'<MapTo("arrayReference")>
'Public Property ArrayReference As RealmTestClass0()

but it doesn't work.

I am new to realm and can't figure out how to open and read from database. I tried using provided demo database from realm:

Dim r1 as Realm
Dim config As RealmConfiguration

config = New RealmConfiguration("demo-v24.realm")
config.Schema = New Type() {GetType(RealmTestClass0), GetType(RealmTestClass1), GetType(RealmTestClass2)}
config.SchemaVersion = 1
config.IsDynamic = True
'open database
r1 = Realm.GetInstance(config)   
'read from database
Dim people As String = r1.All(Of RealmTestClass0)().Where(Function(p) p.IntegerValue > 0 And p.IntegerValue < 100000).ToString

I used this class definition:

 Public Class RealmTestClass1
     Inherits RealmObject

   ...

     <MapTo("arrayReference")>
     Public ReadOnly Property ArrayReference As IList(Of RealmTestClass0)
         Get
           Return Nothing 'GetList(Of RealmTestClass0)("arrayReference")  // does not work!
         End Get
     End Property
 End Class

(of course I have also a RealmTestClass0 definition)

Problem: I was not able to convert the class definition from c# to vb.net correctly. C#:

  public class RealmTestClass1 : RealmObject
    {
    ...
        [MapTo("arrayReference")]
        public IList<RealmTestClass0> ArrayReference { get; }
    }

Error Message: Realms.Exceptions.RealmMigrationNeededException: "Migration is required due to the following errors:

  • Property 'RealmTestClass1.arrayReference' has been removed."

Question 1: How can I translate the IList reference object request correct into vb.net?

Question 2: Is there an easier way to open and read from realm file? (using existing schema from realm file without knowing it before?)

I tried different class definition like

'<MapTo("arrayReference")>
'Public Property ArrayReference As RealmTestClass0()

but it doesn't work.

Share Improve this question asked Jan 22 at 14:07 Hawk78Hawk78 1
Add a comment  | 

1 Answer 1

Reset to default 0

Question 1: Translating the IList Reference Object in VB.NET You need to use the IList interface in VB.NET just like in C#. Your issue with ArrayReference is likely due to it being a read-only property without an initializer. Here's how you can define it in VB.NET:

Public Class RealmTestClass1
    Inherits RealmObject

    <MapTo("arrayReference")>
    Public ReadOnly Property ArrayReference As IList(Of RealmTestClass0)
        Get
            Return GetList(Of RealmTestClass0)("arrayReference")
        End Get
    End Property
End Class

In this code, GetList(Of RealmTestClass0)("arrayReference") is used to fetch the list of RealmTestClass0 objects.

Question 2: Opening and Reading from a Realm File To open and read from a Realm file using an existing schema, you can simplify the process. Here's an example:

Imports Realms

Module Module1
    Sub Main()
        Dim config As New RealmConfiguration("demo-v24.realm")
        config.SchemaVersion = 1

        ' Open the Realm
        Dim realm = Realm.GetInstance(config)

        ' Read from the Realm
        Dim people = realm.All(Of RealmTestClass0)().Where(Function(p) p.IntegerValue > 0 And p.IntegerValue < 100000)
        For Each person In people
            Console.WriteLine(person.ToString())
        Next
    End Sub
End Module

Public Class RealmTestClass0
    Inherits RealmObject

    Public Property IntegerValue As Integer
End Class

Public Class RealmTestClass1
    Inherits RealmObject

    <MapTo("arrayReference")>
    Public ReadOnly Property ArrayReference As IList(Of RealmTestClass0)
        Get
            Return GetList(Of RealmTestClass0)("arrayReference")
        End Get
    End Property
End Class

In this example, RealmConfiguration is used to configure the Realm instance with the specified schema and version. The GetInstance method opens the Realm, and All retrieves the objects.

本文标签: how can i open realm database in vbnet using correct schemaStack Overflow