473,725 Members | 2,118 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 1495
I am having trouble finding uderstanding why using a public variable instead
of a property with get and set is bad programming practice. In fact, there
are a lot of properties that don't do anything that requres a get or set and
it's much easier to accumulate these just below the class statement as public
variables as you dont' have to scroll down thru a lot of screens to read the
code.

Each to his own I guess. What's good programming practice to one is not to
another.
--
Dennis in Houston
"rowe_newsgroup s" wrote:
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.EventArg s) Handles Button1.Click
Dim WOLF As New DOG
WOLF.AGE = 10
End Sub
End Class
Regards Brian
>
>
>
>

Sep 20 '06 #21
Thanks rowe and others for your help.

I know the basics of programming in Visual Basic but I want to get a
more deeper understanding of this language.

Regards Brian
"rowe_newsgroup s" <ro********@yah oo.comwrote:
>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....wh y 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 #22
I am having trouble finding uderstanding why using a public variable instead
of a property with get and set is bad programming practice. In fact, there
are a lot of properties that don't do anything that requres a get or set
In my experience classes tend to "morph" over time. This includes
inheriting the class and overriding various methods. Or adding
overloading versions of properties. Or implementing try catch blocks.
And so on and so on. All of the above examples are common things that
are done with/to classes, and none are possible with with variables.
This is why using properties can make your life (and your fellow
coder's lives) much easier in the long run. It's kind of like the
object orientated programming ideals, it is nothing more than a set of
good ideas. You don't need to use any of them to make good programs,
and in many causes it takes longer to build the program and requires
more code. But in the long run OOP makes things much easier on you,
through code reuse and everything else it offers. I guess you could say
using properties is more conformant to OOP standards than using
variables.
it's much easier to accumulate these just below the class statement as public
variables as you dont' have to scroll down thru a lot of screens to read the
code.
That's what the new outlining feature is for! Besides if you're just
using standard get set property blocks why do you need to read through
the code anyway? I say just type up the property, hit the " - " button
to hide it, and forget it.

Thanks,

Seth Rowe

Dennis wrote:
I am having trouble finding uderstanding why using a public variable instead
of a property with get and set is bad programming practice. In fact, there
are a lot of properties that don't do anything that requres a get or set and
it's much easier to accumulate these just below the class statement as public
variables as you dont' have to scroll down thru a lot of screens to read the
code.

Each to his own I guess. What's good programming practice to one is not to
another.
--
Dennis in Houston
"rowe_newsgroup s" wrote:
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 #23
Hello rowe_newsgroups ,

Seth is entirely correct in his assesment. I work on medium to large projects
everyday. Properties are a must. The code becomes unmaintainable without
them. The instant you have to add error or bounds checking, data transformations ,
or simply causing something to be readonly.. all common things to do with
properties, you've broken your interface. Everyone was hitting Class.Variable. ..
now.. we add bounds checking, and the business rules say the bounds checking
is manditory.. so now we MUST hide the Variable (since it can't provide the
checks).. now.. that class uses the Variable extensively.. do we rename the
variable, and expose a property of its old name? Or do we leave the variable
and create a property of a new name? Either way somebody's code is gonna
have to change. No. We do things right from the start and anything exposed
through the public interface is done via proeprties, not variables.

-Boo
>I am having trouble finding uderstanding why using a public variable
instead of a property with get and set is bad programming practice.
In fact, there are a lot of properties that don't do anything that
requres a get or set
In my experience classes tend to "morph" over time. This includes
inheriting the class and overriding various methods. Or adding
overloading versions of properties. Or implementing try catch blocks.
And so on and so on. All of the above examples are common things that
are done with/to classes, and none are possible with with variables.
This is why using properties can make your life (and your fellow
coder's lives) much easier in the long run. It's kind of like the
object orientated programming ideals, it is nothing more than a set of
good ideas. You don't need to use any of them to make good programs,
and in many causes it takes longer to build the program and requires
more code. But in the long run OOP makes things much easier on you,
through code reuse and everything else it offers. I guess you could
say using properties is more conformant to OOP standards than using
variables.
>it's much easier to accumulate these just below the class statement
as public variables as you dont' have to scroll down thru a lot of
screens to read the code.
That's what the new outlining feature is for! Besides if you're just
using standard get set property blocks why do you need to read through
the code anyway? I say just type up the property, hit the " - " button
to hide it, and forget it.

Thanks,

Seth Rowe

Dennis wrote:
>I am having trouble finding uderstanding why using a public variable
instead of a property with get and set is bad programming practice.
In fact, there are a lot of properties that don't do anything that
requres a get or set and it's much easier to accumulate these just
below the class statement as public variables as you dont' have to
scroll down thru a lot of screens to read the code.

Each to his own I guess. What's good programming practice to one is
not to
another.
--
Dennis in Houston
"rowe_newsgrou ps" wrote:
>>>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.Eve ntArgs) 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.Ev entArgs) Handles Button1.Click
>>Dim WOLF As New DOG
>>WOLF.AG E = 10
>>End Sub
>>End Class
>>Regards Brian

Sep 21 '06 #24
Hello rowe_newsgroups ,

Protected is also used for sharing variables with inherited classes.
Nested classes are something to be avoided like the plague.

-Boo

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
circumstanc es 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
circumstance s 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 21 '06 #25
Protected is also used for sharing variables with inherited classes.

Thanks for adding that, I left it out of my original post.
Nested classes are something to be avoided like the plague.
I agree, about the only time I nest classes is to create very specific
custom exceptions that don't make sense apart from the "host" class.

Thanks,

Seth Rowe

GhostInAK wrote:
Hello rowe_newsgroups ,

Protected is also used for sharing variables with inherited classes.
Nested classes are something to be avoided like the plague.

-Boo

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
circumstance s 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 21 '06 #26

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
9401
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
1
9176
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
8097
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
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
6011
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
4519
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
4784
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
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.