Results 1 to 10 of 10

Thread: [2005] Really Basic Question [Location Property]

  1. #1

    Thread Starter
    Frenzied Member
    Join Date
    Mar 2006
    Location
    Pennsylvania
    Posts
    1,069

    Red face [2005] Really Basic Question [Location Property]

    I feel like such a noob asking this

    Suppose I am in an event handler of a form and type this:

    Code:
    Me.Location.X = 5;
    I get the error:

    HTML Code:
    Cannot modify the return value of 'System.Windows.Forms.Form.Location' because it is not a variable
    For a while now, whenever I wanted to change the Location property of an object, I would just instantiate a new Point. However, if I am changing the location 1,000 times a second, such as in a game, wouldn't that be considered bad practice?

    Surely there is a way to just change the X value to something else without instantiating a new Point o-O

  2. #2
    Raging swede Atheist's Avatar
    Join Date
    Aug 2005
    Location
    Sweden
    Posts
    8,018

    Re: [2005] Really Basic Question [Location Property]

    You could use the Top/Left properties

    EDIT: Or keep a point object declared at class level. Modify its value and assign it to the Location property every time you need to move the form.

    VB.Net Code:
    1. Public Class Form1
    2.     Private formLocation As Point
    3.     Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
    4.         formLocation = Me.Location
    5.     End Sub
    6.  
    7.     Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
    8.         formLocation.X += 1
    9.         formLocation.Y += 1
    10.         Me.Location = formLocation
    11.     End Sub
    12. End Class
    Last edited by Atheist; Nov 29th, 2007 at 08:53 PM.
    Rate posts that helped you. I do not reply to PM's with coding questions.
    How to Get Your Questions Answered
    Current project: tunaOS
    Me on.. BitBucket, Google Code, Github (pretty empty)

  3. #3

    Thread Starter
    Frenzied Member
    Join Date
    Mar 2006
    Location
    Pennsylvania
    Posts
    1,069

    Re: [2005] Really Basic Question [Location Property]

    Yes, exactly! I help out the VB students in during my lunch period at school, and I tell them to use the method with the class level point and assigning the point to the location. They basically always ask, "why can't we just assign to the point value directly?"

    That's kind of weird of VB to do that..

  4. #4
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,221

    Re: [2005] Really Basic Question [Location Property]

    No it's not weird at all. Here's why. The Location property is type Point. Point is a structure, so it's a value type. When you get the Location property you are getting a copy of the Point value stored in the control. If you set the X property of that copy that will have no effect on the original Point value and therefore no effect on the control's location. That's why the Top and Left properties exist: so you can set them and then internally they will create a new value and assign it to the relevant variable internally.

    Now, if Point was a class this would not be the case. The Location property would return a reference to the same Point object so setting its X property directly would be fine. The moral of the story is this: if a property returns a value, rather than a reference, then setting a property of that value will not affect the original. The copy is immediately discarded so any change you make to it is useless so the compiler doesn't even let you try.

    If you want to set one component of the Location only you should be using the Top and Left properties. If you want to set both you should be creating a Point and setting the Location. If you want to set the Location and Size you should using the Bounds property or the SetBounds method.
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

  5. #5

    Thread Starter
    Frenzied Member
    Join Date
    Mar 2006
    Location
    Pennsylvania
    Posts
    1,069

    Re: [2005] Really Basic Question [Location Property]

    Thanks

    What of the Vector3 struct then? It doesn't have those Bounds methods and properties. Should I just make a new point each time? I suppose.

    Yet again, thanks. I'd rep you but I need to rep someone else first.. as usual..

  6. #6
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,221

    Re: [2005] Really Basic Question [Location Property]

    It's just a general rule that you follow in all cases. If you have objectA and you want to set objectA.PropertyA.PropertyB then the type of objectA and PropertyB are irrelevant. The only thing that matters is whether PropertyA is a value type or a reference type. If it's a value type then setting PropertyB directly is not allowed. You'd have to get PropertyA's value into a local variable, set PropertyB on that variable, then assign the variable back to PropertyA.
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

  7. #7
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,221

    Re: [2005] Really Basic Question [Location Property]

    To illustrate that last post with code, Let's say that SomeType.SomeProperty is a reference type. In that case this is legal:
    vb.net Code:
    1. Dom obj As New SomeType
    2.  
    3. obj.SomeProperty.SomeOtherProperty = someValue
    If SomeProperty is value type though, that is not allowed and the compiler will tell you so. In that case you'd need to do this:
    vb.net Code:
    1. Dom obj As New SomeType
    2.  
    3. Dim propVal As SomeValueType = obj.SomeProperty
    4.  
    5. propVal.SomeOtherProperty = someValue
    6. obj.SomeProperty = propVal
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

  8. #8
    Hyperactive Member
    Join Date
    Feb 2006
    Location
    UK
    Posts
    285

    Re: [2005] Really Basic Question [Location Property]

    (In a complete aside) where are you boys getting that VB.Net code tag? I can only see this tag
    Code:
    Code
    .

    Go on, give it up!

  9. #9
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,221

    Re: [2005] Really Basic Question [Location Property]

    The VBCode button adds a [HIGHLIGHT=vb] tag. If you change that to [HIGHLIGHT=vb.net] you'll get that effect. You can also use =vb, =C#, =CSharp, =php and various other languages. I'm not sure of the full list.
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

  10. #10
    Hyperactive Member
    Join Date
    Feb 2006
    Location
    UK
    Posts
    285

    Re: [2005] Really Basic Question [Location Property]

    vb Code:
    1. Dim s As String = rtbLetter.Rtf
    2. Dim i As Int32 = s.IndexOf("\", 3)
    3. s = s.Substring(i)
    Humph! It worked a treat. Thanks!

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width