Not sure how to create this class design. Here is the Class1 code with a read only property.

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]
# 1

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 anyPerson

Dim 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 anyPerson

With user1

.first_Name = "Fred"

.sur_Name = "Bloggs"

.e_mail = "f.bloggs@hotmail.com"

End With

Dim employee1 As anyPerson

With employee1

.first_Name = "Harry"

.sur_Name = "Houdini"

.e_mail = "h.houdini@aol.com"

End With

You 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 anyPerson

Private firstName, surName As String

Private address1, address2 As String

Private location, postOrZipCode As String

Private country, telNo As String

Private email As String

Public 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 Sub

Property first_Name() As String

Get

Return firstName

End Get

Set(ByVal Value As String)

firstName = Value

End Set

End Property

Property sur_Name() As String

Get

Return surName

End Get

Set(ByVal Value As String)

surName = Value

End Set

End Property

Property yourAddressLine1() As String

Get

Return address1

End Get

Set(ByVal Value As String)

address1 = Value

End Set

End Property

Property yourAddressLine2() As String

Get

Return address2

End Get

Set(ByVal Value As String)

address2 = Value

End Set

End Property

Property yourLocation() As String

Get

Return location

End Get

Set(ByVal Value As String)

location = Value

End Set

End Property

Property yourPostOrZipCode() As String

Get

Return postOrZipCode

End Get

Set(ByVal Value As String)

postOrZipCode = Value

End Set

End Property

Property yourCountry() As String

Get

Return country

End Get

Set(ByVal Value As String)

country = Value

End Set

End Property

Property yourTelNumber() As String

Get

Return telNo

End Get

Set(ByVal Value As String)

telNo = Value

End Set

End Property

Property e_mail() As String

Get

Return email

End Get

Set(ByVal Value As String)

email = Value

End Set

End Property

End Class

Regards,

S_DS

Spidermans_DarkSide at 2007-9-4 > top of Msdn Tech,Visual Basic,Visual Basic Language...
# 2

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

Dave299 at 2007-9-4 > top of Msdn Tech,Visual Basic,Visual Basic Language...
# 3
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

Denvas at 2007-9-4 > top of Msdn Tech,Visual Basic,Visual Basic Language...
# 4

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

Public ReadOnly Property Paper() As String

Get

Return "Paper"

End Get

End Property

End Class

Dim myPaper As String

Dim page As New Class1

myPaper = page.Paper

Oops i had this post wrong to start with....sorry.

Regards,

S_DS

Spidermans_DarkSide at 2007-9-4 > top of Msdn Tech,Visual Basic,Visual Basic Language...
# 5
Thanks S_DS, but in your example, I need Paper to have a default Initializer AND be able to be set.
Denvas at 2007-9-4 > top of Msdn Tech,Visual Basic,Visual Basic Language...
# 6

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 Class1

Structure users

Dim Name As String

Dim EMail As String

End Structure

Private myPaper As String

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 Property Paper() As String

Get

Return myPaper

End Get

Set(ByVal Value As String)

myPaper = Value

End Set

End Property

End Class

Dim whatever As New Class1

whatever.Paper = "Daily Mirror."

Regards,

S_DS

Spidermans_DarkSide at 2007-9-4 > top of Msdn Tech,Visual Basic,Visual Basic Language...
# 7
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

Denvas at 2007-9-4 > top of Msdn Tech,Visual Basic,Visual Basic Language...
# 8
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
Denvas at 2007-9-4 > top of Msdn Tech,Visual Basic,Visual Basic Language...
# 9

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

Dave299 at 2007-9-4 > top of Msdn Tech,Visual Basic,Visual Basic Language...
# 10

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

Dave299 at 2007-9-4 > top of Msdn Tech,Visual Basic,Visual Basic Language...
# 11
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 Class

But 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

Denvas at 2007-9-4 > top of Msdn Tech,Visual Basic,Visual Basic Language...
# 12
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 at 2007-9-4 > top of Msdn Tech,Visual Basic,Visual Basic Language...
# 13
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

Spidermans_DarkSide at 2007-9-4 > top of Msdn Tech,Visual Basic,Visual Basic Language...
# 14
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.

Denvas at 2007-9-4 > top of Msdn Tech,Visual Basic,Visual Basic Language...