question

SougataGhosh-7881 avatar image
0 Votes"
SougataGhosh-7881 asked XingyuZhao-MSFT edited

Assigning Values toproperties in Structures

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


dotnet-visual-basic
5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

XingyuZhao-MSFT avatar image
1 Vote"
XingyuZhao-MSFT answered XingyuZhao-MSFT edited

Hi @SougataGhosh-7881 ,
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.


5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

XingyuZhao-MSFT avatar image
0 Votes"
XingyuZhao-MSFT answered SougataGhosh-7881 commented

Hi @SougataGhosh-7881 ,
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.


· 2
5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

Could u pls elaborate as to what you mean when you say that a structure is a "temporary variable"?

0 Votes 0 ·

Hi...Anybody else with any other views? Stuck with this for quite some time now...

0 Votes 0 ·
SougataGhosh-7881 avatar image
0 Votes"
SougataGhosh-7881 answered

Hi @XingyuZhao-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????

5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.