I'm working with a new Windows Phone 8 App, and trying to list the contacts on the phone.
In debug mode I can see the member Id (Contact.Id) with small blue icon next to the member (not an extension) but I can't access this member in programming mode, and can't view it when typing A = Contact.Id !, and can't find any document about it event on Microsoft site I can't find the member: microsoft.phone.userdata.contact.id
what is the type of this member?
I just realized that
contact.GetHashCode()
returns the exact same number as the Id property seen in the variable window drilldown.
Not sure how reliable it is, and how reliable it will be in the future.
Id is a private/protected/internal property of the Contact class. That means that in terms of the Silverlight runtime it's a non-accessible member. And that means you won't be able to get that value at runtime. Contact.Id is out of your reach.
Silverlight respects access levels and will only let you access members in the access level for the calling code. For example, all class can invoke all public members (properties, methods, events, fields, etc) of all other classes. As another example, only classes in the same assembly can invoke internal members of classes in the same assembly. If any class outside that assembly attempt to access internal members they'll get a MemberAccessException. And as a final example if a class declares private members (e.g. private field) then only that class can access that private members. If another class attempts to access private values inside a class it'll receive a MemberAccessException.
The above is true for both runtime (Reflection) invoked members and for compile-time (hardcoded) invoked members.
Related
I have a winform program and a Dll that stores nested classes. One of these is a Permissions class, that basically acts as a permissions director for what can be used by whom in either the winform or the Dll.
The winform needs an instance of the Permission class, so it can direct flow of code. The Dll also needs this too.
The Dll structure simplified looks like this
Master Class
Permission Class (creates the permissions)
Worker Class (needs to know permissions)
Other Class (needs to know permissions)
End Master class
MY idea was that I would create an instance of Permission class in winform and then ‘duplicate’ it in the Dll. This way I would not have to pass the reference of the Permission class instance from the winform back to the DLL, worker class (and others). And they would be in synch if winform instance were to update.
I thought I would do this by creating a Public Shared new Permission class instance in the Master Class, but that does not work, the instance is created but does not update. Im obviously wrong here.
Is there an acceptable way of creating something like the above scenario?
EDIT --------------
Im very new to the OOP concept. The permission class uses one external variable, that is the licence key, the permission class then splits the licence into various variables, each representing a different permission for different things. So, when I create the instance of the Permission class in the Form Exe like:
Perms as new Masterclass.Permission(LicenceKey)
it creates the one instance I want to use, in the Exe, but this instance is not available in the DLL.
Based on Olivier’s suggestion, how do I structure the code to have one instance of Perms being shared in the Exe and other classes within the Masterclass? I would do it in spaghetti code as a global variable in the project and its done, but now I have OOP and DLL’s and I can’t get my (old) head around it.
Assuming that you want to have one single Permission object throughout the whole lifetime of the application shared by all assemblies, create singleton objects to share data. You can do so by declaring a Public Shared ReadOnly field (or property) and making the constructor Private, so nobody else can create another instance. This ensures that only one single object exists.
Public Class Master
Public Class Permission
Public Shared ReadOnly Instance As Permission = New Permission()
Private Sub New()
End Sub
End Class
End Class
Then you can access the permissions with
Dim p = Master.Permission.Instance
or
Master.Permission.Instance.SomeMember
Note that the nested class Permission is nested statically as declaration. The outer class acts as namespace or scope. You do not need to create an instance of the surrounding class to access the inner class. Instances of nested classes are not nested in instances of the surrounding class. I.e., instances of Master and instances of Permission exist as separate, non-nested objects.
On the other hand, if you want one Permission object (an object being an instance of a class) per Master object, you must have a Permission property in the Master class. It makes no difference whether the class declarations themselves are nested or not. Usually, you would declare them as non nested in separate vb files.
Public Class Master
Public ReadOnly Property Permissions As New Permission()
End Class
Public Class Permission
End Class
Don't duplicate any code! This code must be in one assembly only. Typically you would place it in a VB Class Library Project (this creates a DLL) and reference this project in other projects where you need the permissions.
The organisation of the assemblies is completely independent from the question of the lifetime of the objects, nesting etc.
The organisation of the assemblies only affects the availability of the declarations, not the availability of the objects and sharing of data, because all the data exists in one single memory scope at runtime.
This works only within one single Process. When you start an executable, it loads the EXE and the referenced DLL's into the same memory space. The EXE and the DLL's can therefore share objects.
However, if you start different Processes (i.e., EXE's in Windows), each of them will get its own memory space. The processes do not share their memory with other processes!
If you want to share live data between processes you must use Interprocess Communication
Seee also: Partitioning Your Code Base Through .NET Assemblies and Visual Studio Projects
I have a class called Contact and one called Account
and I have a method called public static Account GetAccount(Contact c) {...}
Where is the best place to put this method? What design patterns should I be looking at?
A) With the Contact class
B) With the Account class
C) Have the method accessible in both classes
D) Somewhere else?
There are probably many good answers to your question. I'll take a stab at an answer, but it will have my personal biases baked in it.
In OOP, you generally don't see globally accessible) functions, disconnected from, but available to all classes. (Static methods might be globally available, but they are still tied to a particular class). To follow up on dkatzel's answer, a common pattern is in OOP is instance manager. You have a class or instance that provides access to a a database, file store, REST service, or some other place where Contact or Account objects are saved for future use.
You might be using a persistence framework with your Python project. Maybe something like this: https://docs.djangoproject.com/en/dev/topics/db/managers/
Some persistence frameworks create handy methods instance methods like Contact.getAccount() -- send the getAccount message to a contact and the method return the associated Account object. ...Or developers can add these sorts of convenience methods themselves.
Another kind of convenience method can live on the static side of a class. For example, the Account class could have a static getAccountForContact() method that returns a particular account for a given Contact object. This method would access the instance manager and use the information in the contact object to look up the correct account.
Usually you would not add a static method to the Contact class called getAccountForContact(). Instead, you would create an instance method on Contact called getAccount(). This method could then call Account.getAccountForContact() and pass "self" in as the parameter. (Or talk to an instance manager directly).
My guiding principle is typically DRY - do not repeat yourself. I pick the option that eliminates the most copy-and-paste code.
If you define your method in this way, it's not really connected with either of your classes. You can as well put it in a Util class:
public class AccountUtil{
public static Account getAccount(Contact c){ ... }
// you can put other methods here, e.g.
public static Contact getContact(Account a){ ... }
}
This follows the pattern of grouping static functions in utility classes like Math in Java / C#.
If you would like to bound the function to a class in a clear way, consider designing your class like this:
public class Contact{
public Account getAccount(){ ... } // returns the Account of this Contact
// other methods
}
In OOP it is generally recommended that you avoid using global functions when possible. If you want a static function anyways, I'd put it in a separate class.
It depends on how the lookup from Contact to Account happens but I would vote for putting it in a new class that uses the Repository pattern.
Repository repo = ...
Account account = repo.getAccount(contact);
That way you can have multiple Repository implemtations that look up the info from a database, or an HTTP request or internal mapping etc. and you don't have to modify the code that uses the repositories.
My vote is for a new class, especially if the function returns an existing account object. That is, if you have a collection of instances of Contact and a collection of instances of Account and this function maps one to the other, use a new class to encapsulate this mapping.
Otherwise, it probably makes sense as a method on Contact if GetAccount returns a new account filled in from a template. This would hold if GetAccount is something like a factory method for the Account class, or if the Account class is just a record type (instances of which have lifetimes which are bound to instances of Contact).
The only way I see this making sense as part of Account is if it makes sense as a constructor.
This question has been asked many times but I can't find a solution.
I have a WCF service with a function that takes in a customer object. This customer object is in a separate project that both the client and server code reference. When I add the service reference to the client project, I choose the option to reuse data types. However, when I try to call the function on the client side and pass the customer object in, I get this error:
Error 39 Value of type 'Real.Namespace.Customer' cannot be converted to 'Service.Namespace.Customer'.
The "Real.Namespace" is the class it should be using. "Service.Namespace" is the auto generated class created by the service reference. I know this is supposed to work, so there must be some reason why it is unable to find the real class and reuse it.
I've tried this with very simple objects and it still won't work. Any ideas on why the auto generated code can't find the real class and use it?
Edit: I've tried using a very simple object just to see if I could get it to work. So right now I'm just using a test class that looks like this:
Namespace DTO
<DataContract>
Public Class Test
<DataMember>
Public Property Name As String
End Class
End Namespace
I am having a Service class which implements an Interface. In this class I am using List which is loaded from excel.
I have to load the list only once at the beginning of program. So I have load List at Console_Load and pass the list to Service Class and get in another List. However the values becomes empty inside IService Members. Please help on this
If your instance mode is not singleton you'll probably need to store the list in a static member during the startup of your service
I have a server side service called ConstructionManager, one of its operation is GetAll() which return a list of constructions. Construction is a data contract, and there are several types which inherit from Construction (Buildings, Apartments etc..)
When I send the list of apartments all is good, all properties is on their place, but when i receive that list at client side, and see what it is in the received object at run time, in Non Public Members i saw all the properties that are specific to type that inherits from Construction, like Rooms, Floor, but in Result View it shows all properties have the value "0", and not the value with which they were sent.
On data contract Construction, at the top of class, are KnownType attributes to inherited classes.
It maybe helpful to know, I use Web Service Software Factory.
Sorry for my bad English.
If you see that properties are populated in an object in your service just before it is sent over the wire to the client (i.e. just before serialization), and then see that the received object is missing the values in those properties just after it is received by the client, it means that they were lost in the serialization process.
There are 2 things you need to remember about serialization:
You need to make sure the classes you send over the wire are marked with the [DataContract] attribute, and that all properties within that are marked with the [DataMember] attribute. If a property is not a .NET type, then the class that defines (and the properties within it) it also needs to be marked up with these attributes.
Class Inheritance is lost in serialization. If you create an object of type "Building", and your WCF service method returns a type of "Construction", then the message sent to the client might not serialize correctly (eg. could it only be serializing the properties defined in the base type "Construction"?). I suggest you test this out by making your service return the inherited type rather than the base type, and see if it fixes the problem.
I think that the 2nd point is the most likely cause of your issues. If you provide your code I can help you in more detail