473,725 Members | 2,017 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

A Question on VB Classes

Can some one please tell me what I'm doing wrong. I'm trying to create
a class called Dog, but Visual Basic tells me that I can't enter
Wolf.age....why is this?

Public Class Form1
Public Class DOG
Dim COLOUR As String
Dim AGE As Integer
Dim NAME As String

End Class
Public Sub Button1_Click(B yVal sender As System.Object, ByVal e As
System.EventArg s) Handles Button1.Click
Dim WOLF As New DOG
WOLF.AGE = 10

End Sub
End Class

Regards Brian
Sep 19 '06
25 1494
Do you mean has a class file called DOG.vb with Get and Set
statements?

Regards Brian
GhostInAK <gh*******@gmai l.comwrote:
>Brian,

While it may work, it's ugly as sin. You should separate your DOG class
out into a different file. Also, instead of using fields publicly you should
make them private and provide public properties instead.

-Boo
>Because the property variables aren't visible. Declare them with the
public keyword instead of dim (which is private by default). i.e. The
following should work just fine.

Public Class Form1
Public Class DOG
Public COLOUR As String
Public AGE As Integer
Public NAME As String
End Class

Public Sub Button1_Click(B yVal sender As System.Object, ByVal e As
System.EventAr gs) Handles Button1.Click
Dim WOLF As New DOG
WOLF.AGE = 10
End Sub
End Class
Brian wrote:
>>Can some one please tell me what I'm doing wrong. I'm trying to
create a class called Dog, but Visual Basic tells me that I can't
enter Wolf.age....why is this?

Public Class Form1
Public Class DOG
Dim COLOUR As String
Dim AGE As Integer
Dim NAME As String
End Class

Public Sub Button1_Click(B yVal sender As System.Object, ByVal e As
System.EventA rgs) Handles Button1.Click
Dim WOLF As New DOG
WOLF.AGE = 10
End Sub
End Class
Regards Brian
Sep 20 '06 #11
How would you re-write the DOG example?

Regards Brian

"Ray Cassick \(Home\)" <rc************ @enterprocity.c omwrote:
>Yeah...

Public members are baaaaaaaaaaaaaa aaaaaaaaaaaaaaa d.

Think encapsulation.

If I ever found one of my coders doing Public x As Integer in a class I
would force them to clean the bathrooms for a week.

"Spam Catcher" <sp**********@r ogers.comwrote in message
news:Xn******* *************** ************@12 7.0.0.1...
>"Robinson" <it************ *****@nowmyinbo xisfull.comwrot e in news:eeoj9j
$j************ *@news.demon.co .uk:

>>Your "Dim" isn't public by default, try this instead:

Public COLOUR As String
Public AGE As Integer
Public NAME As String

Even better declare the variables as properties : )
Sep 20 '06 #12
Could you give me an example as I'm new to Classes.

Regards Brian

"rowe_newsgroup s" <ro********@yah oo.comwrote:
>Why use a private variable then a property to read/write it if no special
actions are needed for the read and write?

The dog class isn't a real good example, but normally you want more
control over the property (validation for example). Just like Option
Strict On isn't always needed (and requires more code) using properties
instead of variables is just good programming practice. IMHO it
improves the readablity of the code and makes it much much easier to
update and/or modify.
>Why separate the dog class to a different file...moving it to after the end
of the form class should work also.

Again, this is not a good example but moving it to a different file is
mainly for code reuse (OOP). If this was a more "advanced" class that
would be used in multiple solutions, being in the form's class file
wouldn't be a good idea. You would have to add an unneeded and unwanted
class (the form class) just to get to the advanced class. In my opinion
you shouldn't group unrelated classes in the same file.

Pretty much the suggestions were mainly to teach someone new to vb
classes good programming practices, not to say that what he had was
wrong.

Hope that clarifies some things,

Seth Rowe

Dennis wrote:
>Why separate the dog class to a different file...moving it to after the end
of the form class should work also.

Why use a private variable then a property to read/write it if no special
actions are needed for the read and write?

--
Dennis in Houston
"GhostInAK" wrote:
Brian,

While it may work, it's ugly as sin. You should separate your DOG class
out into a different file. Also, instead of using fields publicly you should
make them private and provide public properties instead.
-Boo

Because the property variables aren't visible. Declare them with the
public keyword instead of dim (which is private by default). i.e. The
following should work just fine.

Public Class Form1
Public Class DOG
Public COLOUR As String
Public AGE As Integer
Public NAME As String
End Class

Public Sub Button1_Click(B yVal sender As System.Object, ByVal e As
System.EventArg s) Handles Button1.Click
Dim WOLF As New DOG
WOLF.AGE = 10
End Sub
End Class
Brian wrote:

Can some one please tell me what I'm doing wrong. I'm trying to
create a class called Dog, but Visual Basic tells me that I can't
enter Wolf.age....why is this?

Public Class Form1
Public Class DOG
Dim COLOUR As String
Dim AGE As Integer
Dim NAME As String
End Class

Public Sub Button1_Click(B yVal sender As System.Object, ByVal e As
System.EventAr gs) Handles Button1.Click
Dim WOLF As New DOG
WOLF.AGE = 10
End Sub
End Class
Regards Brian

Sep 20 '06 #13
I'm just using the DOG class as an example to better understand how
classes work in VB.

Dennis <De****@discuss ions.microsoft. comwrote:
>Why separate the dog class to a different file...moving it to after the end
of the form class should work also.

Why use a private variable then a property to read/write it if no special
actions are needed for the read and write?
Sep 20 '06 #14
>Do you mean has a class file called DOG.vb with Get and Set
>statements?
Yes

Here's a very simple example. Note, I do no validation or error
trapping in this sample, you should add this yourself.

' Aircode Warning

Public Class Dog

' Use an enum if you want to control allowed values for Color
Public Enum ColorEnum
Brown
Black
White
Golden
' ...etc
End Enum

Private m_Color As ColorEnum
Private m_BirthDate As Date
Private m_Name As String

Public Property Color() As ColorEnum
Get
Return m_Color
End Get
Set(ByVal value As ColorEnum)
m_Color = value
End Set
End Property

Public Property Name() As String
Get
Return m_Name
End Get
Set(ByVal value As String)
m_Name = value
End Set
End Property

Public Property Birthdate() As Date
Get
Return m_BirthDate
End Get
Set(ByVal value As Date)
m_BirthDate = value
End Set
End Property

' This is a better way to get the age property (IMHO)
Public ReadOnly Property Age() As Integer
Get
Return DateDiff(DateIn terval.Year, Now(), m_BirthDate)
End Get
End Property

End Class
Thanks,

Seth Rowe
Brian wrote:
I'm just using the DOG class as an example to better understand how
classes work in VB.

Dennis <De****@discuss ions.microsoft. comwrote:
Why separate the dog class to a different file...moving it to after the end
of the form class should work also.

Why use a private variable then a property to read/write it if no special
actions are needed for the read and write?
Sep 20 '06 #15
If someone could give me easy to follow examples of using Classes in
Visual Basic then that would be helpful.
What's your programming background? Maybe we could recomend some
books/websites that might help if we knew what you do and don't know.
Perhaps a web address the has code for a VB program that I could
download or if you have code that could be ziped up and sent to me by
e-mail for me to study then this would be helpful to learn more abolut
how to use classes in VB program code thanks.
www.codeproject.com

Download some beginner classes and just start stepping through the code
to see how they use classes.

Thanks,

Seth Rowe

Brian wrote:
Thanks for all your repies.

If someone could give me easy to follow examples of using Classes in
Visual Basic then that would be helpful.
Perhaps a web address the has code for a VB program that I could
download or if you have code that could be ziped up and sent to me by
e-mail for me to study then this would be helpful to learn more abolut
how to use classes in VB program code thanks.

Regards Brian


Brian <bc****@es.co.n zwrote:
Can some one please tell me what I'm doing wrong. I'm trying to create
a class called Dog, but Visual Basic tells me that I can't enter
Wolf.age....why is this?

Public Class Form1
Public Class DOG
Dim COLOUR As String
Dim AGE As Integer
Dim NAME As String

End Class
Public Sub Button1_Click(B yVal sender As System.Object, ByVal e As
System.EventArg s) Handles Button1.Click
Dim WOLF As New DOG
WOLF.AGE = 10

End Sub
End Class

Regards Brian
Sep 20 '06 #16

"Spam Catcher" <sp**********@r ogers.comwrote in message
news:Xn******** *************** ***********@127 .0.0.1...
"Robinson" <it************ *****@nowmyinbo xisfull.comwrot e in news:eeoj9j
$j************* @news.demon.co. uk:

>Your "Dim" isn't public by default, try this instead:

Public COLOUR As String
Public AGE As Integer
Public NAME As String

Even better declare the variables as properties : )
Well I would have suggested that obviously, but one step at a
time........... ...............
private m_Colour As String
private m_Age As Integer
private m_Name as String

public property Colour As String

Get
return m_Colour
End Get

Set ( byval Value As String)
m_Colour = Value
End Set

End property

etc............ ......

However, in some cases this is utterly superfluous, so I wouldn't be too
religious about "always" wrapping a variable inside a property. Use common
sense. Properties are useful if you want to make access "readonly" or
"writeonly" , or if you want to change other state when a property changes,
or if you want to hide the underlying representation of the object. If you
aren't sure whether you want to do any of these things, use properties just
in case ;).


Sep 20 '06 #17
However, in some cases this is utterly superfluous, so I wouldn't be too
religious about "always" wrapping a variable inside a property. Use common
sense.
My only argument is that classes often vary from what they were
originally designed for, so using properties is like preparing for the
future. In my opinion properties are easier to adapt to new
circumstances than variables (take overloading for example). Of course
the time saved in this is lost in the time it takes to write out the
property. The other huge benefit is in inheritance as you can't
override member variables. I guess it's kind of like non-combat troops
carrying weapons, they are rarely (if ever) going to use that weapon,
but it can sure come in handy if they get attacked. Again, this is all
my opinion, and I'll admit I'm a "just-in-case" kind of person :-)
>The other huge benefit is in inheritance as you can't override member variables
Oh yeah, I know that I didn't put the overridable keyword in my
properties - I didn't want to give Brian to much info to fast. So
Brian, please add that keyword just in case! I can explain it's uses if
you need me to.

Thanks,

Seth Rowe

Robinson wrote:
"Spam Catcher" <sp**********@r ogers.comwrote in message
news:Xn******** *************** ***********@127 .0.0.1...
"Robinson" <it************ *****@nowmyinbo xisfull.comwrot e in news:eeoj9j
$j************* @news.demon.co. uk:

Your "Dim" isn't public by default, try this instead:

Public COLOUR As String
Public AGE As Integer
Public NAME As String
Even better declare the variables as properties : )

Well I would have suggested that obviously, but one step at a
time........... ...............
private m_Colour As String
private m_Age As Integer
private m_Name as String

public property Colour As String

Get
return m_Colour
End Get

Set ( byval Value As String)
m_Colour = Value
End Set

End property

etc............ ......

However, in some cases this is utterly superfluous, so I wouldn't be too
religious about "always" wrapping a variable inside a property. Use common
sense. Properties are useful if you want to make access "readonly" or
"writeonly" , or if you want to change other state when a property changes,
or if you want to hide the underlying representation of the object. If you
aren't sure whether you want to do any of these things, use properties just
in case ;).
Sep 20 '06 #18
My only argument is that classes often vary from what they were
originally designed for, so using properties is like preparing for the
future. In my opinion properties are easier to adapt to new
circumstances than variables (take overloading for example).
I rarely overload or override properties in the majority of my classes.
However I *always* make all variables private by default and then create a
public property for one if and when I need to access it from outside of the
class. This is just from habit of course. In general most of this code
isn't doing anything useful, however it does ensure my design doesn't get
broken in some circumstances where it might otherwise (reflection for
example - which I have to add I rarely use!). There is something of a
discussion here (http://www.codinghorror.com/blog/archives/000654.html)
about this very issue. But as I said before, I wouldn't be religious about
it. I mean I still use "Goto" extensively in SQL stored procedures (but in
an ON ERROR GOTO _Error kind of way) and when teaching a new "player" to the
OO game, I certainly wouldn't stress the point. It's just a construct like
any other. You tend to learn from experience when best to apply it and when
not to, so one thing at a time: Private, the variables cannot be "seen"
from outside of itself "Public" it can, "Protected" nobody knows what
possible use this has ;).
Sep 20 '06 #19
Well said Robinson, I think that sums it up!
"Protected" nobody knows what possible use this has ;).
It's use it to confuse newbie's of course!

Actually I use them occasionally to share data through nested classes.
Like in the dog example if I had dropped the Age property into a nested
class I could still see a "Protected m_Birthdate as date" variable in
the main class. Personally I think it should be renamed to
"ClassPriva te" or something similar to stop all the confusion.

So Brian, are you still with us or have we confused you to much?

Thanks,

Seth Rowe
Robinson wrote:
My only argument is that classes often vary from what they were
originally designed for, so using properties is like preparing for the
future. In my opinion properties are easier to adapt to new
circumstances than variables (take overloading for example).

I rarely overload or override properties in the majority of my classes.
However I *always* make all variables private by default and then create a
public property for one if and when I need to access it from outside of the
class. This is just from habit of course. In general most of this code
isn't doing anything useful, however it does ensure my design doesn't get
broken in some circumstances where it might otherwise (reflection for
example - which I have to add I rarely use!). There is something of a
discussion here (http://www.codinghorror.com/blog/archives/000654.html)
about this very issue. But as I said before, I wouldn't be religious about
it. I mean I still use "Goto" extensively in SQL stored procedures (but in
an ON ERROR GOTO _Error kind of way) and when teaching a new "player" to the
OO game, I certainly wouldn't stress the point. It's just a construct like
any other. You tend to learn from experience when best to apply it and when
not to, so one thing at a time: Private, the variables cannot be "seen"
from outside of itself "Public" it can, "Protected" nobody knows what
possible use this has ;).
Sep 20 '06 #20

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

1
741
by: Bob Rock | last post by:
Hello, in the last few days I've made my first few attempts at creating mixed C++ managed-unmanaged assemblies and looking aftwerwards with ILDASM at what is visible in those assemblies from a managed point-of-view I've noticed that: 1) for each managed and unmanaged C function (not C++ classes) I get a public managed static method (defined on a 'Global Functions' class) in the generated assembly with an export name of the form...
9
1653
by: Jack | last post by:
Hello I have a library of calculationally intensive classes that is used both by a GUI based authoring application and by a simpler non-interactive rendering application. Both of these applications need to serialise the classes to/from the same files but only the GUI app needs the full range of class methods. Now, the rendering app needs to be ported to multiple OS's but the GUI doesn't. In order to reduce the time/cost of porting I'd...
9
6638
by: Aguilar, James | last post by:
I know that one can define an essentially unlimited number of classes in a file. And one can declare just as many in a header file. However, the question I have is, should I? Suppose that, to use the common example, I have a situation where I am implementing many types of Shapes. My current way of thinking is, well, since they are all the same type, let's just put them all in the same file. The include file would be "shapes.h" and it...
12
21233
by: Langy | last post by:
Hello I'm fairly new to C++ but have programmed several other languages and found most of c++ fairly easy (so far!). I've come to a tutorial on classes, could someone please tell me why you would need to use a class? Perhaps you could also give an example on when it might be used rather than an alternative method.
4
1807
by: john townsley | last post by:
do people prefer to design classes for the particular job or for a rangle of tasks they might encounter now and in the future. i am doing some simple win32 apps and picking classes is simple, but understanding others peoples logic isnt (that doesnt mean they are wrong). i prefer designing classes for the currect job and making tangible 'things' classes , not overdoing it with loads of classes or inheritance.. it seems easier to make...
2
9503
by: joye | last post by:
Hello, My question is how to use C# to call the existing libraries containing unmanaged C++ classes directly, but not use C# or managed C++ wrappers unmanaged C++ classes? Does anyone know how to do that? Thanks. Tsung-Yu
18
2042
by: Edward Diener | last post by:
Is the packing alignment of __nogc classes stored as part of the assembly ? I think it must as the compiler, when referencing the assembly, could not know how the original data is packed otherwise. Yet, in my understanding, attributes are only __gc and __value class specific and do not apply to __nogc classes. Is this correct ? If so, how is the packing alignment of __nogc classes stored ?
6
2943
by: ivan.leben | last post by:
I want to write a Mesh class using half-edges. This class uses three other classes: Vertex, HalfEdge and Face. These classes should be linked properly in the process of building up the mesh by calling Mesh class functions. Let's say they point to each other like this: class Vertex { HalfEdge *edge; }; class HalfEdge { Vertex* vert;
0
2032
by: ivan.leben | last post by:
I am writing this in a new thread to alert that I found a solution to the problem mentioned here: http://groups.google.com/group/comp.lang.c++/browse_thread/thread/7970afaa089fd5b8 and to avoid this topic getting lost before people interested in the problem notice it. The important tricks to the solution are two: 1) make the custom classes take a TEMPLATE argument which defines their BASE class 2) EMBED the custom classes in a "Traits"...
2
1915
by: Amu | last post by:
i have a dll ( template class) ready which is written in VC++6. But presently i need to inherit its classes into my new C#.net project.so if there is some better solution with u then please give me the solution.
0
8888
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
8752
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
9257
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
0
9111
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
1
6702
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
4517
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
4782
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
3221
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
3
2157
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.