Newbie - VB class problem.

Z

zalek

This is a problem I got writing a VB program for PocketPC, but I think
is is related to a regular VB:

I created an application that is running. Now I want to change
something. In a class form definition I see:

Public Class Form_template
Inherits System.Windows.Forms.Form
....
.......
Public gl_dot_entered As Boolean <== this statement I added
......

I want to show this form Form_template, but to overrwrite some fields.

In another form I have a code that is working:

Dim form_t As Form
form_t = New Form_template
form_t.ShowDialog()

This code is working - now I want to give different values to
gl_dot_entered member:

Dim form_t As Form
form_t = New Form_template
form_t.gl_dot_entered = False <== compilation error
form_t.ShowDialog()

Compiler is saying:

'gl_dot_entered' is not a member of 'System.Windows.Forms.Form'.

but gl_dot_entered is a member of class Form_template, that means it is
a member of the object form_t.

I know I am wrong - but where?

Thanks,

Zalek
 
T

Thommy Mewes

Dim form_t As Form
form_t = New Form_template
form_t.gl_dot_entered = False <== compilation error
form_t.ShowDialog()

Just replace

Dim form_t As Form

With

Dim form_t As Form_template

Thommy
 
P

Phill. W

Public Class Form_template
Inherits System.Windows.Forms.Form
...
Public gl_dot_entered As Boolean <== this statement I added
now I want to give different values to gl_dot_entered member:

You've hit the first hurdle of Inheritance - anything that is defined as
having a Type of XYZ can *only* do things that XYZ's can do.
Dim form_t As Form

creates a Form object, so it can only do /Form/ things.
form_t = New Form_template

This works, because your Form_Template /inherits/ from Form,
(i.e. it "is a" Form) and can, therefore, [be guaranteed to] do
anything and everything that a Form can do. It can, therefore, be
treated in exactly the same way as a Form and will "fit" into a
"Form"-shaped variable.
form_t.gl_dot_entered = False <== compilation error

A /Form/ doesn't have this property - only your /Form_Template/
has that.

There are two ways around this.

1) Change

Dim form_t As Form

to

Dim form_t As Form_Template

which will probably be good enough for most cases, or,

(2) "downcast" the Form into the correct Type, as in

Dim form_t As Form
form_t = New Form_Template ' but it's *defined* as Form, so

DirectCast( form_t, Form_Template ).gl_dot_entered = False

BTW, better still; wrap the [private] variable up in a Property and
only use that from outside your class.

HTH,
Phill W.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top