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.
Last edited by jrothlander; 07-01-2006 at 05:03 PM.
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.
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"
textbox1.text = strValue 'should return "C"
Public Function Lookup(byref str1 as string, byref str2 as string) as boolean
str2 = "C"
That did not work for me. Should it have worked that way? Maybe I messed up something.
The above code works for me exactly as you describe; strValue contains "C" after the function call. Here's my code:
Dim strValue As String
Debug.WriteLine(strValue) ' Prints "C"
Public Function Lookup(ByRef str1 As String, ByRef str2 As String) As Boolean
str2 = "C"
Please post questions to the forums, where others may benefit.
I do not offer free assistance by e-mail. Thank you!
Last Post: 07-09-2005, 06:50 AM
By Guillaume LagacÚ in forum ASP.NET
Last Post: 05-21-2002, 02:50 PM
Last Post: 03-28-2002, 07:14 AM
By Sandoval Gonzalez in forum VB Classic
Last Post: 11-09-2001, 04:29 PM
Last Post: 09-28-2000, 04:11 PM
Top DevX Stories
Easy Web Services with SQL Server 2005 HTTP Endpoints
JavaOne 2005: Java Platform Roadmap Focuses on Ease of Development, Sun Focuses on the "Free" in F.O.S.S.
Wed Yourself to UML with the Power of Associations
Microsoft to Add AJAX Capabilities to ASP.NET
IBM's Cloudscape Versus MySQL