Functions within Button_Click

  • Thread starter Thread starter Guest
  • Start date Start date
G

Guest

I am trying to call a recursive subroutine within a
Button_Click event. How can I do this?

I tried to create a seperate Function() but it will not
allow me to use any variables that I created in the
Button_Click event.

Any ideas?

Thanks!
 
I am trying to call a recursive subroutine within a
Button_Click event. How can I do this?

I tried to create a seperate Function() but it will not
allow me to use any variables that I created in the
Button_Click event.

Can you pass the variables to the recursive function? The GOSUB command
was not implemented in VB.NET so you can't use that. If there's too
many variables to pass, you could try placing them in a structure.
 
Matt said:
I am trying to call a recursive subroutine within a
Button_Click event. How can I do this?

I tried to create a seperate Function() but it will not
allow me to use any variables that I created in the
Button_Click event.

Matt: A word is worth 1/1000th of a picture :-) Could you post an example?
It should make it easier to come up with a solution.

Tom
 
Matt,
Is this a homework assignment?
I tried to create a seperate Function() but it will not
allow me to use any variables that I created in the
Button_Click event.
You would pass "Variables" to the function as parameters, just like any
other function or sub. Alternatively you can declare the "variables" within
the Class itself.

Have you tried something like:

Private Sub Button_Click(...)
Dim x As Integer = 1000
Dim y As String
MyFunction(x, y)
End Sub

Private Function MyFunction(ByVal x As Integer, ByVal y As String)
If x <= 0 Then Exit Function
MyFunction(x - 1, y)
End Function

Hope this helps
Jay
 
Public Sub B_Lookup_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles B_Lookup.Clic
Dim objUser, objDomain, objShell, objRootDSE, objTrans, objGroupList, objGrou
Dim strUserDN, strDNSDomain, strNetBIOSDomain, strUserNTNam
Dim strText, intConstants, intAns, strtitl
Dim firstname, lastname As Strin


' Constants for the NameTranslate object
Const ADS_NAME_INITTYPE_DOMAIN =
Const ADS_NAME_TYPE_NT4 =
Const ADS_NAME_TYPE_1179 =

objShell = CreateObject("Wscript.Shell"

' Request user sAMAccountName
strtitle = "User Admin Console
strUserNTName = Txt_Input_Username.Tex
Txt_Input_Username.Text = "
If strUserNTName = "" The
L_Result.Text = "Please Enter a Username
Txt_Input_Username.Focus(
Exit Su
End I

' Retrieve DNS domain name
objRootDSE = GetObject("LDAP://RootDSE"
strDNSDomain = objRootDSE.Get("defaultNamingContext"

' Convert DNS domain name to NetBIOS domain name
objTrans = CreateObject("NameTranslate"
objTrans.Init(ADS_NAME_TYPE_NT4, strDNSDomain
objTrans.Set(ADS_NAME_TYPE_1179, strDNSDomain
strNetBIOSDomain = objTrans.Get(ADS_NAME_TYPE_NT4
' Remove trailing backslash
strNetBIOSDomain = Microsoft.VisualBasic.Left(strNetBIOSDomain, Len(strNetBIOSDomain) - 1

' Convert user NT name to Distinguished Name
objTrans.Init(ADS_NAME_INITTYPE_DOMAIN, strNetBIOSDomain
On Error Resume Nex
Err.Clear(
objTrans.Set(ADS_NAME_TYPE_NT4, strNetBIOSDomain & "\" & strUserNTName
If Err.Number <> 0 The
Err.Clear(
L_Result.Text = "No User Found
Txt_Firstname.Text = "
Txt_Initial.Text = "
Txt_Lastname.Text = "
txt_Location.Text = "
Txt_Input_Username.Focus(
Exit Su
End I
On Error GoTo
strUserDN = objTrans.Get(ADS_NAME_TYPE_1179

' Bind to user object
On Error Resume Nex
Err.Clear(
objUser = GetObject("LDAP://" & strUserDN
If Err.Number <> 0 The
Err.Clear(
Txt_Input_Username.Text = "User does not exist
Txt_Firstname.Text = "
Txt_Initial.Text = "
Txt_Lastname.Text = "
txt_Location.Text = "
Txt_Input_Username.Focus(
Exit Su
End I
On Error GoTo

' Bind to domain
objDomain = GetObject("LDAP://" & strDNSDomain
L_Result.Text = "User Found!
Txt_Firstname.Text = objUser.Get("givenname"
Txt_Initial.Text = objUser.Get("initials"
Txt_Lastname.Text = objUser.Get("sn"
txt_Location.Text = objUser.Get("description"

**This is where I want to call a seperate subroutine called GroupEnum (the code for this group is below)**
Call GroupEnum(objUser

B_Return_HFC_Main.Focus(

End Su

Sub GroupEnum(objADObject
' Recursive subroutine to enumerate user group memberships. Includes nested group memberships
Dim colstrGroups, objGroup,
objGroupList.CompareMode = vbTextCompar
colstrGroups = objADObject.memberO
If IsEmpty(colstrGroups) The
Exit Su
End I
If TypeName(colstrGroups) = "String" The
Set objGroup = GetObject("LDAP://" & colstrGroups
If Not objGroupList.Exists(objGroup.sAMAccountName) The
objGroupList(objGroup.sAMAccountName) = Tru
Wscript.Echo objGroup.distinguishedNam
Call GroupEnum(objGroup
End I
Set objGroup = Nothin
Exit Su
End I
For j = 0 To UBound(colstrGroups
Set objGroup = GetObject("LDAP://" & colstrGroups(j)
If Not objGroupList.Exists(objGroup.sAMAccountName) The
objGroupList(objGroup.sAMAccountName) = Tru
Wscript.Echo objGroup.distinguishedNam
Call GroupEnum(objGroup
End I
Nex
Set objGroup = Nothin
End Su


***This script works if I write just a vbscript. But when I try to put it into VB.NET it doesn't...**

Thanks for your help
 
No this is not a homework assignment. I am a systems admin trying to set up a User Admin Console which links into active directory. I am trying to list out all the groups a user is a member of

I posted code right above this post as a reply. Maybe this will show you more of what I am trying to do
 
Matt,
Yes I saw both of your postings, and I retract the question. I hope you
understand & appreciate why I asked the question! :-)

Looking at your code, either or both of my suggestions should work, with
effort.

Either make some of these parameters to GroupEnum, or move then outside of
B_Lookup_Click (class level):

Dim objUser, objDomain, objShell, objRootDSE, objTrans, objGroupList,
objGroup

I say "with effort" as when I cut & pasted the code into VS.NET, IsEmpty &
WScript came up undefined...

Hope this helps
Jay



Matt said:
No this is not a homework assignment. I am a systems admin trying to set
up a User Admin Console which links into active directory. I am trying to
list out all the groups a user is a member of.
I posted code right above this post as a reply. Maybe this will show you
more of what I am trying to do
 
Matt said:
Public Sub B_Lookup_Click(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles B_Lookup.Click
Dim objUser, objDomain, objShell, objRootDSE, objTrans, objGroupList, objGroup
Dim strUserDN, strDNSDomain, strNetBIOSDomain, strUserNTName
Dim strText, intConstants, intAns, strtitle
Dim firstname, lastname As String

Sorry Matt :-) Perhaps somebody else can wade their way through this. I
thought you were having a problem with recursion in VB.Net. But you've
posted a complex example that isn't in VB.Net.
 
Hi Tom,

Matt is redirected by Alex K. Angelopoulos[MVP] to this newsgroup telling
that his code was VB.net so do not blame him.

I asked in a previous thread if he was using VB.net or VS.studio.net to know
what the best way was to help him, but I saw Jay B has already pasted it in
his IDE so he has an answer from Jay B.

If he is not using VB.net we can always try it with all options off.

Cor
 
Tom,
It "is" VB.NET! ;-) As it contains the Handles keyword...

However! I believe it was cut & pasted from VBScript, hence the lack of
types on the variables.

As Cor suggested, you need to use Option Strict Off & Option Explicit Off,
and you still have a few errors you can "clean up" and a few errors that I'm
not sure what to do with.

I really don't know enough about LDAP to help, plus I don't have immediate
access to an LDAP server...

I think I will suggest he consider using the classes in the
System.DirectoryServices namespace, rather then attempt to converting the
VBScript code...

Just a thought
Jay
 
Matt,
Thinking about this, and reviewing the other comments in this thread.

Have you looked at using classes in the System.DirectoryServices namespace
to do your Active Directory searches?

http://msdn.microsoft.com/library/d...s/cpref/html/frlrfSystemDirectoryServices.asp

Contains the classes in the namespace as well as links to "how to" articles.

Hope this helps
Jay


Matt said:
No this is not a homework assignment. I am a systems admin trying to set
up a User Admin Console which links into active directory. I am trying to
list out all the groups a user is a member of.
I posted code right above this post as a reply. Maybe this will show you
more of what I am trying to do
 
Jay,

Thanks for your advice.

I am pretty new to VB.NET. How do I utilize the System.DirectoryServices Namespace?

Thanks again

Matt
 
Matt,
The page I gave you has a link to classes you need, predominately it is the
DirectoryEntry & DirectorySearcher classes. Normally when you review those
class topics on MSDN, they contain samples on how to use them... There are
links to the DirectoryEntry & DirectorySearcher topics on the page I gave
you.

Also in about the middle of the page is a link to "Introduction to Active
Directory Objects", which brings you to the section of MSDN that explains at
a slightly higher level.

I would recommend you read through the following section (which contains the
above "Introduction to Active Directory Objects"):

http://msdn.microsoft.com/library/d...vboriIntroductionToActiveDirectoryObjects.asp

It also contains a "Searching Active Directory Hierarchies" topic that
should be what you need...

Hope this helps
Jay
 
Back
Top