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.
InitializeCompo nent()
'Add any initialization after the InitializeCompo nent() 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.Disp ose()
End If
End If
MyBase.Dispose( disposing)
End Sub
'Required by the Windows Form Designer
Private components As System.Componen tModel.IContain er
'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.Diagnos tics.DebuggerSt epThrough()> Private Sub
InitializeCompo nent()
Me.Button1 = New System.Windows. Forms.Button
Me.TextBox1 = New System.Windows. Forms.TextBox
Me.SuspendLayou t()
'
'Button1
'
Me.Button1.Loca tion = New System.Drawing. Point(88, 136)
Me.Button1.Name = "Button1"
Me.Button1.Size = New System.Drawing. Size(120, 32)
Me.Button1.TabI ndex = 0
Me.Button1.Text = "Button1"
'
'TextBox1
'
Me.TextBox1.Loc ation = New System.Drawing. Point(64, 56)
Me.TextBox1.Nam e = "TextBox1"
Me.TextBox1.Siz e = New System.Drawing. Size(168, 20)
Me.TextBox1.Tab Index = 1
Me.TextBox1.Tex t = "TextBox1"
'
'Form1
'
Me.AutoScaleBas eSize = 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(ByVa l sender As System.Object, ByVal e As
System.EventArg s) Handles MyBase.Load
End Sub
Private Sub Button1_Click(B yVal sender As System.Object, ByVal e As
System.EventArg s) Handles Button1.Click
MyUser.login = Me.TextBox1.Tex t.ToString
Dim Form2 As New Form2
Form2.ShowDialo g()
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.
InitializeCompo nent()
'Add any initialization after the InitializeCompo nent() 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.Disp ose()
End If
End If
MyBase.Dispose( disposing)
End Sub
'Required by the Windows Form Designer
Private components As System.Componen tModel.IContain er
'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.Diagnos tics.DebuggerSt epThrough()> Private Sub
InitializeCompo nent()
Me.frm2textbox = New System.Windows. Forms.TextBox
Me.Button1 = New System.Windows. Forms.Button
Me.SuspendLayou t()
'
'frm2textbox
'
Me.frm2textbox. Location = New System.Drawing. Point(64, 80)
Me.frm2textbox. Name = "frm2textbo x"
Me.frm2textbox. Size = New System.Drawing. Size(168, 20)
Me.frm2textbox. TabIndex = 0
Me.frm2textbox. Text = "TextBox1"
'
'Button1
'
Me.Button1.Loca tion = New System.Drawing. Point(88, 152)
Me.Button1.Name = "Button1"
Me.Button1.Size = New System.Drawing. Size(112, 32)
Me.Button1.TabI ndex = 1
Me.Button1.Text = "Button1"
'
'Form2
'
Me.AutoScaleBas eSize = 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(ByVa l sender As System.Object, ByVal e As
System.EventArg s) Handles MyBase.Load
End Sub
Private Sub Button1_Click(B yVal sender As System.Object, ByVal e As
System.EventArg s) Handles Button1.Click
Dim ThisUser As User = MySingletonClas s.MyForm1.MyUse r
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 MySingletonClas s
Private Shared m_Form1 As Form1
Public Shared Function MyForm1()
If IsNothing(m_For m1) Then
m_Form1 = New Form1
Return m_Form1
Else
Return m_Form1
End If
End Function
End Class
Was this post helpful to 4 2008
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 MySingletonClas s 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(B yVal sender As System.Object, ByVal e As
System.EventArg s) Handles Button1.Click
MyUser1.login = Me.TextBox1.Tex t.ToString
// Add this
MySingletonClas s.MyForm1.MyUse *r = Myuser1
Dim Form2 As New Form2
Form2.ShowDialo g()
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(B yVal sender As System.Object, ByVal e As System.EventArg s) Handles Button1.Click
MyUser.login = Me.TextBox1.Tex t.ToString
Dim Form2 As New Form2
Form2.SetMyLogi n(Me.TextBox1.T ext) ' <-- add this line...
and add a method to Form2 to get this value. Form2.ShowDialo g() ' <-- 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**@discussio ns.microsoft.co m> wrote in message
news:B3******** *************** ***********@mic rosoft.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. InitializeCompo nent()
'Add any initialization after the InitializeCompo nent() 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.Disp ose() End If End If MyBase.Dispose( disposing) End Sub
'Required by the Windows Form Designer Private components As System.Componen tModel.IContain er
'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.Diagnos tics.DebuggerSt epThrough()> Private Sub InitializeCompo nent() Me.Button1 = New System.Windows. Forms.Button Me.TextBox1 = New System.Windows. Forms.TextBox Me.SuspendLayou t() ' 'Button1 ' Me.Button1.Loca tion = New System.Drawing. Point(88, 136) Me.Button1.Name = "Button1" Me.Button1.Size = New System.Drawing. Size(120, 32) Me.Button1.TabI ndex = 0 Me.Button1.Text = "Button1" ' 'TextBox1 ' Me.TextBox1.Loc ation = New System.Drawing. Point(64, 56) Me.TextBox1.Nam e = "TextBox1" Me.TextBox1.Siz e = New System.Drawing. Size(168, 20) Me.TextBox1.Tab Index = 1 Me.TextBox1.Tex t = "TextBox1" ' 'Form1 ' Me.AutoScaleBas eSize = 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(ByVa l sender As System.Object, ByVal e As System.EventArg s) Handles MyBase.Load
End Sub
Private Sub Button1_Click(B yVal sender As System.Object, ByVal e As System.EventArg s) Handles Button1.Click
MyUser.login = Me.TextBox1.Tex t.ToString
Dim Form2 As New Form2 Form2.ShowDialo g() 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. InitializeCompo nent()
'Add any initialization after the InitializeCompo nent() 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.Disp ose() End If End If MyBase.Dispose( disposing) End Sub
'Required by the Windows Form Designer Private components As System.Componen tModel.IContain er
'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.Diagnos tics.DebuggerSt epThrough()> Private Sub InitializeCompo nent() Me.frm2textbox = New System.Windows. Forms.TextBox Me.Button1 = New System.Windows. Forms.Button Me.SuspendLayou t() ' 'frm2textbox ' Me.frm2textbox. Location = New System.Drawing. Point(64, 80) Me.frm2textbox. Name = "frm2textbo x" Me.frm2textbox. Size = New System.Drawing. Size(168, 20) Me.frm2textbox. TabIndex = 0 Me.frm2textbox. Text = "TextBox1" ' 'Button1 ' Me.Button1.Loca tion = New System.Drawing. Point(88, 152) Me.Button1.Name = "Button1" Me.Button1.Size = New System.Drawing. Size(112, 32) Me.Button1.TabI ndex = 1 Me.Button1.Text = "Button1" ' 'Form2 ' Me.AutoScaleBas eSize = 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(ByVa l sender As System.Object, ByVal e As System.EventArg s) Handles MyBase.Load
End Sub
Private Sub Button1_Click(B yVal sender As System.Object, ByVal e As System.EventArg s) Handles Button1.Click Dim ThisUser As User = MySingletonClas s.MyForm1.MyUse r 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 MySingletonClas s Private Shared m_Form1 As Form1 Public Shared Function MyForm1() If IsNothing(m_For m1) 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_Single ton);
}
}
}
Use this from code :
MySingleton ms = MySingleton.Sin gleton;
--
--
Of all words of tongue and pen, the saddest are: "It might have been"
billr <bi***@discussi ons.microsoft.c om> 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_Single ton); } } }
Use this from code :
MySingleton ms = MySingleton.Sin gleton;
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.co m> 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 returns a pointer. I understand the pointer one and it works,
but the reference one doesn't work as I expect. Can anybody explain
this to me? (Code below).
I'm not really looking for thread safety or for something as general
|
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 maybe a Template can be use for that, but C# does not support
Templates (will be C# generics in mid 2005).
Does anyone have a solution on how the singleton pattern can be written, in
C#, as a framework/ infrastructure class, so users can use this...
|
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.
The event in the singleton class always evaluates to null even when I cleary
subscribe to it.
Does anyone have some sample code of how to do this, or is this not possible.
|
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 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
|
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 comp.databases.postgresql.general
unmoderated group comp.databases.postgresql.hackers
unmoderated group comp.databases.postgresql.novice
unmoderated group comp.databases.postgresql.sql
This is a formal Request For Discussion (RFD) for the creation of
| |
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 project (thats why it is in a
lib).
For the database A I created a singleton class AProxy and for the database B
the same as BProxy. Initializing and connetcing to the Database is the same
in both classes (except of the database-path, which is a...
|
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 is, regardless of where the object is hidden, everyone needs access to it.
The global point of access is the object's Instance() method.
Individual users need to be prevented from creating their own instances of the Singleton.
|
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" I mean classes for which
terrible problems would clearly arise if more than one instance were to
exist. But as I'm getting into the design of this new solution, I'm
realizing that a large percentage of the classes _could be_ implemented as...
|
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look !
Part I. Meaning of...
|
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it.
First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
| |
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed.
This is as boiled down as I can make it.
Here is my compilation command:
g++-12 -std=c++20 -Wnarrowing bit_field.cpp
Here is the code in...
|
by: jinu1996 |
last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth.
The Art of Business Website Design
Your website is...
|
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 Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
|
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 protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
|
by: isladogs |
last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 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 a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules.
He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms.
Adolph will...
|
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 then checking html paragraph one by one.
At the time of converting from word file to html my equations which are in the word document file was convert into image.
Globals.ThisAddIn.Application.ActiveDocument.Select();...
| |
by: bsmnconsultancy |
last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...
| |