Click here to Skip to main content
15,891,905 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I am writing code n MVVM model. Each laser is separate project, but ALL of them Are Within one Solution. Ta the bggining user have to login. Later on, I have to record user name into SQL Table, but I don’t want that user have to write IT manually again, but to use username which was entered during login proces.
Does anybody can help.

What I have tried:

I tried to make poblc string varijable in UI Layer, but the Logic for recording Data is in Model layer, which doesn’t have reference to UI layer, but oposite.

Thank you for your assistance.
Posted
Updated 27-Jun-19 4:07am
Comments
BillWoodruff 21-Jun-19 2:27am    
WPF ? or ?
Sinisa Janjetovic 21-Jun-19 2:51am    
WPF

Add static class and make it public, for example:

C#
public static class MyDataHelper
{
    public static string username {get; set;}
   //other members, fields, methods
}


Now, you're able to use it in entire project.

Usage:
C#
//write
MyDataHelper.username = "Maciej";
//read
Console.WriteLine(MyDataHelper.username);
 
Share this answer
 
v3
Comments
Sinisa Janjetovic 21-Jun-19 5:15am    
"Cannot declare instance members" in a static class is error message which I get by using this code
Maciej Los 21-Jun-19 5:21am    
Instance member? What are you trying to do?
Sinisa Janjetovic 21-Jun-19 5:22am    
I just created new class library and copied your code, just changed the name of it and variable name
Sinisa Janjetovic 21-Jun-19 5:31am    
namespace PomocniParametri
{
public static class PomocniParametri
{
public static string KorisnikAplikacije { get; set; }
//other members, fields, methods
}
}

This is proper, it seems
Maciej Los 21-Jun-19 5:40am    
It looks good. I forgot to add "static" word within "username" variable.
Or,
App.Current.Properties["Username"] = "Someone";
See: [^]
 
Share this answer
 
Comments
Sinisa Janjetovic 21-Jun-19 4:02am    
In which file should I put this line?
BillWoodruff 21-Jun-19 5:01am    
try MainWindow.xaml.cs ... or, the same place you put the static class.

you'll need to cast to get the stored object back to a string:

string uname = (string) App.Current.Properties["Username"];
Sinisa Janjetovic 21-Jun-19 5:01am    
thnx
Sinisa Janjetovic 21-Jun-19 5:10am    
Sorry, I didn't manage.
Neither with first answer, neither with your. Probably I don't know how to do that
BillWoodruff 22-Jun-19 1:46am    
When you created your project: what version of Visual Studio are you using ? What version of .NET FrameWork ? The name of the Project Type ?
(1)from login page:
declare --> public static string username="";

(2)inside a function to get access to next page
assign username=whatever the textBox.Text

(3)in any all the pages that you want to use username
assign it to the variable that you have declared ...eg string name=login.username;

Hope it will solve your probles
 
Share this answer
 
Comments
Maciej Los 28-Jun-19 2:51am    
This is not a WWW (ASP.NET) project!

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900