Changing the value of an Interger in another class

levyuk

Well-known member
Joined
Jun 7, 2004
Messages
313
Location
Wales, UK
Programming Experience
3-5
I have this class

Public Class Class1
Public TenToTwelve As Integer = 12
Public TwelveToTwo As Integer = 14
Public TwoToFour As Integer = 18
Public FourToSix As Integer = 18
Public SixToEight As Integer = 12
End Class

I call it in another form in this way

Dim
CodeClass As New Class1

Then I would like to write to that class and change the values but it wont work

Protected Sub ButtonUpdate_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles ButtonUpdate.Click
CodeClass.FourToSix = CInt(TextBoxFour.Text)
CodeClass.SixToEight =
CInt(TextBoxSix.Text)
CodeClass.TenToTwelve =
CInt(TextBoxTen.Text)
CodeClass.TwelveToTwo =
CInt(TextBoxTwelve.Text)
CodeClass.TwoToFour =
CInt(TextBoxTwo.Text)
End Sub

My question is, why not?
 

kulrom

Well-known member
Joined
May 10, 2005
Messages
2,854
Location
Republic of Macedonia
Programming Experience
10+
Before instantiate the class you need to declare the new class ...
PHP:
Private Sub ButtonUpdate_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ButtonUpdate.Click
 
Dim CodeClass As Class1 = New Class1 
 
CodeClass.FourToSix = CType(TextBoxFour.Text, Integer)
 
CodeClass.SixToEight = CType(TextBoxSix.Text, Integer)
 
CodeClass.TenToTwelve = CType(TextBoxTen.Text, Integer)
 
CodeClass.TwelveToTwo = CType(TextBoxTwelve.Text, Integer)
 
CodeClass.TwoToFour = CType(TextBoxTwo.Text, Integer)
 
End Sub

Cheers ;)


P.S. Please don't use vb6 functions like CInt is ... instead you have much faster CType method in VB.NET ... regards :)
 
Last edited:

kulrom

Well-known member
Joined
May 10, 2005
Messages
2,854
Location
Republic of Macedonia
Programming Experience
10+
Hi levyuk,
Maybe I've missunderstood something ... but it works for me! Take a look at attached zip file ... ;)

Btw, i'm not sure that your class is a good candidate for OOP treatment. as it deals with small data; and assume it is not reusable ... If you consider this facts to be acceptable then i was wrong :(

Cheers ;)
 

Attachments

  • Class.zip
    27.1 KB · Views: 36

jmcilhinney

VB.NET Forum Moderator
Staff member
Joined
Aug 17, 2004
Messages
14,882
Location
Sydney, Australia
Programming Experience
10+
Things don't just not work. You must get an error message at a particular line of code. What is the error message and at which line of code does it occur? With that information we can more easily help you.
 

levyuk

Well-known member
Joined
Jun 7, 2004
Messages
313
Location
Wales, UK
Programming Experience
3-5
There is no error. I'll show you the rest of the code
VB.NET:
[size=2][/size][size=2][color=#0000ff]Dim[/color][/size][size=2] CodeClass [/size][size=2][color=#0000ff]As[/color][/size][size=2] Class1 = [/size][size=2][color=#0000ff]New[/color][/size][size=2] Class1

[/size][size=2][color=#0000ff]Protected[/color][/size][size=2] [/size][size=2][color=#0000ff]Sub[/color][/size][size=2] Page_Load([/size][size=2][color=#0000ff]ByVal[/color][/size][size=2] sender [/size][size=2][color=#0000ff]As[/color][/size][size=2] [/size][size=2][color=#0000ff]Object[/color][/size][size=2], [/size][size=2][color=#0000ff]ByVal[/color][/size][size=2] e [/size][size=2][color=#0000ff]As[/color][/size][size=2] System.EventArgs) [/size][size=2][color=#0000ff]Handles[/color][/size][size=2] [/size][size=2][color=#0000ff]Me[/color][/size][size=2].Load
[/size][size=2][color=#0000ff]Me[/color][/size][size=2].TextBoxFour.Text = [/size][size=2][color=#0000ff]CType[/color][/size][size=2](CodeClass.FourToSix, [/size][size=2][color=#0000ff]String[/color][/size][size=2])
[/size][size=2][color=#0000ff]Me[/color][/size][size=2].TextBoxSix.Text = [/size][size=2][color=#0000ff]CType[/color][/size][size=2](CodeClass.SixToEight, [/size][size=2][color=#0000ff]String[/color][/size][size=2])
[/size][size=2][color=#0000ff]Me[/color][/size][size=2].TextBoxTen.Text = [/size][size=2][color=#0000ff]CType[/color][/size][size=2](CodeClass.TenToTwelve, [/size][size=2][color=#0000ff]String[/color][/size][size=2])
[/size][size=2][color=#0000ff]Me[/color][/size][size=2].TextBoxTwelve.Text = [/size][size=2][color=#0000ff]CType[/color][/size][size=2](CodeClass.TwelveToTwo, [/size][size=2][color=#0000ff]String[/color][/size][size=2])
[/size][size=2][color=#0000ff]Me[/color][/size][size=2].TextBoxTwo.Text = [/size][size=2][color=#0000ff]CType[/color][/size][size=2](CodeClass.TwoToFour, [/size][size=2][color=#0000ff]String[/color][/size][size=2])
[/size][size=2][color=#0000ff]End[/color][/size][size=2] [/size][size=2][color=#0000ff]Sub[/color][/size]
[size=2][color=#0000ff] 
[/color][/size][size=2][/size][size=2][color=#0000ff]Protected[/color][/size][size=2] [/size][size=2][color=#0000ff]Sub[/color][/size][size=2] ButtonUpdate_Click([/size][size=2][color=#0000ff]ByVal[/color][/size][size=2] sender [/size][size=2][color=#0000ff]As[/color][/size][size=2] [/size][size=2][color=#0000ff]Object[/color][/size][size=2], [/size][size=2][color=#0000ff]ByVal[/color][/size][size=2] e [/size][size=2][color=#0000ff]As[/color][/size][size=2] System.EventArgs) [/size][size=2][color=#0000ff]Handles[/color][/size][size=2] ButtonUpdate.Click
CodeClass.FourToSix = [/size][size=2][color=#0000ff]CType[/color][/size][size=2](TextBoxFour.Text, [/size][size=2][color=#0000ff]Integer[/color][/size][size=2])
CodeClass.SixToEight = [/size][size=2][color=#0000ff]CType[/color][/size][size=2](TextBoxSix.Text, [/size][size=2][color=#0000ff]Integer[/color][/size][size=2])
CodeClass.TenToTwelve = [/size][size=2][color=#0000ff]CType[/color][/size][size=2](TextBoxTen.Text, [/size][size=2][color=#0000ff]Integer[/color][/size][size=2])
CodeClass.TwelveToTwo = [/size][size=2][color=#0000ff]CType[/color][/size][size=2](TextBoxTwelve.Text, [/size][size=2][color=#0000ff]Integer[/color][/size][size=2])
CodeClass.TwoToFour = [/size][size=2][color=#0000ff]CType[/color][/size][size=2](TextBoxTwo.Text, [/size][size=2][color=#0000ff]Integer[/color][/size][size=2])
[/size][size=2][color=#0000ff]End[/color][/size][size=2] [/size][size=2][color=#0000ff]Sub
[/color][/size]

This is asp.net btw which i don't think makes that much difference but it might.
Anyway at form_load I get the information from the class. The user can then chage the value sof those the intergers and hopefully save the data. But when I click buttonupdate all I get is the textboxes showing the previous values.
 

levyuk

Well-known member
Joined
Jun 7, 2004
Messages
313
Location
Wales, UK
Programming Experience
3-5
Yes I did have a look thanks.

I noticed that the values don't remain when the form is closed. I think with asp.net that when the button is clicked the page is refreshed but because the values are not saved it shows the original values. I need a way to store the numbers even if the asp.net page is closed. I could use a database but is there another way? A simple way. Remember this is asp.net.
 

kulrom

Well-known member
Joined
May 10, 2005
Messages
2,854
Location
Republic of Macedonia
Programming Experience
10+
Hi,
The only question is do you use HTML input tag or ASP.NET textBox control ?
<input type="text">
or
<asp:TextBox id="tb1" runat="server" />
?

Hmm ... i haven't work with asp.net for long time plus i haven't installed IIS on this machine so, i cannot check Authenticity of my claim ... but if i remeber well ASP.NET textBox control remembers value/s even if you leave the current page and back later ...

Cheers ;)
 

levyuk

Well-known member
Joined
Jun 7, 2004
Messages
313
Location
Wales, UK
Programming Experience
3-5
I thought as much. I think I will have to use a database because i think it is the easiest way of storing data on a webserver. i'm not sure if I would be able to write to files such as xml because of permissions, but maybe I will try it.

Thanks for your help kulrom. P.S. where are you from?
 

kulrom

Well-known member
Joined
May 10, 2005
Messages
2,854
Location
Republic of Macedonia
Programming Experience
10+
levyuk said:
I thought as much. I think I will have to use a database because i think it is the easiest way of storing data on a webserver. i'm not sure if I would be able to write to files such as xml because of permissions, but maybe I will try it.

Thanks for your help kulrom. P.S. where are you from?

Of course, generating XML on client side would be good solution ... btw, i'm from MACEDONIA. You?

Cheers ;)
 

JuggaloBrotha

VB.NET Forum Moderator
Staff member
Joined
Jun 3, 2004
Messages
4,530
Location
Lansing, MI; USA
Programming Experience
10+
well asp code can be in vb, c#, c++ so you had the right idea, but since this is a vb forum the asp section here would naturally be all vb as well

at least it's been resolved :)
 
Top