Error 1 Use Of Possibly Unassigned Field
Contents |
resources Windows Server 2012 resources Programs MSDN subscriptions Overview Benefits Administrators Students Microsoft Imagine Microsoft Student Partners ISV Startups TechRewards Events Community Magazine Forums Blogs
Error 1 Use Of Unassigned Local Variable
Channel 9 Documentation APIs and reference Dev centers Retired content Samples error 1 use of unassigned local variable c# We’re sorry. The content you requested has been removed. You’ll be auto redirected in 1 second. MSDN c# struct constructor Library MSDN Library MSDN Library MSDN Library Design Tools Development Tools and Languages Mobile and Embedded Development .NET Development Office development Online Services Open Specifications patterns & practices Servers and Enterprise Development Speech Technologies Web Development Windows Desktop App Development TOC Collapse the table of content Expand the table of content This documentation is archived and is not being maintained. This documentation is archived and is not being maintained. Compiler Error CS0170 Other Versions Visual Studio 2008 Visual Studio 2005 Visual Studio .NET 2003 Use of possibly unassigned field 'field'A field in a structure was used without first being initialized. To solve this problem, first determine which field was uninitialized and then initialize it before you try to access it. For more information about initializing structs, see Structs (C# Programming Guide) and Using Structs (C# Programming Guide).The following sample generates CS0170: Copy // CS0170.cs public struct error { public int i; } public class MyClass { public static void Main() { error e; // uncomment the next line to resolve this error // e.i = 0; System.Console.WriteLine( e.i ); // CS0170 because //e.i was never assigned } } Show: Inherited Protected Print Export (0) Print Export (0) Share IN THIS ARTICLE Is this page helpful? Yes No Additional feedback? 1500 characters remaining Submit Skip this Thank you! We appreciate your feedback. Dev centers Windows Office Visual Studio Microsoft Azure More... Learning resources Microsoft Virtual Academy Channel 9 MSDN Magazine Community Forums Blogs Codeplex Support Self support Programs BizSpark
SQL Server 2014 Express resources Windows Server 2012 resources Programs MSDN subscriptions Overview Benefits Administrators Students Microsoft Imagine Microsoft Student Partners ISV Startups TechRewards Events Community Magazine Forums Blogs Channel 9 Documentation APIs and reference Dev centers Retired content Samples We’re sorry. The content you requested has been removed. You’ll be auto redirected in 1 second. Ask a question Quick access Forums home Browse forums users FAQ Search related threads Remove From My Forums Answered by: Unusual compiler error 'Use of possibly unassigned field' Archived Forums V > Visual C# Language Question 0 Sign in to vote I am getting an unusual compiler of 'Use of possibly unassigned field' when using https://msdn.microsoft.com/en-us/library/829s64e2.aspx foreach in VS2003 .Net for to cycle through an array of structs. The code would look something like:// In another assemblystruct myStruct{decimal x;}someObject = new someObjectType(); // someObjectType is in another assemblymyStruct[] marray = someObject.GetArrayOfmyStructs(); // This is in another assemblyforeach (myStruct st in marray){if (st.x == 0) // error here{// do something}}}Since I am accessing st.x from a variable in a foreach statement, why would I ever be getting https://social.msdn.microsoft.com/Forums/en-US/a41c6fdc-21de-4c33-be75-4f7b233070aa/unusual-compiler-error-use-of-possibly-unassigned-field?forum=csharplanguage this error ? Wednesday, July 12, 2006 2:19 PM Answers 0 Sign in to vote Well, according to these folks, it's a bug in the runtime, for which you need a service patch: http://forums.aspdotnetstorefront.com/showthread.php?t=272 Wednesday, July 12, 2006 8:55 PM All replies 0 Sign in to vote Because the complier has no way of knowing it GetArrayOfmyStructs() initialized the struct in the array it returns. (that is, st will have a value assigned, but st.x may not) Wednesday, July 12, 2006 2:56 PM 0 Sign in to vote Clearly if the compiler has no way of knowing, it should not be giving me a compiler error which stops the compile from being done and keeps my program from being built. What I am supposed to do to tell the compiler that my structure's fields have all been initialized and there is no reason to stop the compile ? Wednesday, July 12, 2006 7:38 PM 0 Sign in to vote Well, according to these folks, it's a bug in the runtime, for which you need a service patch: http://forums.aspdotnetstorefront.com/showthread.php?t=272 Wednesday, July 12, 2006 8:55 PM Microsoft is conducting an online survey to understand your opinion of the Msdn Web site. If you choose to participate, the
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 http://stackoverflow.com/questions/8225819/odd-use-of-possibly-unassigned-field-type-cs0170 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 minute: Sign up Odd “Use of error 1 possibly unassigned field 'Type'” - CS0170 up vote 2 down vote favorite I might be missing something, but I'm 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); error 1 use if (element != null) { ... } } } public struct ElementId { public string Id; public MDAPI_ElementType Type; } With the following extension methods: public static bool IsEmpty(this 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