-
Mar 22nd, 2017, 03:05 PM
#1
Thread Starter
Lively Member
Substituting structures for classes
I want to create a bit more complicated class, but I am not sure how to do this. Here is an example of what I want:
Code:
Dim foo As StructSubstitute
Class StructSubstitute
ReadOnly Property Main As MainStruc
ReadOnly Property Secondary As SeconStruc
ReadOnly Property Name As String
Class MainStruc
ReadOnly Property Data As Byte()
ReadOnly Property List As New List(Of String)
End Class
Class SeconStruc
ReadOnly Property First As Integer
ReadOnly Property Second As Integer
End Class
End Class
Sub Set()
Dim foo = New StructSubstitute
foo.Name = "bar"
foo.Main.List.Add("a")
foo.Main.List.Add("b")
foo.Main.Data = New Byte() {1, 2, 3}
foo.Secondary.First = 1234
End Sub
Sub Read()
Debug.Print(foo.Name) ' Can read value
Try
foo.Name = "new" ' But cannot change value
Catch
End Try
End Sub
1. I want to be able to assign values immediately after "Dim foo = New StructSubstitute".
2. I want to be able to assign/modify values within Set function, but only read in all other functions.
Is it doable and how?
-
Mar 22nd, 2017, 03:25 PM
#2
New Member
Re: Substituting structures for classes
I'm not exactly sure what your looking to do, but if you're looking for intellisense, try namespace: https://msdn.microsoft.com/en-us/lib...v=VS.100).aspx
-
Mar 22nd, 2017, 03:31 PM
#3
Thread Starter
Lively Member
Re: Substituting structures for classes
I don't see how namespaces solves my issues.
I am trying to make the above code to work without errors and I have listed two requirements.
-
Mar 22nd, 2017, 03:34 PM
#4
New Member
Re: Substituting structures for classes
-
Mar 22nd, 2017, 03:45 PM
#5
Thread Starter
Lively Member
Re: Substituting structures for classes
I know how to build a basic structure and basic class, I also know that structure won't work with my given example as it is not a reference type, hence adding values to the list won't be possible directly, e.g. foo.Main.List.Add("a"). You can copy/paste the code and run it to see the errors I am talking about. If you know how to fix them, please let me know.
-
Mar 22nd, 2017, 03:56 PM
#6
New Member
Re: Substituting structures for classes
You can just remove the ReadOnly statement from the declarations, or you can create a property with a get and set in order for your get to be read only (does that even make sense? don't care)
How to: Create a Property (Visual Basic)
-
Mar 22nd, 2017, 03:59 PM
#7
Re: Substituting structures for classes
You are right to be using classes for this. What isn't clear to me is why you want the nested classes you are showing. When I use a nested class, it's because the outer class will be holding a collection of the nested class, and there is no reason for the nested class to be exposed outside the outer class, or even known to the user of the outer class.
Since there are things such as structures, which are different from classes, as you know, I'm not thrilled with using the word Struct as part of the name of something that is really a class. However, you have to create the objects to be able to use them. You could either add a constructor to the outer class, or you could instantiate the two inner class instances directly. Back in the day, you couldn't write a ReadOnly property in that simplified method that you have used. That was only added in something like 2015. Prior to that, you would have written it like this:
Code:
Private mMainStruc as New MainStruc
ReadOnly Property Main As MainStruc
get
Return mMainStruc
End Get
End Property
There's probably an easier way to instantiate the object on the fly these days, rather than writing it out as I have done, but I've been late to using simplified properties, and tend to write them out. However you do it, you have to cause the inner classes to be created (Sub New) when you create the outer class. You are already doing something like this when you created the List in MainStruc.
My usual boring signature: Nothing
-
Mar 22nd, 2017, 05:01 PM
#8
Re: Substituting structures for classes
I feel like you can probably write this and get away with it in modern VB versions:
Code:
Public ReadOnly Property Main As New MainStruc
I'm not sure if it was 2012 or 2015 where initializer syntax appeared, but I'm pretty sure it's there.
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|