Re: singleton class help needed (repost from vsnet.general posted the



The singleton is supposed to contain the object that you want to share.
Your singleton contains Form1. Is Form1 the object you want to share? I
thought that User was the object you wanted to share.

So, to fix the singleton, have it manage the creation of User objects. You
will need to remove any uses of "new User" from any other class (including
Form1, where it occurs about four lines in).

On the other hand, if you just want to pass a value to Form2, a singleton is
a nutty idea.

> Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As
> System.EventArgs) Handles Button1.Click
>
> MyUser.login = Me.TextBox1.Text.ToString
>
> Dim Form2 As New Form2
Form2.SetMyLogin(Me.TextBox1.Text) ' <-- add this line...
and add a method to Form2 to get this value.
> Form2.ShowDialog() ' <-- now,
> in Form_Load for Form2, put the value into the display
> End Sub


HTH,
--
--- Nick Malik [Microsoft]
MCSD, CFPS, Certified Scrummaster
http://blogs.msdn.com/nickmalik

Disclaimer: Opinions expressed in this forum are my own, and not
representative of my employer.
I do not answer questions on behalf of my employer. I'm just a
programmer helping programmers.
--
"Mike" <Mike@xxxxxxxxxxxxxxxxxxxxxxxxx> wrote in message
news:B3049C04-1324-4095-8599-3E9EC30979CA@xxxxxxxxxxxxxxxx
> Please help this is driving me nuts. I have 2 forms, 1 user class and I
> am
> trying to implement a singleton class. Form 1 should create a user object
> and populate some properties in user. Form2 should then have access to
> those
> user properties. I am not getting the property value from user that was
> set
> in form1.
>
> Sorry for posting so much code but I need help bad. How do i make this
> work?
>
> Thanks
> Mike
>
>
> 'Form1.vb
> '--------------------
> Public Class Form1
> Inherits System.Windows.Forms.Form
> Dim 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
>
> #Region " Windows Form Designer generated code "
>
> Public Sub New()
> MyBase.New()
>
> 'This call is required by the Windows Form Designer.
> InitializeComponent()
>
> 'Add any initialization after the InitializeComponent() call
>
> End Sub
>
> 'Form overrides dispose to clean up the component list.
> Protected Overloads Overrides Sub Dispose(ByVal disposing As Boolean)
> If disposing Then
> If Not (components Is Nothing) Then
> components.Dispose()
> End If
> End If
> MyBase.Dispose(disposing)
> End Sub
>
> 'Required by the Windows Form Designer
> Private components As System.ComponentModel.IContainer
>
> 'NOTE: The following procedure is required by the Windows Form Designer
> 'It can be modified using the Windows Form Designer.
> 'Do not modify it using the code editor.
> Friend WithEvents Button1 As System.Windows.Forms.Button
> Friend WithEvents TextBox1 As System.Windows.Forms.TextBox
> <System.Diagnostics.DebuggerStepThrough()> Private Sub
> InitializeComponent()
> Me.Button1 = New System.Windows.Forms.Button
> Me.TextBox1 = New System.Windows.Forms.TextBox
> Me.SuspendLayout()
> '
> 'Button1
> '
> Me.Button1.Location = New System.Drawing.Point(88, 136)
> Me.Button1.Name = "Button1"
> Me.Button1.Size = New System.Drawing.Size(120, 32)
> Me.Button1.TabIndex = 0
> Me.Button1.Text = "Button1"
> '
> 'TextBox1
> '
> Me.TextBox1.Location = New System.Drawing.Point(64, 56)
> Me.TextBox1.Name = "TextBox1"
> Me.TextBox1.Size = New System.Drawing.Size(168, 20)
> Me.TextBox1.TabIndex = 1
> Me.TextBox1.Text = "TextBox1"
> '
> 'Form1
> '
> Me.AutoScaleBaseSize = New System.Drawing.Size(5, 13)
> Me.ClientSize = New System.Drawing.Size(292, 273)
> Me.Controls.Add(Me.TextBox1)
> Me.Controls.Add(Me.Button1)
> Me.Name = "Form1"
> Me.Text = "Form1"
> Me.ResumeLayout(False)
>
> End Sub
>
> #End Region
>
> Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As
> System.EventArgs) Handles MyBase.Load
>
> End Sub
>
> Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As
> System.EventArgs) Handles Button1.Click
>
> MyUser.login = Me.TextBox1.Text.ToString
>
> Dim Form2 As New Form2
> Form2.ShowDialog()
> End Sub
> End Class
>
> 'Form2.vb
> '------------------------
>
> Public Class Form2
> Inherits System.Windows.Forms.Form
>
>
> #Region " Windows Form Designer generated code "
>
> Public Sub New()
> MyBase.New()
>
> 'This call is required by the Windows Form Designer.
> InitializeComponent()
>
> 'Add any initialization after the InitializeComponent() call
>
> End Sub
>
> 'Form overrides dispose to clean up the component list.
> Protected Overloads Overrides Sub Dispose(ByVal disposing As Boolean)
> If disposing Then
> If Not (components Is Nothing) Then
> components.Dispose()
> End If
> End If
> MyBase.Dispose(disposing)
> End Sub
>
> 'Required by the Windows Form Designer
> Private components As System.ComponentModel.IContainer
>
> 'NOTE: The following procedure is required by the Windows Form Designer
> 'It can be modified using the Windows Form Designer.
> 'Do not modify it using the code editor.
> Friend WithEvents frm2textbox As System.Windows.Forms.TextBox
> Friend WithEvents Button1 As System.Windows.Forms.Button
> <System.Diagnostics.DebuggerStepThrough()> Private Sub
> InitializeComponent()
> Me.frm2textbox = New System.Windows.Forms.TextBox
> Me.Button1 = New System.Windows.Forms.Button
> Me.SuspendLayout()
> '
> 'frm2textbox
> '
> Me.frm2textbox.Location = New System.Drawing.Point(64, 80)
> Me.frm2textbox.Name = "frm2textbox"
> Me.frm2textbox.Size = New System.Drawing.Size(168, 20)
> Me.frm2textbox.TabIndex = 0
> Me.frm2textbox.Text = "TextBox1"
> '
> 'Button1
> '
> Me.Button1.Location = New System.Drawing.Point(88, 152)
> Me.Button1.Name = "Button1"
> Me.Button1.Size = New System.Drawing.Size(112, 32)
> Me.Button1.TabIndex = 1
> Me.Button1.Text = "Button1"
> '
> 'Form2
> '
> Me.AutoScaleBaseSize = New System.Drawing.Size(5, 13)
> Me.ClientSize = New System.Drawing.Size(292, 273)
> Me.Controls.Add(Me.Button1)
> Me.Controls.Add(Me.frm2textbox)
> Me.Name = "Form2"
> Me.Text = "Form2"
> Me.ResumeLayout(False)
>
> End Sub
>
> #End Region
>
>
>
>
>
>
> Private Sub Form2_Load(ByVal sender As System.Object, ByVal e As
> System.EventArgs) Handles MyBase.Load
>
>
>
>
> End Sub
>
> Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As
> System.EventArgs) Handles Button1.Click
> Dim ThisUser As User = MySingletonClass.MyForm1.MyUser
> MessageBox.Show(ThisUser.login)
> End Sub
> End Class
>
> 'user.vb
> '----------------------------
> Public Class User
> 'private fields exposed as properties
> Private _login As String
>
> Public Property login() As String
> Get
> Return _login
> End Get
> Set(ByVal Value As String)
> _login = Value
> End Set
> End Property
> End Class
>
> 'MySingleton.vb
> '------------------------
>
> Public Class MySingletonClass
> Private Shared m_Form1 As Form1
> Public Shared Function MyForm1()
> If IsNothing(m_Form1) Then
> m_Form1 = New Form1
> Return m_Form1
> Else
> Return m_Form1
> End If
> End Function
> End Class
> Was this post helpful to
>
>


.