Assign a struct variable through a property

I have the following

Public Structure Foo
   dim i as integer
End Structure

Public Class Bar

Public Property MyFoo as Foo
Get
   return Foo
End Get
Set(ByVal value as Foo)
   foo  = value
End Set

dim foo as Foo    
End Class

Public Class Other

   Public Sub SomeFunc()    
     dim B as New Bar()    
     B.MyFoo = new Foo()    
     B.MyFoo.i = 14 'Expression is a value and therefore cannot be the target of an assignment ???    
   End Sub
End Class

My question is: why can't I assign through my property in class Bar? What did I do wrong?

+3
source share
2 answers

The answer is found here , it says the following:

' Assume this code runs inside Form1.
Dim exitButton As New System.Windows.Forms.Button()
exitButton.Text = "Exit this form"
exitButton.Location.X = 140
' The preceding line is an ERROR because of no storage for Location.

, , . , . , . , .

, struct . , , , struct .

+3

Dim b as New Bar()
Dim newFoo As New Foo()
newFoo.i = 14
b.MyFoo = newFoo

.

#

class Program
{
    public void Main()
    {
        Bar bar = new Bar();
        bar.foo = new Foo();
        bar.foo.i = 14;
        //You get, Cannot modify the return value of ...bar.foo
        //    because it is not a variable
    }
}
struct Foo
{
    public int i { get; set; }
}

class Bar
{
    public Foo foo { get; set; }
}

, ,

Expression is a value and therefore cannot be the target of an assignment
+1

All Articles