object reference

If in form1 I use User user = new User(); to create my user object.  When I go to form2 how do I reference the same user object 







Thanks Mike


Answer this question

object reference

  • AllDisplayNamesRBelongToUs

    yes...

    ThisUser.login and User.login are two seperate instances....
    I take it form1 is your startup object...and you are actually trying to get a single instance of a user...that being the case use the singleton class for your user...


    Public Class MySingletonClass
        Private Shared m_User as User
        Public Shared Function MyUser()as User
                If IsNothing(m_User) then
                      m_User = New User
                      Return m_User
                else
                      return m_User
                end If
       end Function
    end Class



     

    Form Code:


    Public Class Form1
        Dim MyUser as User = MySingletonCLass.MyUser        
    end Class   


    Public Class Form2
        Dim MyUser as User = MySingletonClass.MyUser
    end Class



     



  • ChandraP

    Thank you for your patience!  That is exactly what I wanted to do.

    Cheers and happy Monday!

    Mike

  • Kuik

    It's VB, but should help
    Singleton Class



    Public Class MySingletonClass
        Private m_Form1 as Form1
        Public Function MyForm1()as Form1
                If IsNothing(m_Form1) then
                      m_Form1 = new Form1
                      Return m_Form1
                else
                      return m_Form1
                end If
       end Function
    end Class

     


    Form1 Code:


    Public Class Form1
          m_User as New User
          Public Property MyUser() as User
                   Get
                         Return m_User
                   End Get
                   Set(ByVal value As User)
                           m_User = value
                   End Set
           End Property
    end Class   

     


    Form2 Code



    Public Class Form2
       Private Sub Test
             Dim ThisUser as User =MySingletonClass.MyForm1.MyUser
             ....Do Something
       end Sub
    end Class


     


  • Ross Miltenberg

    You either have to a) expose the User as a public property on Form1 and make sure form2 has a reference to Form1 or use the Singleton class to expose variables/classes that will be used throughout the application

  • rgny

    Thanks for the reply. 

    How would I do number 1

    on Form 1

    public User user {

    get

    {

    return user;

    }

    set

    {

    User user = new User();

    }

    }

    Then on Form2 how do I get a reference to form1

    Thanks
    Mike


  • Dub

    yes... sorry about that...edit Singleton Class as follows:

    Private Shared m_Form1 As Form1

    Public Shared Function MyForm1()



  • ripleym

    I have a similar problem but with using a customRoleProvider

    My IsUserInRole method is set to public overrides isUserInrole

    so when i change

    Public Overridable function IsUserIRole(username as string, Rolename as string)

    to

    Shared Overridable function ...

    VS yells at me and says that I can;t share an overridable function any ideas

    where i'm actiually getting the error is here

    Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
    If IsUserInRole(User.Identity, "Administrator") Then
    End If
    End Sub

    and this is my method in my CustomRoleProvider.vb class

    '
    ' RoleProvider.IsUserInRole
    '
    Public Overrides Function IsUserInRole(ByVal username As String, ByVal roleName As String) As Boolean
    Dim UserIsInRole As Boolean = False
    Dim conn As SqlConnection = New SqlConnection(connectionString)
    Dim cmd As SqlCommand = New SqlCommand("select count(8) from UsersInRoles " & _
    " where Username = and Rolename = and ApplicationName = ", conn)
    cmd.Parameters.Add("@username", SqlDbType.VarChar, 255).Value = username
    cmd.Parameters.Add("@Rolename", SqlDbType.VarChar, 255).Value = roleName
    cmd.Parameters.Add("@ApplicationName", SqlDbType.VarChar, 255).Value = ApplicationName
    Try
    conn.Open()
    Dim numRecs As Integer = CType(cmd.ExecuteScalar(), Integer)
    If numRecs > 0 Then
    UserIsInRole = True
    End If
    Catch e As SqlException
    If WriteExceptionsToEventLog Then
    WriteToEventLog(e, "IsUserInRole")
    Else
    Throw e
    End If
    Finally
    conn.Close()
    End Try
    Return UserIsInRole
    End Function
    and at the tope of my default.aspx.vb page im using
    Import CustomRoleProvider
    any help would be greatly appreciated..
    Thanks
    Deano

  • PatrickC

    Thanks so much but I still can't get a property from User

    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click

    MessageBox.Show(ThisUser.login)

    End Sub

    I know the property is set because I can see it on Form1 with
    MessageBox.Show(User.login)

    am I still missing something here   It is not using the same user object that was created in form1


    Thanks again
    Mike


  • Swatanya

    Thanks for the reply.  I copied you code exactly and I am getting a " Reference to a non-shared member requires an object reference."

    on this line

    Dim
    ThisUser As User = MySingletonClass.MyForm1.MyUser

    I imported everything

    Imports singleton_class_test_vb.MySingletonClass

    Imports singleton_class_test_vb.User

    Imports singleton_class_test_vb.Form1

    Imports singleton_class_test_vb.Form2
    and that didn't help.

    Thanks for spoon feeding me this.  I am trying to write good OOP code and I think this will get me past a big hurdle.

    Thanks again for you valuable time.

    Mike


  • object reference