Share via

Assigning Values toproperties in Structures

Sougata Ghosh 161 Reputation points
2021-04-19T17:33:48.44+00:00

In the below code "f" is an instance of the Class FORM which has a property "s" of type SIZE, a structure which has been defined in the code. My question is: When I try to assign values to the attributes of property "s" of the instance "t" directly it does not work: That is the statement f.s.height = 15 does not work. My confusion is arising from the fact that when I print the values of the property "s" of the instance "f", I am able to print the individual attributes of the structure SIZE but the same cannot be done while assignment of value. Assignment of values require me to call the constructor. Why is it so? What is preventing the assignment of the value to the attributes of "s": i.e. f.s.height & f.s.width?

Module Module1
    Sub Main()
        Dim f As New MyForm()
        f.s = New Size(2, 5) 'Works Fine
        f.colour = "Red"  'Assignment works just fine

        'Below: Individual elements cannot be acceessed for assignment. WHY?
        f.s.height = 15   'Doesn't Work
        f.s.height = +2   'Doesn't work

        'Individual elements can be accessed while printing
        Console.WriteLine("Widht = {0}", f.s.width)
        Console.WriteLine("Height = {0}", f.s.height)

        Console.ReadLine()
    End Sub
End Module
Class MyForm
    Public Property s As Size
    Public Property colour As String
End Class

Public Structure Size
    Dim height As Integer
    Dim width As Integer
    Public Sub New(ByVal w As Integer, ByVal h As Integer)
        width = w
        height = h
    End Sub
End Structure
Developer technologies | VB
0 comments No comments

Answer accepted by question author

Xingyu Zhao-MSFT 5,381 Reputation points
2021-04-21T02:17:12.977+00:00

Hi @Sougata Ghosh ,
Thanks for your feedback.

But i dont understand the meaning of this.

See : Mutating Readonly Structs
The code in VB. NET is

Structure Mutable  
    Private x As Integer  
    Public Function Mutate() As Integer  
        x = x + 1  
        Return x  
    End Function  
End Structure  
Module Module1  
    Public ReadOnly m As Mutable = New Mutable()  
    Sub Main()  
        Console.WriteLine(m.Mutate())  ' 1  
        Console.WriteLine(m.Mutate())  ' 1  
        Console.WriteLine(m.Mutate())  ' 1  
        Console.ReadLine()  
    End Sub  
End Module  

As the blog points out : accessing a value type gives you a COPY of the value. You get a copy of whatever is presently stored in m. m is immutable, but the copy is not. The copy is then mutated, and the value of x in the copy is returned. But m remains untouched.

Here's another reference you may need:
Why are mutable structs “evil”?
The suggestions in the reference also apply to VB. NET.

Hope them could be helpful.

Best Regards,
Xingyu Zhao
*
If the answer is helpful, please click "Accept Answer" and upvote it.
Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread.

Was this answer helpful?

1 person found this answer helpful.
0 comments No comments

2 additional answers

Sort by: Most helpful
  1. Sougata Ghosh 161 Reputation points
    2021-04-20T05:47:16.333+00:00

    Hi @Xingyu Zhao-MSFT

    Thank you for the workaround. But my question was why are we not able to set the attributes of the structure directly when we try to access it through the property of the class which is defined as the structure type. And the reason I want to understand this is because this is the situation when I use the SIZE property of the FORM class to define its size. When altering the height and width of the form using the SIZE property I need to call the constructor. I would not be allowed to individually set the attributes, i.e. I cant write form.size.height = 500 as i cant write here f.s.height = 15.

    In one MSDN documentations the reason mentioned for not being able to was:

    The Size property returns a Size structure, which is a value type. You cannot assign a new value to the property of a value type

    But i dont understand the meaning of this. WHY does a structure being of value type prevent me from setting the values of its attributes directly through an instance of the class????

    Was this answer helpful?

    0 comments No comments

  2. Xingyu Zhao-MSFT 5,381 Reputation points
    2021-04-20T03:22:55.893+00:00

    Hi @Sougata Ghosh ,
    Since the Structure is only a temporary variable, the solution is to create a new 'Structure' of the type and assign it internal variables and then assign that Structure to the Structure property of the class.
    Use the following code to correct the error:

            Dim h As Size  
            h = f.s  
            h.height = 15  
            f.s = h  
    

    So the whole code looks like:

        Sub Main()  
            Dim f As New MyForm()  
            f.s = New Size(2, 5)  
            f.colour = "Red"  
      
            Dim h As Size  
            h = f.s  
            h.height = 15  
            f.s = h  
       
            Console.WriteLine("Widht = {0}", f.s.width)  
            Console.WriteLine("Height = {0}", f.s.height)  
            Console.ReadLine()  
        End Sub  
    

    Hope it could be helpful.

    Best Regards,
    Xingyu Zhao
    *
    If the answer is helpful, please click "Accept Answer" and upvote it.
    Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread.

    Was this answer helpful?


Your answer

Answers can be marked as 'Accepted' by the question author and 'Recommended' by moderators, which helps users know the answer solved the author's problem.