B4J Question Is there way have option of type parameter?

Theera

Well-Known Member
Licensed User
Longtime User
Hi Erel,
I had finished jThaiDate Library ,but I need to know addtion in my case,I need imonth could to be as String or Int. Is there way?

P.S. In order that I could use Only DateTime without DateUtils or use the both of them.

B4X:
Public Sub ThaiMonth(imonth As String or imonth as Int ) As String
  Select imonth
      Case "January" or 1
              Return "มกราคม"
      Case "February" or 2
              Return "กุมภาพันธ์"
      Case "March" or 3
              Return "มีนาคม"
      Case "April" or 4
              Return "เมษายน"
      Case "May" or 5
              Return "พฤษภาคม"
      Case "June" or 6
              Return "มิถุนายน"
      Case "July" or 7
              Return "กรกฎาคม"
      Case "August" or 8
              Return "สิงหาคม"
      Case "September" or 9
              Return "กันยายน"
      Case "October" or 10
              Return "ตุลาคม"
      Case "November" or 11
              Return "พฤศจิกายน"
      Case "December" or 12
              Return "ธันวาคม"
  End Select
End Sub
 

stevel05

Expert
Licensed User
Longtime User
This will work for the select items. I'm not sure what you mean by
In order that I could use Only DateTime without DateUtils or use the both of them.

B4X:
Public Sub ThaiMonth(imonth As Object) As String

    If imonth Is String Then
      Select imonth
          Case "January"
                  Return "มกราคม"
          Case "February"
                  Return "กุมภาพันธ์"
          Case "March"
                  Return "มีนาคม"
          Case "April"
                  Return "เมษายน"
          Case "May"
                  Return "พฤษภาคม"
          Case "June"
                  Return "มิถุนายน"
          Case "July"
                  Return "กรกฎาคม"
          Case "August"
                  Return "สิงหาคม"
          Case "September"
                  Return "กันยายน"
          Case "October"
                  Return "ตุลาคม"
          Case "November"
                  Return "พฤศจิกายน"
          Case "December"
                  Return "ธันวาคม"
      End Select
    Else
        Dim imInt As Int = imonth

        Select imInt
       
          Case 1
                  Return "มกราคม"
          Case 2
                  Return "กุมภาพันธ์"
          Case 3
                  Return "มีนาคม"
          Case 4
                  Return "เมษายน"
          Case 5
                  Return "พฤษภาคม"
          Case 6
                  Return "มิถุนายน"
          Case 7
                  Return "กรกฎาคม"
          Case 8
                  Return "สิงหาคม"
          Case 9
                  Return "กันยายน"
          Case 10
                  Return "ตุลาคม"
          Case 11
                  Return "พฤศจิกายน"
          Case 12
                  Return "ธันวาคม"
      End Select
    End If
   
End Sub
 
Upvote 0

Daestrum

Expert
Licensed User
Longtime User
Leave imonth as String, and change the case statements to:

case "January","1"
.....
case "February","2"
.......
 
Upvote 0

stevel05

Expert
Licensed User
Longtime User
Leave imonth as String, and change the case statements to:

case "January","1"
.....
case "February","2"
.......


I thought I tried that:rolleyes: must be time for a coffee!
 
Upvote 0

Daestrum

Expert
Licensed User
Longtime User
Also Theera, add a case else to the select statement to catch invalid values that may be entered.
 
Upvote 0

Theera

Well-Known Member
Licensed User
Longtime User
Therefore The way imonth is int or string in a same time is no way.
 
Upvote 0

Daestrum

Expert
Licensed User
Longtime User
Leave imonth as String in the sub, if you call it with an Int value, java will convert to String, that is why the lines are
case "January","1"

The "January" part is when you call with a String, the "1" part is for converted Int.

This will accept either "January" as a real String, or 1 as an int which will get converted to the String "1".
 
Upvote 0

Theera

Well-Known Member
Licensed User
Longtime User
I'm sorry that my words make terrible,my English is still badly . The really, if I have one sub module which has input could be String or some type of parameter which isn't string or int,How to do? (It's alike using Overloading in Java's polymorphism)

B4X:
public void print(int a);
public void print(float a);
public void print(double a);
public void print();

P.S. I've no knowledge of Java,I cut them from a book.
 
Upvote 0

Daestrum

Expert
Licensed User
Longtime User
You could try this, not elegant, and it cannot handle null parameters.
B4X:
Sub Process_Globals
    Private fx As JFX
    Private MainForm As Form
    Dim s As String = "a string"
    Dim i As Int = 99
    Dim f As Float = 9.87
    Dim d As Double = 23456
End Sub

Sub AppStart (Form1 As Form, Args() As String)
    MainForm = Form1
    'MainForm.RootPane.LoadLayout("Layout1") 'Load the layout file.
    MainForm.Show
    ' call with  String
    Log(a(s))
    ' call with Int (Integer)
    Log(a(i))
    ' call with Float
    Log(a(f))
    ' call with double
    Log(a(d))
End Sub
Sub a(parm As JavaObject) As String
        Dim t As String = parm.RunMethod("getClass",Null)  'get class of parm
        Dim ti As Object = parm                                     'get parm real value floats are dodgy
        Select t.SubString(t.LastIndexOf(".")+1)               ' just want class name nor java.lang.....
            Case "String"
                Return "a String '" & ti  ' handle a String here
            Case "Integer"
                Return "an Integer " & ti ' handle an Integer here
            Case "Float"
                Return "a Float " & ti   ' handle a Float here
            Case Else
                Return "Unhandled type : " & t.SubString(t.LastIndexOf(".")+1) & "  " & ti
        End Select
End Sub
 
Upvote 0

Daestrum

Expert
Licensed User
Longtime User
@agraham I agree in the original question making it String would suffice, but in post #9 question it implies you need to know the data type being sent.
 
Upvote 0
Top