message box error

I am currently working on a program that has 2 forms : a student information form and a student cost form. I have everthing working properly except on form 2 where I press the button to calculate fees due for a student (credit hours enrolled * rate) On form 1 I enter the student's information including state. On form 2 the rate is $60 for ohio students and $80 for everyone else. The message box when I press calculate keeps saying the cost is $80 per credit hour enrolled regardless what I put in the state textbox when I debug. Anyone have any suggestions of what I can do?
[586 byte] By [missHoudat] at [2007-11-19 6:36:46]
# 1 Re: message box error
missHoudat,
could we see the code where the students rate is used, please? It's really hard to find an error whitout enough data :)
DeepButi at 2007-11-10 3:18:07 >
# 2 Re: message box error
ok here it is for form 2 so far:

Private Sub frmStudentCosts_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
Dim objStudentInformationForm As New frmStudentInformation
End Sub
Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnReturnToPreviousPage.Click
Me.Close()
End Sub

Private Sub Button3_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnReset.Click
txtCreditHoursEnrolled.Text = ""
txtFeesDue.Text = ""
End Sub

Private Sub btnCalculate_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnCalculate.Click
Dim objStudentInformationForm As New frmStudentInformation
If txtCreditHoursEnrolled.Text = "" Then
MessageBox.Show("Please Enter Credit Hours Enrolled.", "No Hours")
txtCreditHoursEnrolled.Focus()
Exit Sub
End If

If objStudentInformationForm.txtState.Text = "OH" Then
txtFeesDue.Text = Convert.ToString(txtCreditHoursEnrolled.Text * 60)
Else
txtFeesDue.Text = Convert.ToString(txtCreditHoursEnrolled.Text * 80)
End If
End Sub
End Class
missHoudat at 2007-11-10 3:19:07 >
# 3 Re: message box error
if you need the code for form one i can post it too
thanks :)
missHoudat at 2007-11-10 3:20:14 >
# 4 Re: message box error
missHoudat,
no I don't need it, your error is clear ... and very common. :)
Dim objStudentInformationForm As New frmStudentInformation
creates a NEW instance of Form1. You don't see it because you don't even show it. But in any case, objStudent is NOT your main form instance, so objStudentInformationForm.txtState.Text is empty :)
In fact you create two instances of Form1 (one at Form2 load and the other at btnCalculate clic).

You need a Form1 instance reference to use it.

Hope it helps
DeepButi at 2007-11-10 3:21:06 >
# 5 Re: message box error
how do i code the

If txtState.txt (from form 1) = "OH" then
rate = $60
Else
rate = $80
missHoudat at 2007-11-10 3:22:16 >
# 6 Re: message box error
missHoudat,
three methods (thanks to SatyaV for his suggestions (http://www.dev-archive.com/forum/showthread.php?t=328966)):

Easiest one: public variable.
' In form2
' at a global level (outside any sub/function)
Public MyParent As Form1 ' A global variable to hold the reference to your main form

' In form1, when creating form2:
Dim f2 as New Form2()
f2.MyParent = Me
f2.Show

OOP maniac one: private variable and public setter
' In form2
' at a global level (outside any sub/function)
Dim MyParent as Form1 ' A global variable to hold the reference to your main form

Public Sub SetParent(ByRef f1 As Form1)
MyParent = f1
End Sub

' In form1
Dim f2 as New Form2()
f2.SetParent(Me)
f2.Show()

Overloading constructor
' In form2
' at a global level (outside any sub/function)
Dim MyParent as Form1 ' A global variable to hold the reference to your main form

Public Sub New(ByRef f1 As Form1)
Me.New()
MyParent = f1
End Sub

' In form1
Dim f2 as New Form2(Me)
f2.Show()

In any case, you now have a variable MyParent to be used anywhere you need it in Form2:

If MyParent.txtState.Text = "OH" Then
...

Hope it helps
DeepButi at 2007-11-10 3:23:12 >
# 7 Re: message box error
Yes it helped thanks so much:)
missHoudat at 2007-11-10 3:24:16 >
# 8 Re: message box error
missHoudat,
three methods (thanks to SatyaV for his suggestions (http://www.dev-archive.com/forum/showthread.php?t=328966)):
OOP maniac one: private variable and public setter
' In form2
' at a global level (outside any sub/function)
Dim MyParent as Form1 ' A global variable to hold the reference to your main form

Public Sub SetParent(ByRef f1 As Form1)
MyParent = f1
End Sub

' In form1
Dim f2 as New Form2()
f2.SetParent(Me)
f2.Show()

Overloading constructor
' In form2
' at a global level (outside any sub/function)
Dim MyParent as Form1 ' A global variable to hold the reference to your main form

Public Sub New(ByRef f1 As Form1)
Me.New()
MyParent = f1
End Sub

' In form1
Dim f2 as New Form2(Me)
f2.Show()

Hope it helps

Hi Deepbuti,
You gave a comment in your code about 'global variable'. Well, that concept existed in VB6, that we gain by declaring it as 'Public' variable. If you meant by Public variable, then in VB.NET, data-members in classes have private accessibility; and methods are public.

The following example may clarify this concept:
Public Class Form2
Inherits System.Windows.Forms.Form
Dim a As Form1
'
'
'
Public Sub New(ByRef s As Form1)
MyBase.New()
a = s
'This call is required by the Windows Form Designer.
InitializeComponent()
'Add any initialization after the InitializeComponent() call
End Sub
'
'
'

Function check(ByRef s As Form1)
a = s
End Function
End Class

Module CheckingModule
Sub Main()
Dim f1 As New Form1
Dim f2 As Form2
'f2.a = f1
' The Above Commented Statement is According to ur Statement But its not work
'until we set the a as Public data member
'A Private Data Cannot Access Directly
'We Can Use it with Subroutine and Function of the same Class,
'Either they are Private Or they Public Does not Matter it works
f2.check(f1)
'This statement works every time whereas the data member is private or Public
End Sub
End Module

Hope u understand :)

Regards
Gurmeet :wave:
ss_gurmeet at 2007-11-10 3:25:19 >
# 9 Re: message box error
ss_gurmeet,
for global I meant known everywhere in the Class, defined outside any Function/Sub. Although probably not an exact word I used it as an easy way to make it clear to people coming to .NET

Nothing to do with Public/Private.

In two of my three methods the variable is private (declared with a Dim) because there is no need for it to be known outside the class.

In all three methods the variable is "global" to allow it to be used anywhere in the Class.

f2.a in your code does not work because you use it outside the class, in a module.

Also, your code for the overloaded constructor would be better calling Me.New() and getting ride of the InitializeComponent statement. No need to know what is done in the standard constructor with Me.New().

With a Public variable, anyone outside the Class can use/modify it (see the example of the "easiest one" method). Not extremely dangerous, but non-OOP anyway.
DeepButi at 2007-11-10 3:26:14 >
# 10 Re: message box error
and methods are public.
No. Methods, as data, are Public or Private depending on how do you declare them:
Public Sub PublicOne()
...
End Sub

Private Sub PrivateOne()
...
End SubTry to use f2.PrivateOne in Form1 code. You cannot because is Private.

Hope it helps
DeepButi at 2007-11-10 3:27:12 >
# 11 Re: message box error
Module CheckingModule
Sub Main()
Dim f1 As New Form1
Dim f2 As Form2
'f2.a = f1
' The Above Commented Statement is According to ur Statement But its not work
'until we set the a as Public data member
'A Private Data Cannot Access Directly
'We Can Use it with Subroutine and Function of the same Class,
'Either they are Private Or they Public Does not Matter it works
f2.check(f1)
'This statement works every time whereas the data member is private or Public
End Sub
End ModuleWell, this code will throw exception at run time. F2 is not initialized with New, neither set reference to any existing object.In all three methods the variable is "global" to allow it to be used anywhere in the Class.

f2.a in your code does not work because you use it outside the class, in a module.You are right, that variable declared at class scope will be avialable to all of its methods, irrespective of access mode. But, can you tell which line in ss_gurmeet's code wont work?Also, your code for the overloaded constructor would be better calling Me.New() and getting ride of the InitializeComponent statement. No need to know what is done in the standard constructor with Me.New().No, you are not correct either. First, you cannot just call the base' class' constructor, if you have overloaded another one in derived class. Second, InitializeComponent is necessary whether you overload (overridable overload) or not. Finally, it is absolutely necessary to call the base class' constructor (New) from the derived's CTOR, as the very first statement of derived constructor.

I hope you should test the things before putting your Knowledge-Base into threads. ;)
Ajay Vijay at 2007-11-10 3:28:21 >
# 12 Re: message box error
No. Methods, as data, are Public or Private depending on how do you declare them:Well, each language has its own semantics. C++ has everything as 'private' in classes, 'public' in structures. In VB.NET, as Gurmeet exploited, methods are Public, and data-members are Private by default in Classes. Furthermore, everything in Modules are public.
Ajay Vijay at 2007-11-10 3:29:17 >
# 13 Re: message box error
But, can you tell which line in ss_gurmeet's code wont work?
the commented line f2.a will not work (we all agree here, isn't it?).
No, you are not correct either. First, you cannot just call the base' class' constructor, if you have overloaded another one in derived class. Second, InitializeComponent is necessary whether you overload (overridable overload) or not. Finally, it is absolutely necessary to call the base class' constructor (New) from the derived's CTOR, as the very first statement of derived constructor.

I hope you should test the things before putting your Knowledge-Base into threads. :thumb: :D
I'm not a guru, just try to help other, but I almost allways test it before posting ... and I must disagree with you.
The call to Me.New() works. For sure.
I cannot see why it shouldn't.
Overloaded means (as far as I understand) that there are several "versions" of the method with different parameters. The compiler knows wich one you intend to use by matching the parameter number and types.
Another case would be an overrided method ... but that's not the case.
I tested it and it works.

Thanks for your comments
DeepButi at 2007-11-10 3:30:21 >
# 14 Re: message box error
the commented line f2.a will not work (we all agree here, isn't it?).Well, have u looked following comment around that commented statement?'f2.a = f1
' The Above Commented Statement is According to ur Statement But its not work
'until we set the a as Public data member
'A Private Data Cannot Access Directly
'We Can Use it with Subroutine and Function of the same Class,
'Either they are Private Or they Public Does not Matter it works
Ajay Vijay at 2007-11-10 3:31:15 >
# 15 Re: message box error
I'm not a guru, just try to help other, but I almost allways test it before posting ... and I must disagree with you.
The call to Me.New() works. For sure.
I cannot see why it shouldn't.
Overloaded means (as far as I understand) that there are several "versions" of the method with different parameters. The compiler knows wich one you intend to use by matching the parameter number and types.
Another case would be an overrided method ... but that's not the case.
I tested it and it works.Well, let clear the cofusion: I just said, we cannot call the base class's constructor while creating the derived object. That is:Class Base
Public Sub New()

End Sub
End class

Class Derived
Inherits Base
Public Sub New(nArg as Integer)

End Sub
End Class
...
Dim x as Derived
x = new Derived ' THIS IS ILLIGAL
x= new Derived(10) ' WE MUST TO PASS ARGUMENT Secondly, calling base class' New is required so that base class it created property. It is absolutely, if base' class needs arguments with CTOR.
Ajay Vijay at 2007-11-10 3:32:16 >
# 16 Re: message box error
Ajay Vijay,
we are probably saying the same with different words.

ss_gurmet overloaded constructor:
Public Sub New(ByRef f As Form1)
MyBase.New()
InitializeComponents()
...
End SubHe calls the base MyBase.New class constructor. Additionally he must call InitializeComponents.

DeepButi overloaded constructor.
Public Sub New(ByRef f As Form1)
Me.New()
...
End Sub
Me.New, not MyBase.New. Open the region generated by the designer and look at New method there. It calls MyBase.New and InitializeComponents.

There are three New methods:
MyBase.New()
Me.New()
Me.New(ByRef f As Form1)

I'm calling Me.New(), wich in turn calls MyBase.New().

I think it's easier to call Me.New without having to know what does it do. I simply add my requeriments and let the base constructors do its job.

Now, you can do both legally. One of them will call the modified one where code has been added; the other will call the standard one:

Dim f2 as New Form2(Me) ' it uses Deepbuti overloaded constructor
Dim f3 as New Form2() ' it uses the standard constructor created by the designer

Hope I made my method clear ... and thanks for all your comments.
DeepButi at 2007-11-10 3:33:17 >
# 17 Re: message box error
Well, have u looked following comment around that commented statement?'f2.a = f1
' The Above Commented Statement is According to ur Statement But its not work
'until we set the a as Public data member
'A Private Data Cannot Access Directly
'We Can Use it with Subroutine and Function of the same Class,
'Either they are Private Or they Public Does not Matter it works
Yes, I have looked at the comment and I disagree.
He says
' The Above Commented Statement is According to ur Statement
and I'm trying to explain him that this statement IS NOT ACCORDING to my text.
A class scope variable (I used erroneously the word "global", sorry for it) can be used in code inside the class. Not in a module outside the class, this effectively requires a Public variable. So it doesn't work because it shouldn't do it.

In all, I cannot see any error in my code except for using "global" instead of "class scope" (in a comment!!!). I think new .NETers will find "global" clearer, but OK, I assume my mistake.
See please the original post where three methods for having a reference to main form are explained for people coming new to .NET. All three work. All three are correct. I tried to make them easy and clear.

But, obviously I can be wrong.

Thanks again
DeepButi at 2007-11-10 3:34:26 >
# 18 Re: message box error
Well, these two terms are somewhat different:Me.Method()
and MyBase.Method()For instace, you have method named Method which can be overridable by derived class (runtime polymorphism). But, in case if base class calls that method, then? Answer is simple the re-implemented method in derived class will be called (if implemented).

But you want to avoid that situation and need to call the base' class version (within base class code), then you would use MyClass.Method.

Furthermore, if a method is re-defined with same method signature (method overloading), and you call that method in derived class - then which will be called? Answer is again simple, the derived one. But you need to, explicity call base class' version. Then you do same by MyBase.Method

Finally, calling Method and Me.Method are exactly same, but no-way same as either MyBase.Method or MyClass.Method (May be same, but depends on Inheritance structure and the method being called, from where).
Ajay Vijay at 2007-11-10 3:35:28 >
# 19 Re: message box error
But, obviously I can be wrong.

Thanks againHey.. Chill! :)
No one is expert here and everyone is learning... :thumb:
Ajay Vijay at 2007-11-10 3:36:23 >
# 20 Re: message box error
Well, this code will throw exception at run time. F2 is not initialized with New, neither set reference to any existing object.Hi Ajay Vijay,
Ooops! :ehh: Well you are right Ajay i agree with ur statement i make a silly mistake here. its left when i m typing in the reply window. But I Accept its a mistake. i m a new comer of Vb.net and also on dev-archive. i m not a Guru of dev-archive like You. I m only showing DeepButi that he is using Wrong word in his Comment.
Well i want to type this
Dim f2 as New Form2

Thanks for giving me Comment :)
Regards
Gurmeet
ss_gurmeet at 2007-11-10 3:37:25 >
# 21 Re: message box error
okey. Let's finish it.

I used a common english word "global" instead of a specific one "class scope" in a comment (wow! what a great mistake ...) without being aware that VB6 people could be confused by it. I apologize for it.

My code was tested before posting it. It works, is correct and fullfills O.P. needs.
It solves correctly a problem for a newbie. No theorics about polymorfism, writting whole new classes or anything else. Just practical, correct, usable code.

If someone can point out a single mistake in my code I would be glad to listen and learn.
If someone can show new, different ways, I would also be glad to listen and learn.

*I hope you should test the things before putting your Knowledge-Base into threads.
I consider this sentence, as a minimum, unpolite. Maybe an Elite member, with 2.000+ posts and a "jewel in the rough" doesn't need to be polite. Or maybe I'm a little bit susceptible (correct word?), but I cannot see any reason to try to show me as someone that cann't be trusted.

I'm allways polite and I expect others to treat me with the same respect.

Thanks to everyone.

PS. I would accept without a word a moderator deleting this post, but I claim my rigths to defend my (small) reputation.
DeepButi at 2007-11-10 3:38:28 >
# 22 Re: message box error
I consider this sentence, as a minimum, unpolite. Maybe an Elite member, with 2.000+ posts and a "jewel in the rough" doesn't need to be polite. Or maybe I'm a little bit susceptible (correct word?), but I cannot see any reason to try to show me as someone that cann't be trusted.Hey dear...
My intention was not to be impolite anyway, and I changed the icons after that statement that may have hurted you. I deeply regret it... :o :blush:
Ajay Vijay at 2007-11-10 3:39:30 >