VB.Net Class Design Question


DevX Home    Today's Headlines   Articles Archive   Tip Bank   Forums   

Results 1 to 4 of 4

Thread: VB.Net Class Design Question

Hybrid View

  1. #1
    Join Date
    Nov 2003
    Location
    Austin, Texas
    Posts
    59

    VB.Net Class Design Question

    I've been working with a simple class library and a class that built. I was doing something that worked, but I broke it and now I cannot get it to work any longer. Some of the testing that I have been doing has shown me that I am missing something here, but I need to understand this. I think it is very simple, but I simply have never needed this before and I have not ran across in the past 4+ years of working in .Net.

    Here's the basic idea.

    Class Library contains a class, lets call it LOOKUP(). Lets say that the class is used for a phone book. The LOOKUP() method would have parameters to pass in such as that might be fname, mname, lname, address, city, state, zip, and phonenumber. Lets say that the class1 has a lookup mechanism where you can send in different parameters and the process will lookup the rest and return it to the calling code. That's sort of on the same level of what I am looking at in my app. It should be very simple.

    However, I am doing something a little odd. I cannot return the data using a dataset, datarow, xml, etc. I need to return the same parameters of the method that go in. So I need to return the fname, mname, lname, address, city, state, zip, and phonenumber using the same names as used in the method's parameters.

    Here's an example in VB.Net.

    Public Function LookUp(ByVal fname As String, ByVal mname As String, ByVal lname As String, ByVal address As String, ByVal city As String, ...

    What I did was to create properties for each of the these. Something like

    Public Property fname() as string
    Public Property mname() as string
    Public Property lname() as string
    ...

    This started to work, but then I realized that .Net was automatically hooking the property and the parameter up. So my guess is that there is a way that .Net can handle this, but I'm missing something.

    Can someone point me in the right direction here? Does .Net handle this sort of thing for you? I want to call a method with a given set of parameters, then have those parameters be accessed after the method has exited. Setting up properties seems to the be correct way to do this, but .net sync'd them for me. So I did not have to set them equal to each other in the code, and I was able to use the same name for the properties and the parameter.

    So my question is, did Microsoft set up something for this so the parameters and the properties could work together without having to do a lot of coding? It looks like they did, but I'm running into a few bugs with it. Are they supposed to automatically sync because they have the same name? That is what I want. I'm just hitting some bugs with this and I think I am setting up something incorrectly. What's the correct way to do this?

    Any suggestions would be appreicated.

    Best regards...
    Last edited by jrothlander; 07-01-2006 at 05:03 PM.

  2. #2
    Join Date
    Dec 2004
    Posts
    61
    If you want to get the value as soon as you call the method you can change the ByVal to ByRef, so every variables that pass to your method's parameters will have the same changes. ByVal only copy the value, so every changes to the parameters will not effect the variables you've passed, but ByRef is copy the reference (the address where the variables belongs in memory) so it will point to the same memory address.

    In VB6 all method's parameter is ByRef by default, but in .NET it's ByVal by default. It's because using ByRef is not a good practice of OOP, beside that receiving value from a lots of method's parameters is not a good OOP practice too.

    me_code2004.

  3. #3
    Join Date
    Nov 2003
    Location
    Austin, Texas
    Posts
    59
    I see what you mean and that is a reasonable approach, but I don't think it's exactly what I need. It may be, but I'm not able to change the values of the objects passed in as parameters. I can change the values of the parameters, but they do not update the values in the calling program. It sounds like that is what you are saying that it should do. Something like...

    dim strValue as string = "B"
    LookUp("A", strValue)
    textbox1.text = strValue 'should return "C"

    Public Function Lookup(byref str1 as string, byref str2 as string) as boolean
    str2 = "C"
    End Function

    That did not work for me. Should it have worked that way? Maybe I messed up something.

  4. #4
    Join Date
    Nov 2003
    Location
    Portland, OR
    Posts
    8,387
    The above code works for me exactly as you describe; strValue contains "C" after the function call. Here's my code:
    Code:
    Sub Main()
        Dim strValue As String
        LookUp("A", strValue)
        Debug.WriteLine(strValue)     ' Prints "C"
    End Sub
    
    Public Function Lookup(ByRef str1 As String, ByRef str2 As String) As Boolean
        str2 = "C"
    End Function
    Phil Weber
    http://www.philweber.com

    Please post questions to the forums, where others may benefit.
    I do not offer free assistance by e-mail. Thank you!

Similar Threads

  1. Replies: 0
    Last Post: 07-09-2005, 06:50 AM
  2. using a c# class in a vb.net webservice
    By Guillaume LagacÚ in forum ASP.NET
    Replies: 1
    Last Post: 05-21-2002, 02:50 PM
  3. question on best programming option
    By James in forum .NET
    Replies: 2
    Last Post: 03-28-2002, 07:14 AM
  4. Design Question
    By Sandoval Gonzalez in forum VB Classic
    Replies: 5
    Last Post: 11-09-2001, 04:29 PM
  5. EJB Design Question
    By Chad in forum Java
    Replies: 1
    Last Post: 09-28-2000, 04:11 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
HTML5 Development Center
 
 
FAQ
Latest Articles
Java
.NET
XML
Database
Enterprise
Questions? Contact us.
C++
Web Development
Wireless
Latest Tips
Open Source


   Development Centers

   -- Android Development Center
   -- Cloud Development Project Center
   -- HTML5 Development Center
   -- Windows Mobile Development Center