Structure gebruiken met een class

Status
Niet open voor verdere reacties.

phobia

Terugkerende gebruiker
Lid geworden
4 sep 2006
Berichten
1.777
Ik heb een Class:
Code:
Imports System.Math

Public Class Prisma

    Public Structure Prismas
        Public strength As Double
        Public Angle As Double
    End Structure

#Region "Math Calculations"
    Private Function degreesToRadians(ByVal degrees As Double) As Double
        Dim radians As Double
        radians = (Math.PI * degrees) / 180
        Return radians
    End Function

    Private Function radiansToDegrees(ByVal radians As Double) As Double
        Dim degrees As Double
        degrees = radians * (180 / PI)
        Return degrees
    End Function

    Private Function Atan2(ByVal y As Double, ByVal x As Double) As Double
        Dim theta As Double

        If (Abs(x) < 0.0000001) Then
            If (Abs(y) < 0.0000001) Then
                theta = 0.0#
            ElseIf (y > 0.0#) Then
                theta = 1.5707963267949
            Else
                theta = -1.5707963267949
            End If
        Else
            theta = Atan(y / x)

            If (x < 0) Then
                If (y >= 0.0#) Then
                    theta = 3.14159265358979 + theta
                Else
                    theta = theta - 3.14159265358979
                End If
            End If
        End If

        Atan2 = theta
    End Function
#End Region


    Public Function CalResultPrisma(ByVal P1 As Prismas, ByVal P2 As Prismas) As Prismas
        Dim Result As Prismas
        Dim P1_x, P1_y As Double
        Dim P2_x, P2_y As Double
        Dim R_x, R_y As Double
        Dim Rh As Double

        P1_x = P1.strength * Cos(degreesToRadians(P1.Angle))
        P1_y = P1.strength * Sin(degreesToRadians(P1.Angle))

        P2_x = P2.strength * Cos(degreesToRadians(P2.Angle))
        P2_y = P2.strength * Sin(degreesToRadians(P2.Angle))

        R_x = P1_x + P2_x
        R_y = P1_y + P2_y

        Result.strength = Sqrt((R_x ^ 2) + (R_y ^ 2))
        Rh = radiansToDegrees(Atan2(R_y, R_x))

        If (P1.Angle >= 180) And (P2.Angle >= 180) Then
            Rh = 360 + Rh
        ElseIf (P1.Angle <= 90) And (P2.Angle >= 270) Then
            Rh = 360 + Rh
        ElseIf (P1.Angle >= 180) And (P2.Angle <= 90) Then
            Rh = 360 + Rh
        End If

        If (Rh = 360) Then
            Rh = 0
        End If
        Result.Angle = Rh

        Return Result
    End Function
End Class

Deze wil ik gebruiken in een form.
Code:
Imports System.Math
Imports prisma.Prisma

Public Class adven

    'TODO: Create a Prisma Class

    Public Structure PrismaValues
        Public strength As Double
        Public Angle As Double
    End Structure

   Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
        Dim P1, P2, Result As PrismaValues
        Dim a As Prisma = New Prisma

        'Nieuw tbv fun ctie
        Double.TryParse(TextBox2.Text, P1.Angle)
        Double.TryParse(TextBox4.Text, P2.Angle)
        Double.TryParse(TextBox1.Text, P1.strength)
        Double.TryParse(TextBox3.Text, P2.strength)

        Result = a.CalResultPrisma(P1, P2)
        If (CheckBox1.Checked = True) Then
            Dim P3 As PrismaValues
            Double.TryParse(TextBox5.Text, P3.Angle)
            Double.TryParse(TextBox6.Text, P3.strength)
            'Add 3th prisma to calculation
            Result = a.CalResultPrisma(P3, Result)
        End If

        Label5.Text = "Prisma: " + Format(Result.strength, "Fixed") + " Angle:" + Format(Result.Angle, "Fixed")
        ClearTextboxes()

    End Sub

End Class

Maar als ik het zo doe, dan krijg ik een foutmelding dat ik de Structure PrismaValues niet kan gebruiken in de functie Result = a.CalResultPrisma(P1, P2) P1 en P2 kunnen niet

Wat doe ik fout, wat moet ik doen en waarom (Het laatste is natuurlijk belangrijk om er ook wat van te leren!)
 
Code:
Dim P1, P2, Result As PrismaValues

Public Function CalResultPrisma(ByVal P1 As Prismas, ByVal P2 As Prismas)

Je method wil Prismas hebben in plaats van PrismaValues
 
dan blijf ik de volgende error krijgen
Error 1 Value of type 'prisma.adven.Prismas' cannot be converted to 'prisma.Prisma.Prismas'.
 
In je Prisma class heb je een struct 'Prismas'. Deze vraag je ook als input parameters bij de method.
Als je dus ook je P1, P2 etc instances van deze struct aanmaakt zal het geen probleem zijn. Waarom maak je een zelfde struct aan in een andere class? Je moet de struct in de Prisma class gebruiken.

Code:
Dim P1, P2 As prisma.Prisma.Prismas
Dit zal denk ik wel werken.

of je method wijzigen dat het waardes neemt ipv structs, dan maakt het niet uit waar je waardes vandaan komen.

(misschien andere namen gebruiken? het is een beetje verwarrend)
 
Okey dat werkt.
Snap denk ik ook wel het waarom.

En dat ik een structure gebruik is omdat ik dan voor elk onderdeel al de gegevens op een plek heb en zo ook weer terug krijg.
 
Status
Niet open voor verdere reacties.
Terug
Bovenaan Onderaan