PASSING ENUM AS PARAMETER

Hi All,

I have enum below :

    Private Enum ColTitleOne
        NoUrut = 0
        NoFaktur = 1
        TglFaktur = 2    
    End Enum

    Private Enum ColTitleTwo
        NoUrut = 0
        CustCode = 1
        CustName = 2    
    End Enum

    Private sub Print_Data()

      if blnOne then
        Print_Total(paramter enum ColTitleOne)
      else
           Print_Total(paramter enum ColTitleTwo)
      endif
    End Sub

    Private sub Print_Total(paramter enum)
    End Sub

I want Print_Total could receive paramater from enum ColTitleOne or ColTitleTwo.

How could I do it ?

Thank you.
LVL 1
emi_sastraAsked:
Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

x
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

CodeCruiserCommented:
The enum is private within your own class so it can not be shared by other classes. Add it to a module as a public and then you can pass it as parameter.
emi_sastraAuthor Commented:
It is in the same class.

Thank you.
Carl TawnSystems and Integration DeveloperCommented:
You can leave it in the same class, you will just need to refer to it as YourClassName.YourEnumName instead.
CompTIA Security+

Learn the essential functions of CompTIA Security+, which establishes the core knowledge required of any cybersecurity role and leads professionals into intermediate-level cybersecurity jobs.

emi_sastraAuthor Commented:
I've got this on the net, what do you think ?


^^ you cannot directly pass the enum season or month cause its a type. you have to declare a variable and then passthat to the function

Thank you.
Asim NazirCommented:
Private Enum ColTitleOne
        NoUrut = 0
        NoFaktur = 1
        TglFaktur = 2
    End Enum

    Private Enum ColTitleTwo
        NoUrut = 0
        CustCode = 1
        CustName = 2
    End Enum

    Private Sub Print_Data()

        Dim blnOne As Boolean

        If blnOne Then
            Print_Total(ColTitleOne.NoFaktur)
        Else
            Print_Total(ColTitleOne.TglFaktur)
        End If
    End Sub

    Private Sub Print_Total(ByVal title As ColTitleOne)
    End Sub

Open in new window

emi_sastraAuthor Commented:
Please see sample, not applicable for my purpose.

Public Enum season
        winter = 0
        summer = 1
    End Enum
    Public Enum month
        jan = 0
        feb = 1
    End Enum

    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
        Dim se As season
        Dim mon As month
        test(se)
        test(mon)
    End Sub

    Sub test(ByRef ses As season)
        MessageBox.Show(ses.summer.ToString & vbCrLf & ses.winter.ToString)
    End Sub

    Sub test(ByRef mo As month)
        MessageBox.Show(mo.feb.ToString & vbCrLf & mo.jan.ToString)
    End Sub

Thank you.
emi_sastraAuthor Commented:
I want to pass entire enum.

If blnOne Then
            Print_Total(ColTitleOner)
        Else
            Print_Total(ColTitleTwo)
        End If

Thank you.
CodeCruiserCommented:
Does it work?
emi_sastraAuthor Commented:
not applicable for my purpose.

 want to pass entire enum.

If blnOne Then
            Print_Total(ColTitleOner)
        Else
            Print_Total(ColTitleTwo)
        End If

Thank you.
CodeCruiserCommented:
I think enum is used to pass ONE of the options. You may want to look at structures.
Asim NazirCommented:
Emi, It cannot work the way you want to do it.
emi_sastraAuthor Commented:
We need two function to hold two enum.

Is it possible using one function to get the parameter?

Thank you.
emi_sastraAuthor Commented:
What should I do ?

Any other way are welcome.

Thank you.
Dirk HaestProject managerCommented:
   Private Sub Print_Total(ByVal myEnum As Object)
        Dim value As Integer
        If myEnum.GetType().Name = "ColTitleOne" Then
            value = CType(myEnum, ColTitleOne)
        Else
            value = CType(myEnum, ColTitleTwo)
        End If

    End Sub
emi_sastraAuthor Commented:
Hi Dhaest,

Why value is a integer not enum ?

Thank you.

Dirk HaestProject managerCommented:
>> Why value is a integer not enum ?

Because if you really want to use it, you'll need to declare 2 variables, one of each type and fill it than to use it further
emi_sastraAuthor Commented:
Please see below :

 Private Sub PrintSubTotal(ByVal ParmColTitle As Object)

        Dim ColTitleAs Integer

        If ParmColTitle.GetType().Name = "ColTitleOne" Then
            ColTitle= CType(ParmColTitle, ColTitleOne)
        Else
            ColTitle= CType(ParmColTitle, ColTitleTwo)
        End If

'compile error below code
       oSheet.getCellByPosition(ColTitle.SalesOrderTgl, intRow).SetString("SUB TOTAL :")

End Sub

Thank you.

CodeCruiserCommented:
What are you actually trying to achieve(in functional terms)?
emi_sastraAuthor Commented:
Please see below code.

 Private Sub PrintSubTotal()

  If blnPrintCustomerAtDetail Then
            oSheet.getCellByPosition(ColTitleTwo.SalesOrderTgl, intRow).SetString("SUB TOTAL :")
        Else
            oSheet.getCellByPosition(ColTitleOne.NoUrut, intRow).SetString("SUB TOTAL :")
        End If

End Sub

Thank you.
Dirk HaestProject managerCommented:
Try this:
If ParmColTitle.GetType().Name = "ColTitleOne" Then
            oSheet.getCellByPosition(Ctype(myEnum,ColTitleOne), intRow).SetString("SUB TOTAL :")
        Else
            oSheet.getCellByPosition(Ctype(myEnum,ColTitleTwo), intRow).SetString("SUB TOTAL :")
        End If

What I really don't understand is the following. You can pass 2 different enums, but those enums have the same values.
So if you pass fe ColTitleOne.NoFactur is the same search (in excel) as passing ColTitleTwo.CustCode ?
CodeCruiserCommented:
May be you should just pass the column index?
emi_sastraAuthor Commented:
Please see the real one:

 Private Enum ColTitleOne
        NoUrut = 0
        NoFaktur = 1
        TglFaktur = 2
        JatuhTempo = 3
        JumlahHariJatuhTempo = 4
        SalesOrderNo = 5
        SalesOrderTgl = 6
        CurcyCode = 7
        NilaiFaktur = 8
        NilaiFakturSaldoAwal = 9
        NilaiFakturPenjualan = 10
        NilaiFakturAlokasi = 11
        NilaiGiroMundur = 12
        SaldoAkhirStlhAlokasi = 13
    End Enum

    Private Enum ColTitleTwo
        NoUrut = 0
        CustCode = 1
        CustName = 2
        NoFaktur = 3
        TglFaktur = 4
        JatuhTempo = 5
        JumlahHariJatuhTempo = 6
        SalesOrderNo = 7
        SalesOrderTgl = 8
        CurcyCode = 9
        NilaiFaktur = 10
        NilaiFakturSaldoAwal = 11
        NilaiFakturPenjualan = 12
        NilaiFakturAlokasi = 13
        NilaiGiroMundur = 14
        SaldoAkhirStlhAlokasi = 15
    End Enum


Thank you.
emi_sastraAuthor Commented:
And the print.

   Private Sub PrintSubTotal(ByVal ParmColTitle As Object)

        Dim value As Integer

        If ParmColTitle.GetType().Name = "ColTitleOne" Then
            value = CType(ParmColTitle, ColTitleOne)
        Else
            value = CType(ParmColTitle, ColTitleTwo)
        End If

        oRange = oSheet.getCellRangeByPosition(0, intRow, intMaxColumn, intRow)
        oCalcCtrl.Select(oRange)

        If blnPrintCustomerAtDetail Then
            oSheet.getCellByPosition(ColTitleTwo.SalesOrderTgl, intRow).SetString("SUB TOTAL :")
        Else
            oSheet.getCellByPosition(ColTitleOne.NoUrut, intRow).SetString("SUB TOTAL :")
        End If

        For intColumn As Integer = ColTitleOne.NilaiFaktur To ColTitleOne.SaldoAkhirStlhAlokasi
            strCellRange = Get_CellRange_OOo(intColumn, intStartRow, intColumn, intRow - 1)
            oSheet.getCellByPosition(intColumn, intRow).SetFormula("=SUM(" & strCellRange & ")")
            oSheet.getCellByPosition(intColumn, intRow).NumberFormat = mdlOpenOffice.getNumberFormat(oCalcDoc, strNumberFormatDecimal)
        Next

        strCellRange = mdlOpenOffice.Get_CellRange_OOo(ColTitleOne.NilaiFaktur, intRow, intMaxColumn, intRow)

        If blnPrintCustomerAtDetail Then

        Else
            oSheet.getCellRangeByName(strCellRange).Merge(False)
        End If

        oSheet.getCellRangeByName(strCellRange).HoriJustify = ALIGNMENT_RIGHT
        oSheet.getCellRangeByName(strCellRange).CharWeight = FONT_BOLD

        strCellRange = mdlOpenOffice.Get_CellRange_OOo(0, intRow, ColTitleOne.SalesOrderTgl, intRow)
        oSheet.getCellRangeByName(strCellRange).Merge(True)
        oSheet.getCellRangeByName(strCellRange).HoriJustify = ALIGNMENT_RIGHT
        oSheet.getCellRangeByName(strCellRange).CharWeight = FONT_BOLD
    End Sub
emi_sastraAuthor Commented:
I hope I don't have to write two function.

Thank you.
Dirk HaestProject managerCommented:
Why don't you just pass the value of the enum (so the integer-value to the function ?), because you just use this value as a reference to the cell-row or cell-columnnumber ?
PrintSubTotal(ctype(ColTitleTwo.SalesOrderNo, integer))


Private Sub PrintSubTotal(ByVal ParmColTitle As integer)
....

End Sub

Open in new window

emi_sastraAuthor Commented:
How about this:

For intColumn As Integer = ColTitleOne.NilaiFaktur To ColTitleOne.SaldoAkhirStlhAlokasi

at below code

For intColumn As Integer = ColTitleOne.NilaiFaktur To ColTitleOne.SaldoAkhirStlhAlokasi
            strCellRange = Get_CellRange_OOo(intColumn, intStartRow, intColumn, intRow - 1)
            oSheet.getCellByPosition(intColumn, intRow).SetFormula("=SUM(" & strCellRange & ")")
            oSheet.getCellByPosition(intColumn, intRow).NumberFormat = mdlOpenOffice.getNumberFormat(oCalcDoc, strNumberFormatDecimal)
        Next

Thank you
Dirk HaestProject managerCommented:
What about that ? I don't see the point (or do you get an error)


For intColumn As Integer = Ctype(ColTitleOne.NilaiFaktur,integer) To Ctype(ColTitleOne.SaldoAkhirStlhAlokasi,integer)
emi_sastraAuthor Commented:
For intColumn As Integer = ColTitleOne.NilaiFaktur To ColTitleOne.SaldoAkhirStlhAlokasi

That is for ColTitleOne

How about for ColTitleTwo ?

Thank you.
emi_sastraAuthor Commented:
Hi CodeCruiser,

- May be you should just pass the column index?
Passing column Index is risky, would be better using 2 functions , one for ColTitleOne, and ColTitleTwo .

Thank you.

vbighamCommented:
It seems like you might be able to use the static helpers in System.Enum ??

I have sometimes used:

System.Enum.GetName()
System.Enum.GetNames()
System.Enum.GetValue()
System.Enum.GetValues()
System.Enum.Parse()

Here is a link to the method definitions on msdn:
http://msdn.microsoft.com/en-us/library/system.enum_methods(v=VS.90).aspx

emi_sastraAuthor Commented:
Hi vbigham,

I have no idea which one solve my problem ?

Thank you.
vbighamCommented:
I think maybe we are all a little bit confused about what you are trying to do.  It is not really obvious by the given code.  It looks to me like you want to update the subtotal on the oSheet, is that right.  Would you mind providing some specific information about what it needs to do?  For example:

How do you need to use the enum passed into the method?
Do you need to use every value in that enum?
Do you need just a single value, but determine which enum was passed as a parameter?
What is the oSheet object that is being updated, and how does it relate to the provided enumerations?

If you can help us out and be as specific as possible about what you are trying to do, I am pretty confident that we can guide you towards a good solution.

Thanks,
Vaughn
emi_sastraAuthor Commented:
Ok. Let me describe more detail.

I have a class to generate report.
This class could generate to 2 kind of report format, using ColTitleOne and ColTitleTwo.

Since ColTitleOne and ColTitleTwo have different columns format, then when I try to write it I need 2 functions to print it.

I hope I could just have a function to print data that could receive the ColTitleOne or ColTitleTwo enum.

If you see the code below, I need If code to separate it. I could just receive enum parameter, I do not need the If again.

OSheet is the OOCalc Sheet Object.

Thank you.
Private Sub PrintSubTotal()

        oRange = oSheet.getCellRangeByPosition(0, intRow, intMaxColumn, intRow)
        oCalcCtrl.Select(oRange)

         If blnPrintCustomerAtDetail Then
            For intColumn As Integer = ColTitleTwo.NilaiFaktur To ColTitleTwo.SaldoAkhirStlhAlokasi
                strCellRange = Get_CellRange_OOo(intColumn, intStartRow, intColumn, intRow - 1)
                oSheet.getCellByPosition(intColumn, intRow).SetFormula("=SUM(" & strCellRange & ")")
                oSheet.getCellByPosition(intColumn, intRow).NumberFormat = mdlOpenOffice.getNumberFormat(oCalcDoc, strNumberFormatDecimal)
            Next

            strCellRange = mdlOpenOffice.Get_CellRange_OOo(ColTitleTwo.NoUrut, intRow, intMaxColumn, intRow)

        Else
            For intColumn As Integer = ColTitleOne.NilaiFaktur To ColTitleOne.SaldoAkhirStlhAlokasi
                strCellRange = Get_CellRange_OOo(intColumn, intStartRow, intColumn, intRow - 1)
                oSheet.getCellByPosition(intColumn, intRow).SetFormula("=SUM(" & strCellRange & ")")
                oSheet.getCellByPosition(intColumn, intRow).NumberFormat = mdlOpenOffice.getNumberFormat(oCalcDoc, strNumberFormatDecimal)
            Next

            strCellRange = mdlOpenOffice.Get_CellRange_OOo(ColTitleOne.NoUrut, intRow, intMaxColumn, intRow)

        End If

        
         End Sub

Open in new window

vbighamCommented:
emi_sastra,

Thank you for that explanation.  It seems that what you are wanting is a more generic method that will work for either case to eliminate repetition of code.  Is that about right?

Since it looks like both loops are identical except for the starting and ending values, I would suggest doing this:
Private Sub PrintSubTotal(ByVal columnTypes As System.Type)
    Dim iStartValue As Integer = CInt(System.Enum.Parse(columnTypes, "NilaiFaktur"))
    Dim iEndValue As Integer = CInt(System.Enum.Parse(columnTypes, "SaldoAkhirStlhAlokasi"))
    Dim iCellRangeStartValue As Integer = CInt(System.Enum.Parse(columnTypes, "NoUrut"))

    For intColumn As Integer = iStartValue To iEndValue
        strCellRange = Get_CellRange_OOo(intColumn, intStartRow, intColumn, intRow - 1)
        oSheet.getCellByPosition(intColumn, intRow).SetFormula("=SUM(" & strCellRange & ")")
        oSheet.getCellByPosition(intColumn, intRow).NumberFormat = mdlOpenOffice.getNumberFormat(oCalcDoc, strNumberFormatDecimal)
    Next

    strCellRange = mdlOpenOffice.Get_CellRange_OOo(iCellRangeStartValue, intRow, intMaxColumn, intRow)
End Sub

Open in new window

This does not include any error checking, if the parse fails I believe it will throw an exception.  Those values are integers I am assuming since there is no type declaration on the enums you defined and Integer is the default underlying type for an enum.

You could then call it like this:
PrintSubTotal(GetType(ColTitleOne))
PrintSubTotal(GetType(ColTitleTwo))

Open in new window

Let me know if you have any trouble making this work.

Thanks,
Vaughn  

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
emi_sastraAuthor Commented:
Hi Vaughn,

Yes, It works great.

Thank you very much for your help.
vbighamCommented:
Glad it works for you.

Thanks
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Visual Basic.NET

From novice to tech pro — start learning today.