c#


Reference object in two classes C#


Let's go straight to my probably fairly simple problem.
I have a LoginService class in my Services folder which makes a connection with the server. I have another UserModel where I want to receive information from the server. In order not to login again (which would be stupid), I need to maintain the client reference in both files having the same value. In other words, I need to be able to access the same object from a different class (make another reference).
I have tried and tried and searched but I am missing something.
A fairly similar post that I found that still didn't solve mine is this.
The code in my LoginService:
namespace App_Name.Services
{
class LoginService
{
public static Class_Name client;
public async Task MakeConnectionAsync(string userToken)
{
client = new Class_Name();
PasswordVault vault = new PasswordVault();
await client.LoginAsync(TokenType.User, userToken);
await client.StartAsync();
}
So now I want to get the user avatar on my UserModel.cs:
namespace App_Name.Models
{
class UserModel
{
public string GetAvatar()
{
return LoginService.client.CurrentUser.GetAvatarUrl();
}
But it always gives an exception because it tries but there is no connection.
I am sure that it was Connected because in order to load the UserModel it has to be a successful connection.
Any ideas ?
When you call directly the client.CurrentUser.GetAvatarUrl() method, its not determined by the LoginService class. You should be create the Login class before the usage. Also you are using async tasks on your LoginService class, you must confirm that already create user by your UserModel class.
For best practice you can create instance with your constructor like this.
static class LoginService
{
public static Class_Name client;
static LoginService()
{
client = new Class_Name();
}
If you want to go with static (which I not prefer for service level) not use static for like this purpose of usage.
Firstly you should be check dependency injection concepts; i suggest unity and structuremap containers. You can create your consume services by your classes without any object null ref. exception. Dependency injection decrease on coupling and null reference exception.
Ehmmm, for anyone that can use this as a reference, my code above is just fine.
The problem was with the connection API not returning the status immediately. A delay of 2 seconds solved my problem. Thanks everyone for their help.

Related Links

AssetBundle Upgrade
Databinding to an Object with a property that's a string array
How can i use isPressed in Label?
Create application that starts with two forms, instead of usual one
C# XNA - Random Room Generation
How to make a label show a double in vs 2012 c#
How do I get Aspects applied to certain element (e.g. for LocationInfo type parameter, or PropertyInfo)?
Can Resharper Intellisense be configured to sort alphabetically?
Parse XML Document to obtain c# code
Ability to click on a button as part of a background style WPF
problems with crop image in Windows Phone 8.1
Navigation to another page using swipe gesture (Windows Phone 8)
Don't the expression need to be faster for getting a property value than the reflection?
EF Lazy loading not working
Asp.net- Mvc Complex Model Binding
RavenDb returns bad request could not figure out what to do at second API call

Categories

HOME
grizzly
typo3
pyspark
snappy
azure-eventhub
postsharp
livecharts
spotfire
psexec
atlassian-fisheye
unreal-engine4
tracking
swift3.1
web-testing
owncloud
filehelpers
wallet
google-calendar-api3
audio-streaming
ajaxcontroltoolkit
ejb-3.1
fibonacci
gnu-screen
my.cnf
visual-studio-community
scripting-language
deferred
keil
storefront
eclipse-jdt
azure-storage-files
jquery-inputmask
aura-framework
datagridviewcombobox
head
nshttpurlresponse
ocpjp
synchronized
anonymous-function
import.io
perldoc
boost-regex
business-catalyst
sbt-proguard
pyshark
custom-build-step
nonblocking
opendolphin
pagecontrol
transparentproxy
glog
system.diagnostics
cl.exe
report-builder2.0
unityvs
illegalargumentexception
apache-pivot
nsurlrequest
xmlserializer
vundle
switchpreference
episerver-7
code-smell
image-preloader
fastcgi-mono-server
node-mongodb-native
scidb
cab
caroufredsel
kohana-orm
extensibility
qmainwindow
ncml
feincms
gssapi
inflate
mail-gem
mysql-connector
scsf
cherokee
gmagick
xhtmlrenderer
hs-err
arraycollection
text-manipulation
chromebug
linq-to-mysql
konsole
web-widget
file-icons
bootstrap-protocol
urlscan

Resources

Encrypt Message