-2

I have some code that uses a class that was converted from Java with IKVM for use in .NET. So my code sample uses VAR when instantiating the class, such as:

var Something = new OriginallyFromJavaClass("c:\ReadInThisFile.txt");

Then methods of this class do something with that text file like:

var analyze = OriginallyFromJavaClass.method1(whatever);

Notice that these other uses of the class do NOT reference "Something" but just call the java class name directly without the "new" word.

The problem is that reading the text file once takes like 30 seconds so I want to do it just once in my WinForms application and then call the other methods within a button click. But if I move the above line of code up to my class level so I only have to open the TXT file once, the C# compiler fails because it doesn't allow VAR types at a class level. I know fundamentally and from using GetType to evaluate this variable that it is a class. So how would I restructure the above using explicit types and where I instantiate the class (thus reading my TXT file) just once?

yshavit
  • 42,327
  • 7
  • 87
  • 124
stackonfire
  • 1,525
  • 4
  • 17
  • 23
  • What type is it? That's the type it is. Use that type. The code says `new OriginallyFromJavaClass()`. That's the type it is. That's it's type. The type of an object is the class that it is. The class is the type. Declare the field as that type. What other type could it possibly be? Does it look like you're creating an integer there? **Did you *try* anything?** – 15ee8f99-57ff-4f92-890c-b56153 Oct 28 '16 at 17:30
  • `OriginallyFromJavaClass Something = new OriginallyFromJavaClass(@"c:\ReadInThisFile.txt");` – itsme86 Oct 28 '16 at 17:30
  • just find out what type the variable is and replace `var` with whatever your type is...? i.e. `var Something = ` would become `OriginallyFromJavaClass Something = ` – nhouser9 Oct 28 '16 at 17:31
  • Ed, yes, I DID try things. Thanks for just being negative. My confusion stemmed from the fact that this involves some ported Java code and the generic things I had tried weren't working. – stackonfire Oct 28 '16 at 21:16

4 Answers4

2

var is simply sugar for not having to repeat yourself like

OriginallyFromJavaClass Something = new OriginallyFromJavaClass("c:\ReadInThisFile.txt");

so you can use

A a = new A()

instead of

var a = new A()
brakeroo
  • 1,407
  • 13
  • 24
  • Except when you are using `var`, VS intellisense does not pick up the members at development time. Otherwise `var` is nice, cute and dandy. – ajeh Oct 28 '16 at 17:33
  • 4
    @ajeh, that's not true, VS intellisense behaves in exactly same way whether you use var or not – Evk Oct 28 '16 at 17:34
  • @ajeh It does for me. – itsme86 Oct 28 '16 at 17:35
  • 5
    @ajeh Point is, don't spread misinformation. I was simply letting you know that the problem is with *you or your configuration* and that your statement was inaccurate. So you can take your "good for you" nonsense somewhere else. – itsme86 Oct 28 '16 at 17:36
  • @ajeh If I remember correctly, Visual Studio introduced real time compile during coding. So Intellisense should behave identically during development / runtime. – Greg Oct 28 '16 at 17:39
  • Thank you for all your pointy notes, I did check on another machine and it worked as you described. I never suspected that it was supposed to work. As to what is wrong in the setup, I have no idea as I inherited the machine from another developer. – ajeh Oct 28 '16 at 19:20
1

Try

 OriginallyFromJavaClass Something = new OriginallyFromJavaClass("c:\ReadInThisFile.txt");
talex
  • 17,973
  • 3
  • 29
  • 66
1

Well, var just infers the type you should be fine if you just declare the actual type instead.

TypeClass something = new TypeClass("c:\ReadInThisFile.txt");
Rokuren
  • 101
  • 8
  • 1
    This is not an answer. It should be a comment. You'll be able to leave comments at 50 rep I believe. – itsme86 Oct 28 '16 at 17:31
0

Though var is simply syntax sugar, to clean a bit of code. Sometimes an error may occur, because what the CLR expects versus what is returned is inferred is actually incorrect. So sometimes being explicit will be ideal.

Otherwise it is ideal for short hand:

Dictionary<int, List<Sample>> example = new Dictionary<int, List<Sample>>();
var example = new Dictionary<int, List<Sample>>();

So as long as it correctly infers the type, it is the same as explicit declaration. Hopefully this clarifies your issue.

Greg
  • 11,302
  • 2
  • 48
  • 79