Error 2 Use Of Possibly Unassigned Field
help? Post your question and get tips & solutions from a community of 418,506 IT Pros & Developers. It's quick & easy. "Use of possibly unassigned field" compile error P: n/a Chris LaJoie Our company has been developing a program in C# for some time now, and we haven't had any problems with it, but just last night something cropped up that has me, and everyone else, stumped. I have a struct that contains several different types of data. This struct is used throuout the program. Now, when I compile, I get 6 errors, all of them "Use of possibly unassigned field 'awayTime'" or "Use of possibly unassigned field 'intlTime'". It is correct, it is possible that these fields aren't assigned, but I don't care. It's inside of a struct, and everything inside of a struct gets assigned its default value anyway. This is difficult to explain, but if anyone else has had this problem and fixed it, i'd love to know how. Thanks. Chris LaJoie Nov 15 '05 #1 Post Reply Share this Question 10 Replies P: n/a Jon Skeet Chris LaJoie
here for a quick overview of the site Help Center Detailed answers to any questions you might have Meta Discuss the workings and policies of this site About Us Learn more about Stack Overflow the company Business Learn more about hiring developers or posting ads with us Stack Overflow Questions Jobs Documentation Tags Users Badges Ask Question x Dismiss Join the Stack Overflow Community Stack Overflow is a community of 4.7 million programmers, just like you, helping each other. Join them; it only takes a https://bytes.com/topic/c-sharp/answers/227853-use-possibly-unassigned-field-compile-error minute: Sign up Struct implicit vs empty constructor up vote 1 down vote favorite Considering the following struct : struct S { public string s; } What is the difference between 1 : S instance = new S(); instance.s = "foo"; and 2 : S instance; instance.s = "foo"; Both versions compile and run fine. I am just curious to know what http://stackoverflow.com/questions/9394174/struct-implicit-vs-empty-constructor happens behind the scenes. Edit : I guess in case 2 S is unassigned until we put a value on its s field; As this doesn't work : S instance; if (inst.s == null) inst.s = "foo"; //Compiler drops : Use of possibly unassigned field 's' while this does : S instance; inst.s = "foo"; if (inst.s == null) inst.s = "bar"; //Compiler drops : Use of possibly unassigned field 's' and this also works : S inst = new S(); if (inst.s == null) inst.s = "foo"; I welcome any deeper explanations about this behavior Update I found those 2 posts, completing Marc's answer : why are mutable structs evil when to use struct in c# c# share|improve this question edited Feb 22 '12 at 15:20 asked Feb 22 '12 at 11:48 Mehdi LAMRANI 5,06285084 updated my answer to reflect your edit –Marc Gravell♦ Feb 22 '12 at 11:57 add a comment| 3 Answers 3 active oldest votes up vote 5 down vote accepted What is the difference between S instance = new S(); instance.s = "foo"; and S instanc
here for a quick overview of the site Help Center Detailed answers to any questions you might have Meta Discuss the workings and policies of this site About Us Learn more about Stack Overflow the company http://stackoverflow.com/questions/8225819/odd-use-of-possibly-unassigned-field-type-cs0170 Business Learn more about hiring developers or posting ads with us Stack Overflow Questions Jobs Documentation Tags Users Badges Ask Question x Dismiss Join the Stack Overflow Community Stack Overflow is a community of 4.7 million programmers, http://answers.unity3d.com/questions/395361/need-help-converting-js-to-c-11.html just like you, helping each other. Join them; it only takes a minute: Sign up Odd “Use of possibly unassigned field 'Type'” - CS0170 up vote 2 down vote favorite I might be missing something, but I'm error 2 getting what I think seems an odd error, that none of the other developers are getting with the same code... public void SomeMethod(... symbolInfo) { ElementId elementId = symbolInfo.GetElementIds().Head(true); if (elementId.HasValue()) { // error here "Use of possibly unassigned field 'Type'" object element = repository.FindElement(elementId.Type, elementId.Id); if (element != null) { ... } } } public struct ElementId { public string Id; public MDAPI_ElementType Type; } With the following extension methods: public static bool IsEmpty(this error 2 use ElementId id) { return id.Type == ElementType.ElementUnknown || string.IsNullOrEmpty(id.Id); } public static bool HasValue(this ElementId id) { return !id.IsEmpty(); } Can anyone tell my why this won't build? c# c#-4.0 share|improve this question edited Nov 22 '11 at 22:46 Graham Clark 10.3k73269 asked Nov 22 '11 at 11:10 Ian 20k1453137 How does symbolInfo.GetElementIds().Head(true) create the ElementId? –ChrisF♦ Nov 22 '11 at 11:14 @ChrisF: symbolInfo is a COM object and GetElementIds() is an instance member. I'm not 100% sure on what it does internally. Head attempts to retrieve the first item from an IEnumerable
Answers Feedback Issue Tracker Blog Evangelists User Groups Navigation Home Unity Industries Showcase Learn Community Forums Answers Feedback Issue Tracker Blog Evangelists User Groups Get Unity Asset Store Unity account You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account Language Chinese Spanish Japanese Korean Portuguese Ask a question Spaces Default Help Room META Moderators Topics Questions Users Badges Home / -1 Question by NavinAE · Feb 07, 2013 at 05:23 PM · Need help converting Js to C# Hi I need help converting Js to C# . I have tried but I failed. Js : //This script controls how the soldier will move. @script RequireComponent(CharacterController); var forwardSpeedMultiplier : float = 3.0; var strafeSpeedMultiplier : float = 2.0; var turnSpeedMultiplier : float = 6.0; var gravity : float = 9.8; //var soldierLocation : String = "smoothWorldPosition/soldierSkeleton"; var soldier : Transform; var turnSpeed : float = 0.0; var forwardSpeed : float = 0.0; //Speed the character will moved at. var strafeSpeed : float = 0.0; var isGrounded : boolean; private var stopAfterLanding : float = 0.0; //How much time in seconds will the character stop after landing. private var fallSpeed : float = 0.0; private var lastGroundedTime : float; //Last time since soldier was touching the ground. private var lastLandingTime : float; //Last time since the soldier landed after a fall. private var targetForwardSpeed : float; private var targetStrafeSpeed : float; private var crouchControllerScript : crouchController; private var isFalling : boolean; private var healthScript : health; private var recoilAmount : float; private var recoilAmountTarget : float; function Start(){ crouchControllerScript = GetComponent("crouchController"); healthScript = GetComponent("health"); isFalling = false; //soldier = transform.Find(soldierLocation); } function Update () { //Stick to platform