If they all have the same properties, I would suggest an abstract base or an interface that defines all of the properties, then concrete implementations of each of them. I think using a case statement to determine type, the way you are, might be an indication you need to look at abstraction:
Class MustInherit PhoneBook Public MustOverride ReadOnly Property Name as String End Class Class PhoneBookFormatXYZ Inherits PhoneBook Public Overrides ReadOnly Property Name as String return mid(_LineOfText, 1, 12) End Property End Class Or for an interface: Interface IPhoneBook ReadOnly Property Name as String End Interface Class PhoneBookFormatXYZ Implements IPhoneBook Public ReadOnly Property Name as String Implements IPhoneBook.Name return mid(_LineOfText, 1, 12) End Property End Class I'd prefer the interface version in this case because it doesn't limit you to a singly rooted hierarchy. Then when you want to use it, you can do something like: Dim pb1 as IPhoneBook Dim pb2 as IPhoneBook Dim pb3 as IPhoneBook Dim pb4 as IPhoneBook pb1 = New PhoneBookFormatXYZ ' implicit cast since pb1 XYZ implements IPhoneBook pb2 = New PhoneBookFormatZZABC pb3 = New PhoneBookFormatABC pb4 = New PhoneBookFormat123 Dim s as string s = pb1.Name ' compiler knows about Name property since all pbX objects are IPhoneBooks ... s = pb2.Name ... s = pb3.Name ... s = pb4.Name ... -----Original Message----- From: Discussion of advanced .NET topics. [mailto:[EMAIL PROTECTED] On Behalf Of Jon Rothlander Sent: Saturday, September 09, 2006 12:38 AM To: ADVANCED-DOTNET@DISCUSS.DEVELOP.COM Subject: Re: [ADVANCED-DOTNET] Need help with a simple VB.Net Problem Thanks for you reply. Let me try to explain what I am trying to do so you can understand what I need to do. The sample I set up shows the problem, but it doesn't really show you what I am trying to do. Maybe I'm looking at the wrong solution to begin with. What I have is a class library that handles formatting for me from fixed-format text files. Let's say that I have the following line in a file. Phonebook Format 1 Greg Rothlander 1234 First Street Austin Texas 12345 Jerry Lee 5678 3rd Street Dallas Texas 34567 Phonebook Format 2 Greg Rothlander 1234 First Street Austin Texas 12345 Jerry Lee 5678 3rd Street Dallas Texas 34567 Phonebook Format 3 Greg Rothlander 1234 First Street Austin,Texas 12345 Jerry Lee 5678 3rd Street Dallas,Texas 34567 So in the first type of file and the class that defines the character positions, the first name is character position 1 thru 12, then last name is 13 thru 25, etc. What I did was build a class structure over the class. Lets say each of the phonebook class below is wrapped into a single PhonebookFormats Class. Sort of like... (This is not working code. Just enough to explain what I am doing.) Class PhonebookFormats Class Phonebook1 Class EntryItem _Lineoftext as string = "" Property FirstName() as string return mid(_LineOfText, 1, 12) End Property End Class Class PhoneBook1 However, depending on the file being read, other classes might look like the following.... Keep in mind that in the real class there are about 50 properties like the Name property here. The property simple pulls a given character set from the LineOfText property. Class Phonebook2 Class EntryItem _Lineoftext as string = "" Property Name() as string return mid(_LineOfText, 1, 18) End Property End Class End Class Class Phonebook3 Class EntryItem _Lineoftext as string = "" Property Name() as string return mid(_LineOfText, 1, 25) End Property End Class End Class 'Phonebook3 End Class 'PhonebookFormats One thing to keep in mind is that the class might have sub-class like the following... Lets say that there might be 3 different types of address records in each phonebook entry. The good thing is that if one of the Phonebooks has three address formats, they all will. That might look like... Class Phonebook3 Class EntryItem Private _Lineoftext as string = "" Public Property LineofText Set (Value as string) _LineOfText = Value End Set End Property Property Name() as string return mid(_LineOfText, 1, 25) End Property Property Address() as string return mid(_LineOfText, 30, 25) End Property Class AddressFormat1 property number... property street... End Class Class AddressFormat2 property number... property street... End Class Class AddressFormat3 property number... property street... End Class End Class Class PhoneBook3 So what I need to be able to do is create the classes for each type of record, which is actually many... probably 50 to 60 different types. Then based on what type of file I read, I need to load the correct class that contains the file formats. The code that processes the class and data after it is loaded, is the same for ALL of the code. It might have code like the following, assuming that the object version of this was working... Dim obj as object Select Case WhateverClassIsNeeded Case 0 obj = new PhonebookFormats.Phonebook1 Case 1 obj = new PhonebookFormats.Phonebook2 Case 2 obj = new PhonebookFormats.Phonebook3 End Select Dim sName as string = Obj.EntryItem.Name Dim sAddress as string = Obj.EntryItem.Address Does that make any sense? I have all of these classes where are basically the same thing with different formats. They have pretty much the same properties, but each property has a unique definition do to the character position it is looking at for the given text file. I have set up something similar using reflection, but I was hoping that there was a better way. There may be a better way to design this. If so, I would be very interested to here what others think. Best regards, Greg -----Original Message----- From: Discussion of advanced .NET topics. [mailto:[EMAIL PROTECTED] On Behalf Of Bob Provencher Sent: Friday, September 08, 2006 8:22 PM To: ADVANCED-DOTNET@DISCUSS.DEVELOP.COM Subject: Re: [ADVANCED-DOTNET] Need help with a simple VB.Net Problem I'm more familiar with C#, but I'll take a stab at it. I think Test1 is attempting to create new Object and store it's reference in obj, then create a new dateformatter and overwrite the ref previously stored in obj. This is not a cast. The type of obj is still Object. I think you can just do: Dim obj as Object obj = New DateFormatter.YearFormatter In Test2, you are dimensioning the same var twice. I think what you want is: 'Test2 'Dim obj As New DateFormatter.YearFormatter 'Lets just pretend that there is logic that could cause either one to be hit. 'For this sample, I'm forcing it to the first case. Dim int As Integer = 0 Dim str as String Select Case int Case 0 str = obj.PreY2K.YY Case 1 str = obj.PostY2K.YYYY End Select But, your code does not know that your object has PreY2K and PostY2K properties since you only have an Object reference. Since you only have the one type you can just change the declaration to be that type: Dim dtFmter as DateFormatter.YearFormatter If you have more than one time and that is the reason you think you need to Dim as an Object, then you might need to introduce a class hierarchy or an interface. Give us more details on what you are trying to do. -----Original Message----- From: Discussion of advanced .NET topics. [mailto:[EMAIL PROTECTED] On Behalf Of Jon Rothlander Sent: Friday, September 08, 2006 10:06 PM To: ADVANCED-DOTNET@DISCUSS.DEVELOP.COM Subject: [ADVANCED-DOTNET] Need help with a simple VB.Net Problem I'm hoping that someone can take a few minutes and take a look at the following code for me. It may look complex at first, but it actually is very simple. What I have here is a VB.Net WinForm with no controls on the form. When it executes, it runs through a sample app that I set up that recreates the problem that I am having, but it's not the same application. I wrote a sample that is based on a Y2K sample that I just made up because what I am actually working on is a compiler and there's just too much in the code to post the real thing. So this sample recreates the same problem, but keep in mind that it's not the same code so my solutions may be limited. If you create a WinForm and paste the code and close at the bottom of this post, you will see the following in a Test() function at the bottom of the code... 'Test1 Dim obj As New Object obj = New DateFormatter.YearFormatter 'Test2 'Dim obj As New DateFormatter.YearFormatter 'Lets just pretend that there is logic that could cause either one to be hit. 'For this sample, I'm forcing it to the first case. Dim int As Integer = 0 Select Case int Case 0 Dim str As String = obj.PreY2K.YY Case 1 Dim str As String = obj.PostY2K.YYYY End Select To see what my problem is. Use the code below and execute with a breakpoint on the line after the Test1 comment. You will get an error on the "dim str" line. Then comment out the Test1 code and uncomment the Test2 code. Run it again and note that you do not get the error. What's the difference? Note that Test1 uses a generic object and then casts it as the YearFormatter class. This causes an error when it hits the code that looks at the PreY2K.YY property. However in Test2 if I just go ahead and create it obj as that type, then I do not get an error. I do not see what the difference between the two is, but apparently I am overlooking something significant. The problem is that I have been using logic like Test2 and I need to change it to Test1 using a generic object. I need to make it generic because I have a number of different class that would be equivalent to YearFormatter. Maybe it would be sort of like having YearFormmater, YearFormatterY2K, and YearFormatterPreY2K classes, then needed the logic of the code to pick which one to use. So I need to be able to control which class becomes the obj object and set it accordingly. In the end, I just need to figure out how to get Test1 to work like Test2. Any ideas? Any suggestions or ideas would be very much appreciated. I think I am over looking something simple here. My sample code is below. Best regards, Greg Rothlander Public Class Form1 Inherits System.Windows.Forms.Form #Region " Windows Form Designer generated code " Public Sub New() MyBase.New() 'This call is required by the Windows Form Designer. InitializeComponent() 'Add any initialization after the InitializeComponent() call End Sub 'Form overrides dispose to clean up the component list. Protected Overloads Overrides Sub Dispose(ByVal disposing As Boolean) If disposing Then If Not (components Is Nothing) Then components.Dispose() End If End If MyBase.Dispose(disposing) End Sub 'Required by the Windows Form Designer Private components As System.ComponentModel.IContainer 'NOTE: The following procedure is required by the Windows Form Designer 'It can be modified using the Windows Form Designer. 'Do not modify it using the code editor. <System.Diagnostics.DebuggerStepThrough()> Private Sub InitializeComponent() ' 'Form1 ' Me.AutoScaleBaseSize = New System.Drawing.Size(5, 13) Me.ClientSize = New System.Drawing.Size(292, 273) Me.Name = "Form1" Me.Text = "Form1" End Sub #End Region Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load Dim obj As New SomeClass obj.Test() End Sub End Class Class DateFormatter Class YearFormatter Class PreY2K ' Definition for pre y2k file. ' YY Private Shared _YY As String = "" Public Shared Property YY() As String Get Return _YY End Get Set(ByVal Value As String) _YY = Value End Set End Property Public Shared Function ConvertToY2K() As String If CInt(YY > 50) Then Return "19" & YY Else Return "20" & YY End If End Function End Class Class PostY2K ' Definition for post y2k file. ' YYYY Private Shared _YYYY As String = "" Public Shared Property YYYY() As String Get Return _YYYY End Get Set(ByVal Value As String) _YYYY = Value End Set End Property Public Shared Function ConvertToPreY2K() As String Return Mid(YYYY, 3, 2) End Function End Class End Class End Class Class SomeClass Public Function Test() As String 'Test1 Dim obj As New Object obj = New DateFormatter.YearFormatter 'Test2 'Dim obj As New DateFormatter.YearFormatter Dim int As Integer = 0 Select Case int Case 0 Dim str As String = obj.PreY2K.YY Case 1 Dim str As String = obj.PostY2K.YYYY End Select End Function End Class =================================== This list is hosted by DevelopMentor. http://www.develop.com View archives and manage your subscription(s) at http://discuss.develop.com =================================== This list is hosted by DevelopMentor. http://www.develop.com View archives and manage your subscription(s) at http://discuss.develop.com =================================== This list is hosted by DevelopMentor. http://www.develop.com View archives and manage your subscription(s) at http://discuss.develop.com =================================== This list is hosted by DevelopMentorĀ® http://www.develop.com View archives and manage your subscription(s) at http://discuss.develop.com