I've a file SiteMinder.CS in App_code where I set the UserID who has accessed the webpage
public class SiteMinder : IHttpModule
{
public string UserID { get; set; }
public void Init(HttpApplication context)
{
context.PreRequestHandlerExecute += new EventHandler(Application_PreRequestHandler);
}
private void Application_PreRequestHandler(Object source, EventArgs e)
{
if (HttpContext.Current.Request.Headers != null)
{
NameValueCollection coll = HttpContext.Current.Request.Headers;
UserID = coll["uid"]; // Doesn't have NULL value
}
}
}
In another webpage UserDetails.aspx.cs file I'm trying to access this UserID but it is having NULL value.
public partial class UserDetails : System.Web.UI.Page
{
protected string SessionUser { get; set; }
protected void Page_Load(object sender, EventArgs e)
{
SiteMinder objSite = new SiteMinder();
SessionUser = objSite.UserID;//Returns NULL
}
}
All these are under same namespace. Please let me know where I'm wrong here.
You're creating a new SiteMinder object. That's not the same object that had the property set on it, so the property will have the default value (null).
You need to obtain a reference to the original SiteMinder object which set the property - or store the value somewhere else (such as the HttpContext).
Related
I'm very new to c#, I started a few days ago, so please excuse me if it is basic.
I have two forms, the first one is like a login page, where someone enters their name. On my "Info.cs" class, it reads this name via a setter, into a variable, and my Getter called "GetCardName" returns this Name. I now made a new form where I want to access this name via the GetCardName getter, just dont know how too. Heres the code :
Here is some of the "info.cs" class code:
private string CardName { get; set; } = "";
public string GetCardName()
{
return this.CardName;
}
public void SetName(string name = "")
{
this.CardName = name;
}
And here is the code from the other form that is just trying to call GetCardName():
private void Form2_Load(object sender, EventArgs e)
{
lblWelcome.Text = Info.GetCardName();
}
When creating Form2 you need also pass it reference to the other form to get its properties.
So when creating and showing Form1 you should also create Form2 to pass that reference. Example (not tested) code:
var form1 = new Form1();
var form2 = new Form2(form1);
form1.Show();
and Form2 should be like:
public class Form2
{
private Form1 _form1;
public Form2(Form1 form1)
{
_form1 = form1;
// ... other initialization code
}
// ... other class declarations
}
General solution is: you need to persist reference to the Form1 being shown to the user and then pass that reference to Form2 whenever you create it.
You have two options :
you can create an instance of the class that you want to call
EX : Info infoVar = new Info(); (now you can use infoVar to call any methods of the Info.cs class)
you can make Info class a STATIC class (probably not what you want to do, but still helpful for the future perhaps) This makes it possible to call the info class directly without having to create a variable of that class but has some drawbacks. (more info here)
There is few ways to achieve what you want:
Public static property:
public class Info
{
public static string CardName { get; set; } = string.Empty;
}
You can access it or set value to it directly by:
private void Form2_Load(object sender, EventArgs e)
{
// Set
Info.CardName = "Some name";
// Get
lblWelcome.Text = Info.CardName;
}
Public non-static property:
public class Info
{
public string CardName { get; set; } = string.Empty;
}
You can access it or set value to it directly too, but need to create Info class instance before:
private void Form2_Load(object sender, EventArgs e)
{
Info info = new Info();
// Set
info.CardName = "Some name";
// Get
lblWelcome.Text = info.CardName;
}
Private static field with separated public static Get and Set methods:
public class Info
{
private static string cardName = string.Empty;
public static string GetCardName()
{
return cardName;
}
public static void SetCardName(string name = "")
{
cardName = name;
}
}
You can access GetCardName and SetCardName without creating Info class instance:
private void Form2_Load(object sender, EventArgs e)
{
// Set
Info.SetCardName("Some name");
// Get
lblWelcome.Text = Info.GetCardName();
}
Private non-static field with separated public non-static Get and Set methods:
public class Info
{
private string cardName = string.Empty;
public string GetCardName()
{
return cardName;
}
public void SetCardName(string name = "")
{
cardName = name;
}
}
You can access GetCardName and SetCardName after creating Info class instance:
private void Form2_Load(object sender, EventArgs e)
{
Info info = new Info();
// Set
info.SetCardName("Some name");
// Get
lblWelcome.Text = info.GetCardName();
}
Difference between fields and properties was pretty nice explained here: What is the difference between a field and a property?. In short, properties are "wrappers" over fields, which usually are private and you can't access to them directly or modify. It is a part of Member Design Guidelines. Also properties allow to add some validations through property setter to be sure that valid value is stored at cardName field, e.g.:
public class Info
{
private string cardName = string.Empty;
public string CardName
{
get => cardName;
set
{
// Check that value you trying to set isn't null
if (value != null)
cardName = value;
// Or check that name is not too short
if (value.Length >= 3) // Card name should be at least of 3 characters
cardName = value;
}
}
}
info myInfo=new info();
lblWelcome.Text = myInfo.GetCardName();
Am passing a parameter as a way to allow a user to go back and make changes
private void go_back_btn_Click(object sender, RoutedEventArgs e)
{
Frame.Navigate(typeof(TruckRegistrationPage), this.truckdetails);
}
Now on the trruck registration page
protected override void OnNavigatedTo(NavigationEventArgs e)
{
base.OnNavigatedTo(e);
this.changeddetails= (TruckRegistrationDetails)e.Parameter;
//set the form fields based on the details
if (e.Parameter) //this throws an error of boolean not casted
{
truck_reg_no.Text = changeddetails.reg_no;
transporter_name.Text = truckdetails.owner_id;
.......assign other xaml controls
}
}
The parameters am passing are of type TruckRegistrationDetails whic is a class containing properties as below
class TruckRegistrationDetails
{
public int id { get; set; }
public string reg_no { get; set; }
public int truck_category { get; set; }
.......others
}
How do i check to see if any parameters have been passed and hence assign the xaml controls value
Change your code to this
protected override void OnNavigatedTo(NavigationEventArgs e)
{
base.OnNavigatedTo(e);
changeddetails = (TruckRegistrationDetails)e.Parameter;
if (changeddetails != null)
{
truck_reg_no.Text = changeddetails.reg_no;
//do what ever you want ^^
}
}
Your check was for a boolean, but e.Parameter is an object. Here is the link to MSDN https://msdn.microsoft.com/de-de/library/windows/apps/windows.ui.xaml.navigation.navigationeventargs.parameter.aspx
I was getting an error using this approach as at some point OnNavigatedTo is called with a NavigationEventArgs.Parameter of an empty string. This was causing a Cast exception when the object is cast to the object type I passed.
I used:
if (args.Parameter is DeviceInformation)
{
DeviceInformation deviceInfo = (DeviceInformation)args.Parameter;
//Do something with object
}
This checks the type of object first to see if it matches the expected first, then the cast will not throw an exception.
As the title states I'm looking for a way how to solve following:
namespace Test
{
public partial class SetVariable : Form
{
public string test = "";
public SetVariable()
{
InitializeComponent();
}
private void button2_Click(object sender, EventArgs e)
{
test = "test"
}
}
}
and in a second form I want to read it, but also want to restrict the user from making any changes to the variable (by accident or on purpose), as all the variables are only to be set in the SetVariable Form, and then be used across all other forms that are planned.
namespace Test
{
public partial class GetVariable : Form
{
public GetVariable()
{
InitializeComponent();
}
private void button2_Click(object sender, EventArgs e)
{
if (SetVariable.test == "test")
{ //doSomething;}
}
}
}
}
If I make the variable a public readonly, than I cant write to it in the form where its supposed to be written. Is there another way of initalizing a global variable which is only changable in the form where its created?
Thanks in advance.
Change:
public string test = "";
to:
public string test { get; private set; }
Also see https://stackoverflow.com/a/3847982/34092 .
Make public property private set.
public partial class SetVariable : Form
{
public string Test {get; private set;}
//Just in case if you want to set value to Test property from other class.
//If you want Test property readonly to other
//class you don't need this method.
public void SetTest(string test)
{
Test = test;
}
}
public class Main
{
SetVariable sv = new SetVariable();
sv.SetTest("Some Value"); //unwanted to scenario. Just in case if you want
//read Test value
string testValue = sv.Test; //allowed
//set Test value
sv.Test = "Other value"; //not allowed.
}
I have a class that has multiple properties. What I want to have is a value set to a particular property if there is no value passed to it. For instance, if txtFather.Text is blank, then the _theFather property of a class should have a default value of "N/A". Here is the code I have so far:
class StudentModel
private string _theFather = "N/A";
public string SetFather
{
get { return _theFather; }
set { _theFather = value; }
}
public void InsertStudent(StudentModel student)
{
MessageBox.Show(_theFather);
}
class AddStudent
private void button1_Click(object sender, EventArgs e)
{
var sm = new StudentModel()
{
SetFather = txtFathersName.Text
};
sm.InsertStudent(sm);
}
If I place a value in the txtFather.Text, I get the its value in the StudentModel Class but if I leave the txtFather.Text blank, I don't get the "N/A" value. I just get a blank or no value at all.
Thanks for your help.
I would encapsulate the logic within the StudentModel class by checking the value being passed to the setter, and only updating your backing field if the string is not null or whitespace.
public string SetFather
{
get { return _theFather; }
set {
if(!string.IsNullOrWhiteSpace(value))
{
_theFather = value;
}
}
}
This way you only have the logic in a single place. If you modify your class' consuming code, then you have to remember to change it everywhere.
You should set the property only if a string is typed, like this:
private void button1_Click(object sender, EventArgs e)
{
var sm = new StudentModel();
if (!string.IsNullOrEmpty(txtFathersName.Text))
sm.SetFather = txtFathersName.Text;
sm.InsertStudent(sm);
}
I have a class called Global.cs:
public class Global
{
private string id= string.Empty;
public string Id
{
get { return id;}
set { id= value; }
}
}
Now in the Main class,
public class Main
{
public Global objGlobal;
protected void Page_Load(object sender, EventArgs e)
{
objGlobal= new Global();
objGlobal.id="XX001";
}
public void Setdata()
{
// Trying to access objGlobal.id value here but it's null
}
}
What am I missing?
Shouldn't you always be getting/setting "Id" rather than "id". As "id" is private.
Well, your XX class instance is more than one time.
If you need to persist some user-retalive info, try storing it into the SessionState.
If you need to just have the static class with some static data, add the static keyword to both class and its members.