Not sure how to create this class design...
I was wondering what would be the syntax to achieve an end result of something like this:
Dim MyClass as New Class1
With MyClass.User
.Name = "Dude"
.Email = "Dude@email.com"
End With
With MyClass.Employee
.Name = "Whoever"
.Email = "whoever@comp.com"
End with
I've tried classes within classes, structures within classes; but I keep getting all kinds of errors.
This was my last try:
Namespace CompanyInfo
Public Class Class1
Public Class User
Public Name as String
Public Email as String
Public Referred as String = "Paper" 'A default, doesn't need to be set
End Class
Public Class Employee
Public Name as String
Public Email as String
Public Referred as String = "Paper" 'A default, doesn't need to be set
End Class
End Class
End Namespace
Mind you, the above example may make no sense (Referred) - but it's just an example. :)
Any ideas are much appreciated.
- Denvas
[1129 byte] By [
Denvas] at [2007-12-28]
Hi,
I already had a CLASS in a project, the CLASS is called anyPerson.
Go to the PROJECT MENU and ADD CLASS in the usual way and paste
my code in to a class that needs to be named anyPerson.Vb
You can then have an array of users and an array of employees. 
Dim employees() As anyPersonDim users() As anyPerson
It includes firstName, surName, address1, address2, location ( as in town name),
country, telephoneNumber and now i've added email to it. Hope you like it?
My With ... End With statements are as follows.>>
Dim user1 As anyPersonWith user1.first_Name = "Fred"
.sur_Name = "Bloggs"
.e_mail = "f.bloggs@hotmail.com"
End WithDim employee1 As anyPersonWith employee1.first_Name = "Harry"
.sur_Name = "Houdini"
.e_mail = "h.houdini@aol.com"
End WithYou can do this sort of declaration too as well if you like.>>
Dim myDetails As New anyPerson("Fred", "Bloggs", _
"myRoad", "Acklam", "Middlesbrough", "TS5 1DD", _
"England", "01642 111111", "f.bloggs@aol.com")
' Here is my CLASS CODE.>>
Public
Class anyPersonPrivate firstName, surName As StringPrivate address1, address2 As StringPrivate location, postOrZipCode As StringPrivate country, telNo As StringPrivate email As StringPublic Sub New(ByVal first_name As String, _ByVal sur_name As String, ByVal addr1 As String, _ByVal addr2 As String, ByVal loc As String, _ByVal
code As String, ByVal countr As String, _ByVal
num As String, ByVal mailTo As String)firstName = first_name
surName = sur_name
address1 = addr1
address2 = addr2
location = loc
postOrZipCode = code
country = countr
telNo = num
email = mailTo
End SubProperty first_Name() As StringGetReturn firstNameEnd GetSet(ByVal Value As String)firstName = Value
End SetEnd PropertyProperty sur_Name() As StringGetReturn surNameEnd GetSet(ByVal Value As String)surName = Value
End SetEnd PropertyProperty yourAddressLine1() As StringGetReturn address1End GetSet(ByVal Value As String)address1 = Value
End SetEnd PropertyProperty yourAddressLine2() As StringGetReturn address2End GetSet(ByVal Value As String)address2 = Value
End SetEnd PropertyProperty yourLocation() As StringGetReturn locationEnd GetSet(ByVal Value As String)location = Value
End SetEnd PropertyProperty yourPostOrZipCode() As StringGetReturn postOrZipCodeEnd GetSet(ByVal Value As String)postOrZipCode = Value
End SetEnd PropertyProperty yourCountry() As StringGetReturn countryEnd GetSet(ByVal Value As String)country = Value
End SetEnd PropertyProperty yourTelNumber() As StringGetReturn telNoEnd GetSet(ByVal Value As String)telNo = Value
End SetEnd PropertyProperty e_mail() As StringGetReturn emailEnd GetSet(ByVal Value As String)email = Value
End SetEnd PropertyEnd
Class
Regards,
S_DS
Try This:
Public Class Form1
Dim C As New Class1
Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
Dim U As New Class1.users
With U
.Name = "Fred"
.EMail = "Fred@email.com"
End With
C.User = U
End Sub
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
MessageBox.Show(C.User.Name)
MessageBox.Show(C.User.EMail)
End Sub
End Class
Public Class Class1
Structure users
Dim Name As String
Dim EMail As String
End Structure
Private _User As users
Public Property User() As users
Get
Return _User
End Get
Set(ByVal value As users)
_User = value
End Set
End Property
End Class
Thank you both.
What I was looking for was more along the lines of what Dave299 wrote.
It's working great, but one element isn't working from my example.
Public Referred as String = "Paper" isn't working when I place it into the users Structure. It gives me "Initializers in structure members are only valid for 'Shared' members and constants."So, I added Shared. But then if I try to change the Referred within the Form1 class I get:
"Access of shared member, constant member, enum member or nested type through an instance; qualifying expression will not be evaluated."
Seems like I'm stuck between a rock a *** place LOL.
Most of these properties (Referred, and others) need to have defaults (Initializers), but they still have to be set from Form1 (in your code example)
Any ideas?
Thanks again,
Denvas
Public
Class Class1Structure usersDim Name As StringDim EMail As StringEnd StructurePrivate _User As usersPublic Property User() As usersGetReturn _UserEnd GetSet(ByVal value As users)_User = value
End SetEnd PropertyPublic ReadOnly Property Paper() As StringGetReturn "Paper"End GetEnd PropertyEnd
Class
Dim myPaper As StringDim page As New Class1myPaper = page.Paper
Oops i had this post wrong to start with....sorry.
Regards,
S_DS
Thanks
S_DS, but in your example, I need Paper to have a default Initializer AND be able to be set.
Denvas wrote: |
Thanks S_DS, but in your example, I need Paper to have a default Initializer AND be able to be set.
|
|
In your 1st post you said it doesn't need to be set, hence why i made it a readOnly property.
Set your default in the CLASS code then if you like.
Use this then.>>
Public
Class Class1Structure usersDim Name As StringDim EMail As StringEnd StructurePrivate myPaper As StringPrivate _User As users
Public Property User() As users
Get
Return _UserEnd GetSet(ByVal value As users)_User = value
End SetEnd PropertyPublic Property Paper() As StringGetReturn myPaperEnd GetSet(ByVal Value As String)myPaper = Value
End SetEnd PropertyEnd
Class
Dim whatever As New Class1whatever.Paper = "Daily Mirror."
Regards,
S_DS
That's cool. What I meant - it wasn't necessary to be set since it would be a default, but I still need it to be set.
I appreciate your time in trying to help - it's so annoying to get this just right. It looks like it should be such a simple task. But there are so many little restrictions that prevent me from getting a nice neat solution.
I revised the code from Structure to Class and it's working. It's not exactly what I wanted, since Cells and Cell can be confusing to the user - but it works; and I'm definitely happy about that.
If there's an alternate or addendum suggestion - I'm all ears. For now, this is my route.
-Denvas
LOL, I must've sent my post just as you were sending yours.
Thanks again, but what I'm trying to do, is keep "Paper" within the User structure/class (whatever.User.Paper = "Daily Mirror."). Since there will be two different structures/classes (User and Employee) that have the same attributes - the attributes need to be grouped by both User and Employee.
Kinda like this:
Dim MyClass as New Class1
With MyClass.User
.Name = "Dude"
.Email = "Dude@email.com"
.Referred = "Daily Mirror"
End With
With MyClass.Employee
.Name = "Whoever"
.Email = "whoever@comp.com"
End with
Appreciated,
Denvas
Not sure what you've finished up with from your post but I was about to post this - is it the same as your solution:
Public Class Form1
Public C As New Class1
Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
Dim U As New Class1.Users
With (U)
.Name = "Fred"
.EMail = "Fred@email.com"
End With
C.User = U
End Sub
Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click
MsgBox(C.User.Name)
MsgBox(C.User.EMail)
MsgBox(C.User.Referred)
End Sub
End Class
Public Class Class1
Private _User As Users
Public Property User() As Users
Get
Return _User
End Get
Set(ByVal value As Users)
_User = value
End Set
End Property
Public Class Users
Public Name As String
Public EMail As String
Public Referred As String = "Paper"
End Class
End Class
God this forum gets confusing when it jumbles the posts up (and changing the subject title every time doesn't help!!). Is this what you've finished up with:
Public Class Form1
Public C As New Class1
Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
Dim U As New Class1.Users
Dim M As New Class1.Employees
With (U)
.Name = "Fred"
.EMail = "Fred@email.com"
End With
C.User = U
With M
.Name = "Ford"
.EMail = "Ford@email.com"
End With
C.Employee = M
End Sub
Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click
MsgBox(C.User.Name)
MsgBox(C.User.EMail)
MsgBox(C.User.Referred)
MsgBox(C.Employee.Name)
MsgBox(C.Employee.EMail)
MsgBox(C.Employee.Referred)
End Sub
End Class
Public Class Class1
Private _User As Users
Private _Employee As Employees
Public Property User() As Users
Get
Return _User
End Get
Set(ByVal value As Users)
_User = value
End Set
End Property
Public Property Employee() As Employees
Get
Return _Employee
End Get
Set(ByVal value As Employees)
_Employee = value
End Set
End Property
Public Class Users
Public Name As String
Public EMail As String
Public Referred As String = "Computer Weekly"
End Class
Public Class Employees
Public Name As String
Public EMail As String
Public Referred As String = "The Sun"
End Class
End Class
Yes, exactly. It works, and I appreciate that.
The thing is another programmer is going to be using this. And Users sounds like a collection of User - which will be confusing. I'm trying to make the Users class private, so the programmer(s) only see User. The only way I've been able to that is to take out the references to Users in the User property because just making it Private gave me an error - "'User' cannot expose type 'Users' in namespace 'whatever' through class 'Class1'." - But this is EXACTLY what I want to do. Arggh, so frustrating LOL.
Ex:
Public Class Class1
Private _User As Users
Public Property User()
Get
Return _User
End Get
Set(ByVal value)
_User = value
End Set
End Property
Private Class Users
Public Name As String
Public EMail As String
Public Referred As String = "Paper"
End Class
End ClassBut I don't like this solution b/c it's not typed, and it doesn't show up in intellisense. Almost there, but still feels so far away :(- Denvas
I so agree. How did my later post end up before my previous post? LOL
See my last post. I did end up with that with a lil' modding. It's almost where I want it - but just not there.
Denvas wrote: |
LOL, I must've sent my post just as you were sending yours.
Thanks again, but what I'm trying to do, is keep "Paper" within the User structure/class (whatever.User.Paper = "Daily Mirror."). Since there will be two different structures/classes (User and Employee) that have the same attributes - the attributes need to be grouped by both User and Employee.
Kinda like this:
Dim MyClass as New Class1
With MyClass.User .Name = "Dude" .Email = "Dude@email.com" .Referred = "Daily Mirror" End With
With MyClass.Employee .Name = "Whoever" .Email = "whoever@comp.com" End with
Appreciated, Denvas
|
|
My very first post used the same CLASS for both user and employee each with their attributes.
Would it not have been easier to use?
Dim user() As anyPerson
Dim employee() As anyPerson
user(1).first_name="John"
user(1).e_mail="j@aol.com"
employee(1).first_name="Ted"
employee(1).e_mail="t@hotmail.com"
'or using WITH>>
With user(1)
.first_Name="John"
.e_mail="j@aol.com"
End With
With employee(1)
.first_name="Ted"
.e_mail="t@hotmail.com"
End With
I thought an array of users and an array of employees would be a better approach?
Regards,
S_DS
You're solution works well. It's just the user and employee belong to a higher class called
Class1. Class1 needs to hold the data for both the user and employee. Whether it's an array, or just a simple string variable really doesn't matter.Changing the code you provided to fit into this model gives me the error: "'User' is a type in 'Class1' and cannot be used as an expression".
This is my dilemma.