Solved

Retruning 2 dimensional array in VB.NET

Posted on 2006-11-28
11
276 Views
Last Modified: 2008-01-09
Experts,

I have the following function and I am returning 2 fields a groupId and roleId to the dataset.
However, I need to know how to transform this fucntion so that it returns a 2 dimensional array instead one.

I am getting the following error:
UserDC.vb(182): 'ReDim' cannot change the number of dimensions of an array.
'---------------------------------------------------------------------------------
Here's the function below:

    Public Function GetUserGroupsRolesAsArray(ByVal UserName As String) As Integer(,)
        Dim objDS As DataSet
        Dim objRow As DataRow
        Dim aintGroups(,) As Integer
        Dim intLoop As Integer

        objDS = GetUserGroups(UserName)

        ReDim aintGroups(objDS.Tables(0).Rows.Count - 1)

        intLoop = 0
        For Each objRow In objDS.Tables(0).Rows
            aintGroups(intLoop) = Convert.ToInt32(objRow.Item("groupID"))
            intLoop += 1
        Next

        Return aintGroups
    End Function

Thanks
0
Comment
Question by:yaney00074
  • 6
  • 4
11 Comments
 
LVL 16

Expert Comment

by:Hillwaaa
ID: 18031787
Hi yaney00074,


I think you want something along these lines:

    Public Function GetUserGroupsRolesAsArray(ByVal UserName As String) As Integer()()
        Dim objDS As DataSet
        Dim objRow As DataRow
        Dim aintGroups()() As Integer
        Dim intLoop As Integer

        objDS = GetUserGroups(UserName)

        ReDim aintGroups(objDS.Tables(0).Rows.Count - 1)(????)

        intLoop = 0
        For Each objRow In objDS.Tables(0).Rows
            aintGroups(intLoop)(?????) = Convert.ToInt32(objRow.Item("groupID"))
            intLoop += 1
        Next

        Return aintGroups
    End Function

Cheers,
Hillwaaa
0
 
LVL 24

Expert Comment

by:Jeff Certain
ID: 18031788
The problem is here:
ReDim aintGroups(objDS.Tables(0).Rows.Count - 1)

You really want:
ReDim aintGroups(objDS.Tables(0).Rows.Count - 1, 1)

0
 
LVL 24

Expert Comment

by:Jeff Certain
ID: 18031803
Full rewrite:

  Public Function GetUserGroupsRolesAsArray(ByVal UserName As String) As Integer(,)
        Dim objDS As DataSet
        Dim objRow As DataRow
        Dim aintGroups(,) As Integer
        Dim intLoop As Integer

        objDS = GetUserGroups(UserName)

        ReDim aintGroups(objDS.Tables(0).Rows.Count - 1, 1)

        intLoop = 0
        For Each objRow In objDS.Tables(0).Rows
            aintGroups(intLoop, 0) = Convert.ToInt32(objRow.Item("groupID"))
            aintGroups(intLoop, 1) = Convert.ToInt32(objRow.Item("roleId"))
            intLoop += 1
        Next

        Return aintGroups
    End Function
0
 

Author Comment

by:yaney00074
ID: 18031898
Chaosian,

Thanks for your quick feedback.

If you don't mind, can you explain to me, in a step-by-step fashion what's happening?

Thanks
0
 
LVL 24

Accepted Solution

by:
Jeff Certain earned 500 total points
ID: 18032021
Sure... you want a 2D array. However, what you really want is an array that has N pairs of integers, where N is the number of rows in your dataset.

ReDim aintGroups(objDS.Tables(0).Rows.Count - 1, 1)  ' changes the array to be N x 2, since the array index is zero-based
(as an aside, if I knew you were using VS2005, I'd recommend a generics-based alternative)

Then you loop through your rows. Each time through, the (N,0) element gets the groupId assigned to it and the (N,1) element gets the role Id. So, for any row with index n, the (n,0) element of aintGroups has the appropriate groupId and the (n,1) element has the row Id.

The rest is your code :)
0
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 

Author Comment

by:yaney00074
ID: 18032905
Ooh I forgot to ask (hopefully it's not too late)

The calling variable is a 2-dimensional property array.

Now we're having the array assigned to that property.

How concretely would I use it to evaluate a condition.
For example with the first item being the GroupID and the second the roleID within that group
Here's what I get for example:
(0,0) = 175
(0,1) = 1
(1,0) = 23
(1,1) = 3
(2,0) = 46
(2,1) = 3
etc...

0
 
LVL 24

Expert Comment

by:Jeff Certain
ID: 18032918
1. What do you mean by "evaluate a condition"?
2. What version of .NET are you using?
0
 

Author Comment

by:yaney00074
ID: 18036063
What do you mean by "evaluate a condition"?

What I mean is looping through that 2-D array to check if a particular
Basically, this array is a property of the user object (objUser)  like this:
objUser.groupID

The functionality returns a user object with those properties encapsulated in it.

But the challenges I facing is to know HOW TO EVALUATE THE CONTENT OF that (now) 2D array
by  stripping out the value of the Group it belongs (which is the first element) and the Role (which is the second element)

Please, let me know if I am still not clear

What version of .NET are you using?
.NET 1.1
0
 
LVL 24

Expert Comment

by:Jeff Certain
ID: 18036145
The way things sit now, your user can belong to multiple groups, and the return value contains all groups and roles that the user belongs to.

You'll need to do something like this in your user object (assuming that your user object has a property called Groups that contains that array:

Public Function BelongsToGroup(ByVal group As Integer) As Boolean
    BelongsToGroup = False

    Dim i As Integer
    For i = 0 To UBound(Groups, 1) - 1
      If Groups(i, 0) = group Then
        Return True
      End If
    Next
  End Function
0
 

Author Comment

by:yaney00074
ID: 18047780
Chaosian,

I am having serious problem getting your functionality to work:

Public Function BelongsToGroup(ByVal group As Integer) As Boolean
    BelongsToGroup = False

    Dim i As Integer
    For i = 0 To UBound(Groups, 1) - 1
      If Groups(i, 0) = group Then
        Return True
      End If
    Next
  End Function

---

How can I adjust this to work and that would to the job:

           For i = 0 To UBound(cUser.GroupID, 1) - 1
                For j = 0 To UBound(cUser.GroupID, 1) - 1
                    Me.lblGroup.Text = "(" & cUser.GroupID(i, 0).ToString & ", " & cUser.GroupID(0, j) & ")" & "<br/>"
                Next
            Next

Question:
How do i loop to display the content of the array?
0
 
LVL 24

Expert Comment

by:Jeff Certain
ID: 18050558
For i = 0 To UBound(cUser.GroupID, 1) - 1
                    Me.lblGroup.Text = "(" & cUser.GroupID(i, 0).ToString & ", " & cUser.GroupID(i, 1) & ")" & "<br/>"
            Next
0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Introduction As chip makers focus on adding processor cores over increasing clock speed, developers need to utilize the features of modern CPUs.  One of the ways we can do this is by implementing parallel algorithms in our software.   One recent…
If you need to start windows update installation remotely or as a scheduled task you will find this very helpful.
Along with being a a promotional video for my three-day Annielytics Dashboard Seminor, this Micro Tutorial is an intro to Google Analytics API data.
This Micro Tutorial demonstrates using Microsoft Excel pivot tables, how to reverse engineer competitors' marketing strategies through backlinks.

910 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

22 Experts available now in Live!

Get 1:1 Help Now