I spent several hours yesterday trying to fix an error with one of my classes. I have a clas which handles all the resources of a game, and they are all declared as public. When a function of the same rseource class is called by another class, its meant to display the values of some of my variables. I did all the nessecary code, yet the function only picks up the default value of all the resources, which is 0, when it constantly changes throughout runtime. Does anyone have any suggestions?


7 Years
Discussion Span
Last Post by nexocentric

Would it be possible for you to post the code for the class in question and the function that should be getting the value of the resources?



Sure, here is the code. (Irrelivant parts have been removed.)

Public Class SenateTown
Inherits Form
Dim Resource As New Resource

Public Sub SenateTownClick(ByVal sender As Object, ByVal e As System.EventArgs) Handles MyBase.Click

If Cursor.Position.X > SenateSquarePos.X And Cursor.Position.X < SenateSquarePos.X + 254 And Cursor.Position.Y > SenateSquarePos.Y And Cursor.Position.Y < SenateSquarePos.Y + 209 Then
Call Resource.OpenSMarket()
End If

End class

Public Class Resource
Public strSBuildingResource As String
Public strSWeapon As String
Public strSStapleFood As String
Public strSLuxuryFood As String
Public strSGold As String
Public strPCBuildingResource As String
Public strPCWeapon As String
Public strPCLuxuryFood As String
Public strPCStapleFood As String
Public strPCGold As String
Public SBuildingResource As Integer
Public SWeapon As Integer
Public SStapleFood As Integer
Public SLuxuryFood As Integer
Public SGold As Integer = 10000
Public PCBuildingResource As Integer
Public PCWeapon As Integer
Public PCStapleFood As Integer
Public PCLuxuryFood As Integer
Public PCGold As Integer = 1000
Friend WithEvents PCBR As System.Windows.Forms.Label
Friend WithEvents PCW As System.Windows.Forms.Label
Friend WithEvents PCSF As System.Windows.Forms.Label
Friend WithEvents PCLF As System.Windows.Forms.Label
Friend WithEvents PCG As System.Windows.Forms.Label
Friend WithEvents SBR As System.Windows.Forms.Label
Friend WithEvents SW As System.Windows.Forms.Label
Friend WithEvents SSF As System.Windows.Forms.Label
Friend WithEvents SLF As System.Windows.Forms.Label
Friend WithEvents SG As System.Windows.Forms.Label

Public Sub OpenSMarket()
Dim MarketMenu As New Form
Dim PCBRpos, PCWpos, PCSFpos, PCLFpos, PCGpos, SBRpos, SSFpos, SLFpos, SWpos, SGpos As Point
PCBRpos.X = 192
PCBRpos.Y = 48
PCWpos.X = 192
PCWpos.Y = 80
PCSFpos.X = 192
PCSFpos.Y = 104
PCLFpos.X = 192
PCLFpos.Y = 136
PCGpos.X = 192
PCGpos.Y = 160
SBRpos.X = 192
SBRpos.Y = 184
SWpos.X = 192
SWpos.Y = 208
SSFpos.X = 192
SSFpos.Y = 232
SLFpos.X = 192
SLFpos.Y = 256
SGpos.X = 192
SGpos.Y = 280
MarketMenu.Enabled = True
MarketMenu.Visible = True
MarketMenu.Width = 344
MarketMenu.Height = 348
Dim MarketMenuText As New Bitmap("MarketMenuText.bmp")
Dim M As Graphics
M = MarketMenu.CreateGraphics
M.DrawImage(MarketMenuText, 0, 0)
Me.tmrTest = New System.Windows.Forms.Timer
Me.PCBR = New System.Windows.Forms.Label
Me.PCW = New System.Windows.Forms.Label
Me.PCSF = New System.Windows.Forms.Label
Me.PCLF = New System.Windows.Forms.Label
Me.PCG = New System.Windows.Forms.Label
Me.SBR = New System.Windows.Forms.Label
Me.SW = New System.Windows.Forms.Label
Me.SSF = New System.Windows.Forms.Label
Me.SLF = New System.Windows.Forms.Label
Me.SG = New System.Windows.Forms.Label
Me.PCBR.Size = New System.Drawing.Size(40, 23)
Me.PCW.Size = New System.Drawing.Size(40, 23)
Me.PCSF.Size = New System.Drawing.Size(40, 23)
Me.PCLF.Size = New System.Drawing.Size(40, 23)
Me.PCG.Size = New System.Drawing.Size(40, 23)
Me.SBR.Size = New System.Drawing.Size(40, 23)
Me.SW.Size = New System.Drawing.Size(40, 23)
Me.SSF.Size = New System.Drawing.Size(40, 23)
Me.SLF.Size = New System.Drawing.Size(40, 23)
Me.SG.Size = New System.Drawing.Size(40, 23)
Me.tmrTest.Interval = 1000
Me.PCBR.Name = "PCBR"
Me.PCW.Name = "PCW"
Me.PCSF.Name = "PCSF"
Me.PCLF.Name = "PCLF"
Me.PCG.Name = "PCG"
Me.SBR.Name = "SBR"
Me.SW.Name = "SW"
Me.SSF.Name = "SSF"
Me.SLF.Name = "SLF"
Me.SG.Name = "SG"
strPCBuildingResource = PCBuildingResource
strPCGold = PCGold
strPCStapleFood = PCStapleFood
strPCLuxuryFood = PCLuxuryFood
strPCWeapon = PCWeapon
strSBuildingResource = SBuildingResource
strSGold = SGold
strSStapleFood = SStapleFood
strSLuxuryFood = SLuxuryFood
strSWeapon = SWeapon
Me.PCBR.Text = strPCBuildingResource
Me.PCW.Text = strPCWeapon
Me.PCSF.Text = strPCStapleFood
Me.PCLF.Text = strPCLuxuryFood
Me.PCG.Text = strPCGold
Me.SBR.Text = strSBuildingResource
Me.SW.Text = strSWeapon
Me.SSF.Text = strSStapleFood
Me.SLF.Text = strSLuxuryFood
Me.SG.Text = strSGold
Me.PCBR.Location = PCBRpos
Me.PCW.Location = PCWpos
Me.PCSF.Location = PCSFpos
Me.PCLF.Location = PCLFpos
Me.PCG.Location = PCGpos
Me.SBR.Location = SBRpos
Me.SSF.Location = SSFpos
Me.SLF.Location = SLFpos
Me.SW.Location = SWpos
Me.SG.Location = SGpos
If MarketMenu.Enabled = True Then
tmrTest.Enabled = True
End If
End Sub
End class


I assume that you're trying to find out what values were set after you called Resource.OpenMarket.

I don't know if VB allows this or not, but in most programming languages you can't have a Class (let's think of this as a blueprint) and an object(thing made from the blueprints) of the same name.

It might be better to do
Dim Resource1 As new Resource

Did you try accessing the any of the members of Resource after you called Resource.OpenMarket()?

A class is just a blueprint for making an object that you want to use, so if you want to know what is inside the new object you have to access using the object you made.

For example

If you mouse over this then you should get what was stored by Resource1; however if you mouse over any of the parts between

Public Class Resource
End Class

You're not going to get any useful information because that isn't the actual object. It's just the blueprint.

I found a fairly decent tutorial on the nets. It talks about making classes and using get and set to access them as well.

I hope that I was of some help.



Thankyou, I will try out your idea and look at the link.

The class SenateTown was meant to open resource.openmarket, which is a new form window showing market prices of the resource variables.


Good to hear!

Also the next time that you post code if you highlight the code that you're posting and click the CODE button, the site will format the code so that it's easy for others to read.

This question has already been answered. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.