Solved

Retruning 2 dimensional array in VB.NET

Posted on 2006-11-28
11
275 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
Highfive + Dolby Voice = No More Audio Complaints!

Poor audio quality is one of the top reasons people don’t use video conferencing. Get the crispest, clearest audio powered by Dolby Voice in every meeting. Highfive and Dolby Voice deliver the best video conferencing and audio experience for every meeting and every room.

 

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

Better Security Awareness With Threat Intelligence

See how one of the leading financial services organizations uses Recorded Future as part of a holistic threat intelligence program to promote security awareness and proactively and efficiently identify threats.

Join & Write a Comment

Parsing a CSV file is a task that we are confronted with regularly, and although there are a vast number of means to do this, as a newbie, the field can be confusing and the tools can seem complex. A simple solution to parsing a customized CSV fi…
It was really hard time for me to get the understanding of Delegates in C#. I went through many websites and articles but I found them very clumsy. After going through those sites, I noted down the points in a easy way so here I am sharing that unde…
Sending a Secure fax is easy with eFax Corporate (http://www.enterprise.efax.com). First, Just open a new email message.  In the To field, type your recipient's fax number @efaxsend.com. You can even send a secure international fax — just include t…
This video explains how to create simple products associated to Magento configurable product and offers fast way of their generation with Store Manager for Magento tool.

757 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

18 Experts available now in Live!

Get 1:1 Help Now