473,372 Members | 954 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,372 software developers and data experts.

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

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
Aug 12 '05 #1
4 1971
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/

Aug 13 '05 #2
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

Aug 13 '05 #3
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"
Aug 15 '05 #4
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
Aug 15 '05 #5

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

2
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...
21
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...
10
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....
6
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...
0
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...
9
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...
5
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() {
3
weaknessforcats
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...
2
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"...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...
0
by: ryjfgjl | last post by:
In our work, we often need to import Excel data into databases (such as MySQL, SQL Server, Oracle) for data analysis and processing. Usually, we use database tools like Navicat or the Excel import...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
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 using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.