Adam Coulson
Adam Coulson

Reputation: 43

Dims being used by .class File

I have this and I want to use something similar to the way Java uses .class files to be able to call events and use them in my main code.

The problem is that I cannot get the .class file to use my Dims

Form1.vb:

Namespace LFS_External_Client
    Public Class Form1
        Inherits Form

        Private OutGauge As OutGaugeInterface

        Dim SpeedPref As String
        Dim FuelCapacity As String
        Dim Fuel As String

        Public Sub New()
            InitializeComponent()
        End Sub

        Private Sub Form1_Load() Handles MyBase.Load
            Some Code
            GetFuel()
        End Sub
    End Class
End Namespace

Then in the Dataproccer.vb (.class file):

Public Class DataProcesser
    Public Sub GetFuel()
        Some Code
        Fuel = og.Fuel.ToString() * FuelCapacity
    End Sub
End Class

Code was shortened but has all of the relevant and necessary parts.

Upvotes: 0

Views: 112

Answers (4)

codechurn
codechurn

Reputation: 3970

You can make SpeedPref, FuelCapacity and Fuel public member variables, however a better approach would be to make them properties on the class with appropriate getters and setters. Dim just declares a variable. Please see the modified code sample below:

Form1.vb:
Namespace LFS_External_Client
Public Class Form1
    Inherits Form

    Private OutGauge As OutGaugeInterface

    Private _SpeedPref As String
    Private _FuelCapacity As String
    Private _Fuel As String

    Public Property SpeedPref
        Get
            return _SpeedPref
        End Get
        Set(value As String)
            _SpeedPref = value 
        End Set
    End Property

    ...

End Class
End Namespace

Upvotes: 0

Mark Hall
Mark Hall

Reputation: 54532

Looking at the MSDN page for the Dim Statement.

It states:

Code outside a class, structure, or module must qualify a member variable's name with the name of that class, structure, or module. Code outside a procedure or block cannot refer to any local variables within that procedure or block.

Also according to this MSDN article the default access level for the Dim Statement is Private at the Module Level.

So why not make GetFuel a function and pass the FuelCapacity in like @kcBeard states and return the Fuel value.

Private Sub Form1_Load() Handles MyBase.Load    
    Some Code    
    Fuel = DataProcesser.GetFuel(FuelCapacity)    
End Sub    

Public Shared Function GetFuel(Byval FuelCapacity as string) as string
    Some Code
    return og.Fuel.ToString() * FuelCapacity
End Function

Upvotes: 1

BlackICE
BlackICE

Reputation: 8926

If you want to use the actual variables from the form instead of passing them through the method calls, you would need to declare them public instead of using dim:

...
Private OutGauge As OutGaugeInterface

Public SpeedPref As String
Public FuelCapacity As String
Public Fuel As String
...

Upvotes: 2

Keith Beard
Keith Beard

Reputation: 1611

Dim FuelCapacity As String

Private Sub Form1_Load() Handles MyBase.Load
            Some Code
            DataProcesser.GetFuel(FuelCapacity)
End Sub

Public Shared Sub GetFuel(Byval FuelCapacity as string)
        Some Code
        Fuel = og.Fuel.ToString() * FuelCapacity
End Sub

Upvotes: 1

Related Questions