Converting a 4 argument class to int?

In summary: Private Property m_ReadOnly As Boolean End Property Write only in VB6/VBA:'...Private Property m_WriteOnly As Boolean End PropertyIn summary, in VB.Net you can set a property to be either read only or write only. In VB6/VBA you can also make a property read only.
  • #1
pazmush
32
0
i was wondering whether it was possible to have a class say Vec4(i,j,,k,n) and then being able to assign it as an int

eg/

Vec4 A(i,j,k,1) = 1;


does this make any sense?
 
Technology news on Phys.org
  • #2
I'm not quite sure what language you're talking about (C,C++,C#?), but I think you can only check the return with "function = 1" unless you're setting a property in which case "A" is the property and "i,j,k,1" are the arguments.

I only know VB well, so I can tell you that's the way it works in there.
 
  • #3
i am using visual basic, but i don't really understand what you are saying, do you reacon you could break it down a bit for me


thanks
 
  • #4
pazmush said:
i am using visual basic, but i don't really understand what you are saying, do you reacon you could break it down a bit for me thanks

Surely.

I'm trying to wrap my head around what you're asking.

What you typed, to my knowledge, will not work. If you can explain what you're trying to do, I can get a better idea of it.Note: I'm going to describe both the VB.Net version and VB6/VBA version of properties, feel free to skip the property stuff.

Note2: If any of this is incorrect, please someone feel free to correct me, I don't know everything and only novice to intermediate expertise in .Net or VB6. In other words, someone confirm this for me. :redface:

In property terms of what I was thinking you could do is something like this:
Code:
'Random class/random function
dim Vec4 as Class
Vec4.A(i,j,k,l) = 1

or you can check the return function

Code:
'Random class/random function
Dim Vec4 as Class
if Vec4.A(i,j,k,l) = 1 then
...
end if

The property stuff:
According to Wikipedia a property is a intermediary between a private member variable and the outside world. (Code examples from same link).

VB6/VBA:
Code:
' in a class named clsPen
Private m_Color As Long 'Private member variable
 
Public Property Get Color() As Long 'Public reading function
    Color = m_Color
End Property
 
Public Property Let Color(ByVal RHS As Long) 'Public writing procedure
    m_Color = RHS
End Property

VB.Net:
Code:
' in a class named clsPen
Private m_Color As Integer ' Private field
 
    Public Property Color As Integer ' Public property
        Get
            Return m_Color 'Public reading function
        End Get
        Set(ByVal Value As Integer) 
            m_Color = Value 'Public writing procedure
        End Set
    End Property

All you see as the developer when you're writing to clsPen.Color is:
Writing:
Code:
 clsPen.Color=4
Reading:
Code:
 A = clsPen.Color

As mentioned in the link, properties adds a level abstractness between the outside code and the private member, so you can actually check to make sure that the value passed is within a range or is a color. You can do this by using built in functions or outside functions.

Validating information to set is a number:
Valdidating in VB6/VBA:
Code:
' in a class named clsPen
Private m_Color As Long 'Private member variable
 
Public Property Get Color() As Long 'Public reading function
    Color = m_Color
End Property
 
Public Property Let Color(ByVal RHS As Object) 'Public writing procedure
'Long changed to object to allow more abstractness to what is sent to it
' without throwing error, not necessarily correct, but shows the functionality
' of the Let statement
    If IsNumeric(m_Color) then m_Color = RHS
    'Or
    'If TypeName(m_Color) = "Long" then m_Color = RHS
End Property

Validating in VB.Net:
Code:
' in a class named clsPen
Private m_Color As Integer ' Private field
 
    Public Property Color As Object' Public property
        Get
            Return CObj(m_Color) 'Public reading function 
'(CObj converts the integer to an Object)
        End Get
        Set(ByVal Value As Object)'Public writing procedure
            If IsNumeric(Value) then m_Color = Value
            'Or the more proper way
            'If TypeOf(Value) Is Integer then m_Value = Value 
'TypeOf is better than TypeName (imho) because TypeOf is
' returning the value to be compared against a static value
' rather than the string name. I use TypeOf all the time to 
' verify things in control loops. Remember use "Is" between
' objects and "=" between everything else to verify it
        End Set
    End Property
Also, using properties you can make a property read only or write only.
Read only in VB6/VBA:
Code:
' in a class named clsPen
Private m_Color As Long 'Private member variable
 
Public Property Get Color() As Long 'Public reading function
    Color = m_Color
End Property

Write only in VB6/VBA:
Code:
' in a class named clsPen
Private m_Color As Long 'Private member variable
 
Public Property Let Color(ByVal RHS As Long) 'Public writing procedure
    m_Color = RHS
End Property

Read only in VB.Net:
Code:
' in a class named clsPen
Private m_Color As Integer ' Private field
 
    Public ReadOnly Property Color As Integer ' Public property
        Get
            Return m_Color 'Public reading function
        End Get
    End Property

Write only in VB.Net
Code:
' in a class named clsPen
Private m_Color As Integer ' Private field
 
    Public WriteOnly Property Color As Integer ' Public property
        Set(ByVal Value As Integer)'Public writing procedure
            m_Color = Value
        End Set
    End Property

HTH
 

1. How do I convert a 4 argument class to an int?

To convert a 4 argument class to an int, you can use the built-in method __int__(). This method should be defined within your class and should return an integer representation of your class.

2. What are the 4 arguments in a 4 argument class?

The 4 arguments in a 4 argument class refer to the 4 parameters or attributes that are passed into the class when it is instantiated. These arguments are typically used to initialize the class and define its properties.

3. Can I convert a 4 argument class to other data types?

Yes, you can convert a 4 argument class to other data types such as strings, floats, or even custom data types. This can be done by defining appropriate methods within your class, such as __str__() for converting to a string or __float__() for converting to a float.

4. Is it possible to convert a 4 argument class to an int without using the __int__() method?

No, the __int__() method is necessary for converting a 4 argument class to an int. This method is automatically called when the built-in int() function is used on an instance of your class.

5. What happens if the __int__() method is not defined in my 4 argument class?

If the __int__() method is not defined in your 4 argument class, attempting to convert it to an int will result in an error. It is important to define this method in your class if you intend to convert it to an int or use it in any arithmetic operations.

Similar threads

  • Programming and Computer Science
Replies
3
Views
1K
  • Programming and Computer Science
2
Replies
36
Views
3K
  • Programming and Computer Science
2
Replies
35
Views
2K
  • Programming and Computer Science
2
Replies
55
Views
4K
  • Programming and Computer Science
2
Replies
52
Views
3K
  • Programming and Computer Science
2
Replies
36
Views
2K
  • Programming and Computer Science
Replies
17
Views
1K
  • Programming and Computer Science
Replies
15
Views
3K
  • Programming and Computer Science
3
Replies
75
Views
4K
  • Programming and Computer Science
Replies
4
Views
1K
Back
Top