|
Hey,
I need to share variables among forms in the Web session (or application) manner. I used to have some code - which I don't have now- that implements the singleton design pattern and allows adding and retrieving objetcs this way :
Application["MyObject"] = object;
Can you help?
Thanks.
|
|
|
|
|
I would use a static class with a static member with an indexer:
public static class SC
{
private static SettingsClass settings = new SettingsClass();
public static SettingsClass Application
{
get { return settings; }
}
public class SettingsClass
{
Dictionary<string, object> settings = new Dictionary<string,object>();
public object this[string id]
{
get
{
return settings[id];
}
set
{
if (!settings.ContainsKey(id))
settings.Add(id, value);
else
settings[id] = value;
}
}
}
}
add use it this way:
SC.Application["key"] = someObject;
Hope it helps anyhow.
Greetings - Gajatko
Portable.NET is part of DotGNU, a project to build a complete Free Software replacement for .NET - a system that truly belongs to the developers.
|
|
|
|
|
Hello,
Thanks gajatko for your help.
Your code uses generics and SettingsClass that are not available in C# 1.1. How can I rewrite it to make it work in VS2003?
Thanks.
-- modified at 10:25 Monday 24th September, 2007
|
|
|
|
|
Implement System.Collections.DictionaryBase.
public static class SC
{
private static SettingsClass settings = new SettingsClass();
public static SettingsClass Application
{
get { return settings; }
}
public class SettingsClass
{
StringObjectDictionary settings = new StringObjectDictionary();
public object this[string id]
{
get
{
return settings[id];
}
set
{
if (!settings.ContainsKey(id))
settings.Add(id, value);
else
settings[id] = value;
}
}
}
}
class StringObjectDictionary : System.Collections.DictionaryBase
{
public StringObjectDictionary() : base()
{
}
public void Add(string key, object value)
{
base.Dictionary.Add(key, value);
}
public object this[string key]
{
get { return base.Dictionary[key]; }
set { base.Dictionary[key] = value; }
}
public bool ContainsKey(string key)
{
return base.Dictionary.Contains(key);
}
}
Edit: The alternative is use of environment variables (Environment.SetEnvironmentVariable ). They also are session-like.
-- modified at 11:16 Monday 24th September, 2007
Greetings - Gajatko
Portable.NET is part of DotGNU, a project to build a complete Free Software replacement for .NET - a system that truly belongs to the developers.
|
|
|
|
|
You can also use cookies, whose default expiration time is exactly one session.
Request.Cookies.Add(...);
Request.Cookies[id]...
Greetings - Gajatko
Portable.NET is part of DotGNU, a project to build a complete Free Software replacement for .NET - a system that truly belongs to the developers.
|
|
|
|
|
Have a look at the HttpContext.Current object. You can access your session variables through it, and create some wrapper code around your session variables.
Like this:
using System.Web;
public static class SessionHelper
{
public static string SomeString
{
get { return HttpContext.Current.Session["SomeString"] as string; }
set { HttpContext.Current.Session["SomeString"] = value; }
}
public static int SomeNumber
{
get { return (int)HttpContext.Current.Session["SomeNumber"]; }
set { HttpContext.Current.Session["SomeNumber"] = value; }
}
}
|
|
|
|
|
Why have you posted a question on Web Forms in the Win Forms section?
Deja View - the feeling that you've seen this post before.
|
|
|
|
|
Hello,
In fact i've been misunderstood. I am talking about Winforms not Webforms.
You know that in Webforms you have predfined classes that allows the persistence of variables (or objects) and sharing them between Webforms (Application and Session classes).
What I want is to implement something similar in Winforms, i.e declaring some variable in Form1, and using it in Form2 .. FormN..
I guess that the first answer of gajatko would help.
Thank you.
|
|
|
|
|
You can use public variables in this manner,
public static class Global
{
public static string fromForm;
}
you can assign a value to fromForm variable in form1 and you can use it in form2
another way is declare public variable in form2
and pass a value to form2 as a parameter when you call form2 from form1
-- modified at 2:01 Thursday 27th September, 2007
|
|
|
|
|
hi everybody, how u doin ?
i have a problem, i created many projects seperatly and i want to combine them in one with a main form with buttons that links to them.
i'm adding the projects using File->Add->Existing Project, and then i'm adding a reference to them, but still i can't access the forms.
any one can help me out ?
thanks in advance.
Know thyself
|
|
|
|
|
I'm just trying to do this and I have no problems. Remember that a class from other projects may be in an another namespaces. Also, if a name conflict occurs then the class from the main assembly are used.
Greetings - Gajatko
Portable.NET is part of DotGNU, a project to build a complete Free Software replacement for .NET - a system that truly belongs to the developers.
|
|
|
|
|
the thing is that the projects are small and there are no classes, only functions and modules inside the forms' code. and each application has its own functionalities, datasets and reports, nothing that big. so i just want to link from a main form (also created seperatly) to all of the others.
Know thyself
|
|
|
|
|
Which programming language do you use? All .NET forms are classes inheriting from the System.Windows.Forms.Form .
Greetings - Gajatko
Portable.NET is part of DotGNU, a project to build a complete Free Software replacement for .NET - a system that truly belongs to the developers.
|
|
|
|
|
the forms are written using vb.net
to be specific, i wrote another project and i added the projects blablabla...
i want to be able to do the following while i'm in the new project:
Project.Form.show()
can i do this just by adding the project and adding a reference to it?
Know thyself
|
|
|
|
|
Yes you can! I just did it again and added a button to a form:
private void button1_Click(object sender, EventArgs e)
{
WindowsApplication1.Form1 frm = new WindowsApplication1.Form1();
frm.Show();
}
which would look in VB.NET like this:
Private Sub button1_Click(sender as object, e as EventArgs) handles button1.click
frm as new WindowsApplication1.Form1
frm.Show()
End Sub
Greetings - Gajatko
Portable.NET is part of DotGNU, a project to build a complete Free Software replacement for .NET - a system that truly belongs to the developers.
|
|
|
|
|
Hi,
Iam using Windows based DatagridView..So i want to display the Child View..
and iam adding the TextLinkColumn..So just if i click the TextLinkColumn and displaying the related records in Child DataGridView..
Here i done the coding for displaying child dataGridview..So iam geting error in this Coding part...
already i've done the RelationShip to dataBase Tables
Error is:"Child list for field APPLICATION_DETAILS cannot be created."
here...Declare dtTable- DataTable
private void LocationView(int applicationID)
{
this.Cursor= Cursors.WaitCursor;
APPLICATION locationForm=new APPLICATION();
CurrencyManager cm = ((CurrencyManager)this.BindingContext[dtTable, "APPLICATION_DETAILS.APPLICATION_DETAILSAPPLICATION"]);
IBindingListView list=(IBindingListView)cm.List;
locationForm.ShowDialog(applicationID, this, list);
this.Cursor=Cursors.Default;
}
Pls tel me..now critical situation..
Pls need help..
Somu
|
|
|
|
|
Please can you help me out in this.....
I am having trouble in changing my checkbox backcolor into red when CheckedState is checked, yellow when it's undeterminate and unchecked turns to normal color.
CheckBox Property:
1.ThreeState = true //unchecked,undeterminate,checked
2.CheckedState = unchecked; //unchecked,undeterminate,checked
3.BackColor = System.Control; //normal color
code for the first CheckedState..
checkBoxName.checked = true;
checkBoxName.BackColor = Color.Red;
|
|
|
|
|
All you have to do is handle the CheckStateChanged event of the CheckBox and check the value of the CheckBox's CheckState property. Set the BackColor accordingly.
|
|
|
|
|
I'm trying to add a StringCollection to the resources of my application.
In the VS Resource Editor there is a section called "Other" and accourding to the MSDN article it is possible to add any type that supports serialization, but I'm unable to do it. The grid is inactive.
Does anybody know a solution to the problem?
|
|
|
|
|
Hi,
Please try the following steps toadd a string resource.
1) With a project selected in Solution Explorer, on the Project menu, click Properties.
2) Click the Resources tab. On the Resource Designer toolbar, point to the resource view drop-down, click the arrow, and make sure it is set to Strings (which is the default). A settings grid will appear, displaying the strings maintained by that instance of the Resource Designer.
3) Click on the Name column of the last row in the grid, which is marked with an asterisk (*).
4) In the Name column, enter a name for the string.
5) In the Value column, enter the string to be used as a resource.
6) Optionally, enter a comment in the Comment column.
Hope this helps .
Regards,
John Adams
ComponentOne LLC
|
|
|
|
|
I am using Windows DataGridView....
How to Display the Parent With Child View
iam using TextLinkColumn..So if i Click the TextLinkColumn and displaying the Child View from another Table.
So Pls any example...
pls tel me..
Regards
Somu
|
|
|
|
|
internal Lybra.Forms.Controls.GradientPanelXP pnlHeader;
i declared like this
but i got error.
the type or namespace name"lybra"couldnot be found...
what should i do?
|
|
|
|
|
the compiler is saying it doesn't know what the Lybra namespace is.. make sure that you have a reference added to your project to the DLL you are trying to use. If you don't have the DLL, or don't know what I mean, then you might want to talk to the creator of the control you are trying to use.
|
|
|
|
|
You have to add a reference to the lybra namespace, as the other poster said.
"I really like comments where I don't have to answer stupid questions" - stfx
|
|
|
|
|
Actually.i want to display the Child Datagrid form Access Database table
iam using the coding:
CurrencyManager cm = ((CurrencyManager)this.BindingContext [dtTmp,"APPLICATION_DETAILS.APPLICATION_DETAILSAPPLICATION"]);
Iam getting this Error!!!!
"Child list for field APPLICATION_DETAILS cannot be created."
Pls tell me
somasundaram
|
|
|
|