Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

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
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

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 /
avatar image
0
Question by Xzapo · May 19, 2015 at 08:50 PM · c#classxml

C# and XML Question

So I am trying to just simply grab some data from an XML file and what I've been finding just isn't making much sense to me, and also it's telling me "var" does not exist in current context. This is the current issue not allowing me to see if what I have even works! But I'll post my code so far below, but please correct me in I'm wrong on this, but i can just upload my xml file as an assets correct?

If someone could just elaborate on what the code does I have so far and explain to me if I'm even on the right track or not that would be great and save me some time. Also the var issue is really holding me up.

C#

     [XmlRoot("MyKeno")];
     public class SetUp 
     {
         [XmlArray("Numbers"),XmlArrayItem("Number")]
         public Keno[] KenoNums;
 
         var numStart = SetUp.Load(Path.Combine(Application.dataPath, "num.xml"));
     }


XML

 <?xml version=”1.0" encoding=”utf-8" ?>
 
 <KenoNumbers>
     <numbers num="1" />
     <numbers num="4" />
     <numbers num="32" />
     <numbers num="54" />
     <numbers num="77" />
 <KenoNumbers/>


Comment
Add comment · Show 1
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image hu90 · May 19, 2015 at 10:02 PM 1
Share

var numStart = SetUp.Load(Path.Combine(Application.dataPath, "num.xml"));

This line should be in a method. You can't put that directly in the class declaration.

EDIT: As pointed out by @Baste, that's not correct: your initialization is valid, the problem is that the type must be explicit.

1 Reply

· Add your reply
  • Sort: 
avatar image
0

Answer by Baste · May 19, 2015 at 11:58 PM

You can put method calls in the declaration of fields as long as those method calls can be executed before the object exists - which means that you're limited to static calls. So that's not quite correct, @hu90.

That being said, you can't use type inference (ie. var) in that context - every field has to have an explicit type. This won't compile:

 public class Foo {
     var myInt = 1;
 }

So the answer is simple - give the numStart the proper type - which would be the return type of the Load call.

Comment
Add comment · Show 1 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image hu90 · May 20, 2015 at 11:15 PM 0
Share

Thanks, my bad. The initialization contains no member access/call. Updated my comment.

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

21 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

C# Utility Class 1 Answer

Multiple Cars not working 1 Answer

Distribute terrain in zones 3 Answers

How to read data from a constantly updating xml server? 1 Answer

Why is Start() from my base class ignored in my sub-class? 4 Answers


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges