\ 'VB.NET FRAMEWORK' 카테고리의 글 목록 :: Something New
728x90
반응형

Bit연산

체크를 넣은 항목만 적용시키고 싶을 때

각 항목을 boolean으로 T/F로 정의하지 않고

비트 연산을 사용해서 각 자릿수의 0/1을 판단해서 1인 항목만을 적용시킴

예)font, fontsize, fontcolor, fontStyle, buttonColor, width, height,btnTxtPos

어떤 것에도 체크가 되지 않았을 때 메시지를 내보냄

 

'Before
'하나씩 T/F를 지정해주는 방식

Private bFontSizeBS As Boolean = False
Private bFontStyleBS As Boolean = False
Private bFontColorBS As Boolean = False
Private bBackColorBS As Boolean = False
Private bWidthBS As Boolean = False
Private bHeightBS As Boolean = False
Private bTxtPosBS As Boolean = False

체크를 넣어주는 처리에 각 항목을 true로 바꿔줌

If (bFontBS OrElse bFontColorBS OrElse bFontSizeBS OrElse bFontStyleBS OrElse bBackColorBS OrElse bWidthBS OrElse bHeightBS OrElse bTxtPosBS) = False Then

		Dim strWarningMsg As String = dbSC.GetMessageString(msgName, 100)
		Dim ret As DialogResult = MessageBox.Show(strWarningMsg, "", MessageBoxButtons.OK,
    MessageBoxIcon.Warning, MessageBoxDefaultButton.Button1)
    Exit Sub
End If

If bFontBS Then
		defN.Font = BSFontName
End If

'After
'16진수 BIT연산으로 판단하는 방법

Private Const INT_Font As Integer = &H1
Private Const INT_FontSize As Integer = &H2
Private Const INT_FontColor As Integer = &H4
Private Const INT_FontStyle As Integer = &H8
Private Const INT_BackColor As Integer = &H10
Private Const INT_Width As Integer = &H20
Private Const INT_Height As Integer = &H40
Private Const INT_TxtPos As Integer = &H80

nRange = nRange Or INT_Height

If nRange = 0 Then
		Dim strWarningMsg As String = dbSC.GetMessageString(msgName, 100)
		Dim ret As DialogResult = MessageBox.Show(strWarningMsg, "dbSheetClient", MessageBoxButtons.OK,
		MessageBoxIcon.Warning, MessageBoxDefaultButton.Button1)
		Exit Sub
End If
If nRange And INT_Font Then
		defN.Font = BSFontName
End If

 

728x90

'VB.NET FRAMEWORK' 카테고리의 다른 글

[vb.net]HASH SET Multiple PK 주키 설정하기  (0) 2020.05.31
728x90
반응형

Hash Set

;고유한 요소를 배열에 저장하고 싶을 때 최적화된 집합

 

특징

  • 생성자를 사용해서 문자열을 set로 배열에 저장
  • 모든 중복은 제거됨
  • 인수는 IEnumerable(Of String)가 됨

Example

엑셀에서 불러온 표를 그대로 DB에 저장하는 부분으로 열명이 중복되면 안 되기 때문에

열명이 중복될 경우에는 보통 본래의 열명을 Invalid Row무효행 처리합니다.*(아래와 같은 예시에는 사용되지 않음)

datatable에서 PrimaryKey설정을 복수로 할 경우

Invalid Row Name Birth Nation Sex
Lee 92/08/29 KOREA M
Kim 94/11/04 AMERICA M
Park null CHINA W
Kim 94/11/04 JAPAN M

Invalid Row무효행; 

✔: 해당 행은 db에 넣지 않음(실제로는 체크박스 체크)

⬜: 해당 행은 db에 들어감(실제로는 체크박스 빈칸)

 

PRIMARY KEY; 고유의 값으로 값이 중복되거나 NULL이 아닌 열

PK를 Name으로 설정하고 싶은데 동명이인이 존재할 때

Birth도 같이 PK를  복수선택 하면 된다.

주의할 점;

1. Name 열 안에서는 중복,공백(NULL) OK (Kim이 두 명이어도 상관없음)

2. BIrth 열 안에서는 중복,공백(NULL) OK (94/11/04생이 두 명이어도 상관없음)

3. Name,Birth가 둘 다 일치할 경우는 PK설정 안 됨(이름이 Kim이면서 94/11/04 생인 행이 두 개면 안됨)

 

 

```vbnet
Dim hsPk As New HashSet(Of String)
Friend Function ConfirmDataDuplication() As Boolean
        Dim HashSet As New HashSet(Of String)
        For Each row As DataRow In ViewTable.Rows
            If row(0) IsNot DBNull.Value AndAlso row(0) Then    
                Continue For
            Else
                Dim strHashset As String = ""
                For Each strPk As String In HashSet
                    If row(strPk) Is DBNull.Value Then          '값이 NULL일 때
                        Return True
                    End If
                    strHashset &= row(strPk) & "+"         '중복확인을 위해 값을 연결해줌
                Next
                If Not HashSet.Add(strHashset) Then             'HASH SET에 넣어보고 중복이면 FALSE를 반환
                    Return True
                End If
            End If
        Next
        Return False
    End Function
```

여기서부터는 한 번 주키로 설정한 열을 한 번 더 설정할 경우 해제,

다른 열로 설정할 경우는 전에 설정한 주키를 해제하고 해당 선택 열로 바꾸는 과정입니다

 

```vbnet
Dim dicColName As New Dictionary(Of String, Boolean)
Friend Property MultiplePrimaryKeys As String()             '복수의 PK설정
        Get
            Dim arrStrPk(HashSet.Count) As String
            HashSet.CopyTo(arrStrPk)
            Return arrStrPk
        End Get
        Set(value As String())
            Dim lstBeforePk As New List(Of String)
            Dim lstSelectedCol As New List(Of Boolean)
            For Each strColName As String In dicColName.Keys    '전체 열명
                If dicColName(strColName) Then                   '설정된 열이 PK면 list에 넣어줌
                    lstBforPk.Add(strColName)
                End If
            Next

            For Each strSelectedColName As String In value             '선택된 열명
                lstSelectedCol.Add(dicColName(strSelectedColName))     '선택된 열의 boolean값을 전부 넣어줌
            Next
            ClearPK()

            Dim bSetPk As Boolean = False
            If lstSelectedCol.Contains(False) Then                  '선택한 열이 원래 주키가 아니었을 때
                bSetPk = True
                For Each strSelectedColName As String In value      '선택된 열명
                    dicColName(strSelectedColName) = True
                Next
            End If

            If bSetPk Then 
                For Each str As String In value
                    Me.HashSet.Add(str)          'PK에 추가
                Next
                Dim strMsg As String = Nothing
                If Me.ckbInvalid.Checked AndAlso WarningMessage() Then                      ' 주키값이 중복이나 문자수 초과하면 경고메세지
                    ClearPK()                        
                    For Each str As String In lstBeforePk       '취소시에 예전값을 넣어줌
                        HashSet.Add(str)
                        dicColName(str) = True
                    Next

                    Exit Property
                End If

                Me.dgvExcel.ClearSelection()
            End If
            SetTable() 
        End Set
    End Property
```

 

728x90

'VB.NET FRAMEWORK' 카테고리의 다른 글

[VB.NET]bit연산  (0) 2021.02.22

+ Recent posts