Results 1 to 3 of 3
Like Tree1Likes
  • 1 Post By jmcilhinney

Thread: losing variables values

  1. #1
    intes is offline VB.NET Forum Newbie
    .NET Framework
    .NET 4.5
    Join Date
    Aug 2014
    Posts
    2
    Reputation
    0

    losing variables values

    Hi,

    I am a begginer in VB.NET. I was doing tutorials for it and in one 'program' two variables (breadth and Height) values are lost when second subroutine is called (even though values are being past to it). I can't figure out why is that happening and how to fix it. Please HELP!
    Attached Images Attached Images

  2. #2
    jmcilhinney's Avatar
    jmcilhinney is offline VB.NET Forum Moderator
    .NET Framework
    .NET 4.0
    Join Date
    Aug 2004
    Location
    Sydney, Australia
    Posts
    13,813
    Reputation
    1707
    Presumably that CalculateAreas method is supposed to be taking `length`, `Breadth` and `height` as input and passing `FloorArea` and `WallArea` as output. In that case, the obvious issue is that, while `FloorArea` is declared ByRef, `WallArea` is not. That means that you'll get the calculated floor area but not the wall area. If you're not using ByRef correctly there then you're most likely not using it correctly elsewhere, e.g. the GetUserInputs method. If that's the case, those values are being lost at the GetUserInputs call, not the CalculateAreas call.

    Do you know what ByVal and ByRef actually do? It's looks like you probably don't. Basically, if a parameter is declared ByVal then the argument is passed by value and assigning a new value to the parameter inside the method will not affect the original variable. Conversely, if a parameter is declared ByRef then the argument is passed by reference and assigning a new value to the parameter inside the method will affect the original variable. Basically, if you want to pass a new object out via a parameter then you declare it ByRef, otherwise you accept the default ByVal.

    That means that all three parameters of your GetUserInputs method must be declared ByRef. I'm guessing that only `Length` is, given that you say that the other two values are "lost". They are all three being used for output so they all need to be ByRef. Similarly, the first three parameters of the CalculateAreas method should be ByVal and the last two should be ByRef. Presumably the first two should be ByVal in CalculateRequirements and the last three should be ByRef.

    I would also urge you to learn right now to always be consistent. An example of this is with the explicit use of ByVal. ByVal is the default way to pass parameters so, in the most recent versions of VB, you have been allowed to omit it. Notice that your BtnEnter_Click method has `sender` and `e` parameters that have not been explicitly declared ByVal or ByRef. That means that they are ByVal. Now look at your CalculateAreas method. You have explicitly declared `length` ByVal but omitted it for `Breadth` and `height`. That's bad. You should either use ByVal explicitly everywhere or nowhere. DO NOT mix and match. If do the same thing in two different ways in different places, people will look at your code and wonder why they are different. They will think that, if you have done thiongs differently, there must be something different about the situations. Confusing people by writing inconsistent code is bad. "People" includes you, because you will often have to go back and read old code and you WILL forget what you were thinking at the time and your code WILL confuse even you if it's not well-written. Trust me on that. I would suggest that you accept what the IDE does by default and omit ByVal in all cases. Doing so actually makes those few cases that are ByRef stand out even more.

    Another example of inconsistency is the fact that you're starting most of your variables and parameters with an upper-case letter and a couple with a lower-case letter. Again, people will look at that code and wonder what's different about those variables that you chose to name them differently. You really should be using a lower-case letter to start the name of all variables and parameters. What you do is up to you but the most widespread .NET convention is to use lower-case to start variable and parameter names and upper-case for everything else.

    Finally, the use of the Call keyword in VB.NET is completely useless and I suggest that you stop using it immediately. It doesn't hurt but it serves no purpose. Its existence and usage is a VB6 holdover, where it was required under certain circumstances.
    intes likes this.

  3. #3
    intes is offline VB.NET Forum Newbie
    .NET Framework
    .NET 4.5
    Join Date
    Aug 2014
    Posts
    2
    Reputation
    0
    Thank you for your help and for useful tips . My mistake was that I thought that ByRef and ByVal applies for the whole row rather than one value, that's why I wrote it like that.

Bookmarks

Posting Permissions

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