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 4 1977
Hi
Looks like you are getting messy somewhere.In form1 you are creating
object of User class which is not shared. In form2 you are trying to
access MySingletonClass which is definetly shared but the user objects
are different.
I think just make the user class property shared everything will be
fine. If you want solve it by this way then
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles Button1.Click
MyUser1.login = Me.TextBox1.Text.ToString
// Add this
MySingletonClass.MyForm1.MyUse*r = Myuser1
Dim Form2 As New Form2
Form2.ShowDialog()
End Sub
End Class
Shivprasad Koirala
C# , VB.NET , SQL SERVER , ASP.NET Interview Questions http://www.geocities.com/dotnetinterviews/
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" <Mi**@discussions.microsoft.com> wrote in message
news:B3**********************************@microsof t.com... 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
Singleton pattern (in C#):
public class MySingleton
{
private static MySingleton m_Singleton = null;
static MySingleton() { m_Singleton = new MySingleton(); }
private MySingleton() { /* initialisation code */ }
public static MySingleton Singleton
{
get
{
if(m_Singleton == null) { m_Singleton = new MySingleton(); }
return(m_Singleton);
}
}
}
Use this from code :
MySingleton ms = MySingleton.Singleton;
--
--
Of all words of tongue and pen, the saddest are: "It might have been"
billr <bi***@discussions.microsoft.com> wrote: Singleton pattern (in C#):
public class MySingleton { private static MySingleton m_Singleton = null; static MySingleton() { m_Singleton = new MySingleton(); } private MySingleton() { /* initialisation code */ } public static MySingleton Singleton { get { if(m_Singleton == null) { m_Singleton = new MySingleton(); } return(m_Singleton); } } }
Use this from code :
MySingleton ms = MySingleton.Singleton;
That's a rather long-winded way of doing it - why bother with the
nullity check in the property when you've got a static constructor?
You might also consider moving the contents of the static constructor
to the declaration (depending on how lazy you want the initialization
to be) and perhaps even making it a public static readonly field.
--
Jon Skeet - <sk***@pobox.com> http://www.pobox.com/~skeet
If replying to the group, please do not mail me too This thread has been closed and replies have been disabled. Please start a new discussion. Similar topics
by: Sean Dettrick |
last post by:
Hi,
apologies for yet another Singleton posting.
From reading around previous postings etc, I've cobbled together two
Singleton template definitions - one that returns a reference, and one
that...
|
by: Sharon |
last post by:
I wish to build a framework for our developers that will include a singleton
pattern.
But it can not be a base class because it has a private constructor and
therefore can be inherit.
I thought...
|
by: Opa |
last post by:
I have tried for two days to solve this problem with no luck.
I have a singleton class which has some events declared. When I inherit
from this class the events don't seem to come along with it....
|
by: Mike |
last post by:
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...
|
by: Harry Smith |
last post by:
This was posted in news.groups, but it was not posted to the list.
REQUEST FOR DISCUSSION (RFD)
unmoderated group comp.databases.postgresql.admin
unmoderated group...
|
by: Marcel Hug |
last post by:
Hallo NG !
I Have a little question about inheritance of a singleton class.
In my application i have a Database-Connection Lib, in which I would ¨like
to connect different databases of the same...
|
by: Damien |
last post by:
Hi all,
I'm using a pretty standard C++ Singleton class, as below:
template <typename T>
class Singleton
{
public:
static T* Instance()
{
|
by: weaknessforcats |
last post by:
Design Pattern: The Singleton
Overview
Use the Singleton Design Pattern when you want to have only one instance of a class. This single instance must have a single global point of access. That...
|
by: Bob Johnson |
last post by:
Just wondering the extent to which some of you are implementing classes as
Singletons. I'm working on a brand new project and, early on, identified
some obvious candidates. By "obvoius candidates"...
|
by: nemocccc |
last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
|
by: Sonnysonu |
last post by:
This is the data of csv file
1 2 3
1 2 3
1 2 3
1 2 3
2 3
2 3
3
the lengths should be different i have to store the data by column-wise with in the specific length.
suppose the i have to...
|
by: Hystou |
last post by:
There are some requirements for setting up RAID:
1. The motherboard and BIOS support RAID configuration.
2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
|
by: marktang |
last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
|
by: Hystou |
last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
|
by: Oralloy |
last post by:
Hello folks,
I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>".
The problem is that using the GNU compilers,...
|
by: Hystou |
last post by:
Overview:
Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
|
by: tracyyun |
last post by:
Dear forum friends,
With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
|
by: conductexam |
last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...
| |