Results 1 to 5 of 5

Thread: VB.net code help

  1. #1

    Thread Starter
    Dazed Member
    Join Date
    Oct 1999
    Location
    Ridgefield Park, NJ
    Posts
    3,418

    Talking VB.net code help

    What's up all. .net is here so i guess i will attempt to make the transition I haven't programmed in Visual Basic in awhile so im looking for code that is equivalent to Java. For instance i want to implement getters/setters in Visual Basic.

    In Java i would just do somthing like.......
    Code:
    public class Guage{
    
      static private double temp;
      static private double dewpoint;
    
      public void setTemp(double temp){
        this.temp = temp; 
      }
      public double getTemp(){
        return temp; 
      } 
      public void setDewpoint(double dewpoint){
        this.dewpoint = dewpoint; 
      } 
      public double getDewpoint(){
        return dewpoint; 
      }
     
      public static void main(String[] args){
         Guage g = new Guage();
         System.out.println(" Temp is " + g.getTemp()  + " Dewpoint is " + g.getDewpoint());
         g.setTemp(56.7); 
         g.setDewpoint(76.6);
         System.out.println(" Temp is " + g.getTemp()  + " Dewpoint is " + g.getDewpoint());            
      }
    }
    Now in Visual Basic .net i see that bolth are lumped in one property like the following.
    Code:
      Dim strName as String
    
      Property Name() as String
       Get
        Name = strName 
       End Get
    
       Set(byVal sName as String)
        strName = Value
       End Set
     End Property
    Is this the proper way to use code getters/setters in Visual Basic?
    And i thought that Visual Basic was true OOP? So i there a way to refrence the object in which the current method is being called? It seems stupid to define this method with a return type of String when say if the value is just being set then we don't want anything returned. That's the way it was in VB6. As in the following.
    Code:
      Dim strName as String
      
      Property Get Name() as String
        Name = strName
      End Property 
    
      Property Let Name(strNameIn as String)
       strName  = strNameIn
      End Property

  2. #2
    I'm about to be a PowerPoster! Joacim Andersson's Avatar
    Join Date
    Jan 1999
    Location
    Sweden
    Posts
    14,649
    Since a Property is constructed with both a set and a get keyword it must have a return type but the property only returns something when the Get part is called.

  3. #3

    Thread Starter
    Dazed Member
    Join Date
    Oct 1999
    Location
    Ridgefield Park, NJ
    Posts
    3,418
    Posted by Joacim Andersson
    Since a Property is constructed with both a set and a get keyword it must have a return type but the property only returns something when the Get part is called.
    thanks for replying. No i understand that the property only returns a value when the get part is called. But dosent this seem an akward way to program? It made more sense the way they were doing it before. Also you said "since a propety is constructed with a bolth a get and set keyword." It sounds like in order to construct a property a get and set must be supplied. Do i always need bolth? Thanks.

  4. #4
    I'm about to be a PowerPoster! Joacim Andersson's Avatar
    Join Date
    Jan 1999
    Location
    Sweden
    Posts
    14,649
    No you don't need both. You can have a read-only property by just adding a Get Property or a Write-Only (but this feels akward) to only provide a Set Property.
    Me personally doesn't feel that the way a property is constructed in VB.Net is not akward at all, it's just a different way.
    You have, of course, the choise to add two methods as in Java instead of a property construction.
    However this way of handling objects is not the normal way for a VB user.

    What I don't like with the new Property construction is that the Property Get and Property Set can't have different scoping.
    In VB5 and VB6 you could make one public and declare the other as Friend, this can not be done in VB.Net.
    So in this case you will need a read-only property and a Friend method to alter the value.

    Best regards

  5. #5
    Frenzied Member DevGrp's Avatar
    Join Date
    Nov 2001
    Location
    Charlotte, NC
    Posts
    1,256
    I aslo get a little confused with the way VB implements the setters and getters.

    I think if you have a C++ or a java background, it tend to get a little confusing.
    Dont gain the world and lose your soul

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