1

I'm trying to add a short value to my indices list, but I'm getting an error that the method has invalid arguments.

    int verticesStart = vertices.Count();
    short vertStart = (short)verticesStart;

Since the indices list is a "short", I cast the variable verticesStart so it is usable.

    indices.Add(vertStart + 0);

This line is where I get the error. Am I not allowed to do any kind of math function in an Add method?

Christian Frantz
  • 253
  • 2
  • 11

4 Answers4

3

Arithmetic in C# is defined for 32-bit and up integer types, not for short. When you do vertStart + 0, the result is an int, that you have to cast back to a short:

indices.Add((short)(vertStart + 0));

(I'm assuming you're not actually adding 0, but some other value. Because adding 0, of course, doesn't change the value.)

Tim S.
  • 55,448
  • 7
  • 96
  • 122
2

Since the indices list is a "short"

Therefore, you can only add shorts to indices.

vertStart + 0

The rules of the language specify that short + int has type int. Since vertStart is of type short and 0 is an int literal, vertStart + 0 has type int. Therefore,

indices.Add(vertStart + 0);

is attempting to add an int to a list that you said holds shorts. This is not possible.

Am I not allowed to do any kind of math function in an Add method?

That is not the point. Any expressions are evaluated for their values before they are passed along to the Add method. In your case, you have an expression that evaluates to type int. But if indicies is really a List<short> as you say, then you can't add ints to it. You will have to have a narrowing conversion to short.

indices.Add((short)(vertStart + 0));
jason
  • 236,483
  • 35
  • 423
  • 525
  • This worked. I had forgotten that any plain number is an int. Thank you – Christian Frantz Jul 14 '13 at 13:10
  • This answer is incorrect, in that it implies that if you had two `short`s and added them together, the answer would be a `short`. In fact, it's an `int`. Also, `0` is a literal, which normally (e.g. `var x = 0;`) resolves to `int`, but it is more flexible; e.g. `short s = 1;` is valid, though `int i = 1; short s = i;` is not. Curiouser, `const int i = 1; short s = i;` is valid (since the compiler is able to understand the second part as `short s = 1;`). – Tim S. Jul 15 '13 at 16:35
  • 1
    @Tim S.: It implies no such thing. And while what you say about `short`s is true, they are not relevant to this question. – jason Jul 15 '13 at 16:45
  • 1
    Well, I guess you're right, it technically does not. However, I interpreted that from it, and others might do the same. I'm afraid my downvote is already locked in, though, so I can't undo that (unless you edit it). – Tim S. Jul 15 '13 at 16:48
  • @Tim S.: I've edited. Regarding your curiosity, the C# language specification permits implicit narrowing conversions from constant expressions of type `int` that are representable in the narrower type. There was a similar [question](http://stackoverflow.com/q/17110918/45914) about Java recently, but the [answer](http://stackoverflow.com/questions/17110918/primitive-cast-and-assignments-in-java/17111090#17111090) is basically the same for C#. – jason Jul 15 '13 at 17:59
  • 1
    @Tim S.: The relevant section of the C# specification is §7.19 which says: "*An implicit constant expression conversion (§6.1.9) permits a constant expression of type `int` to be converted to `sbyte`, `byte`, `short`, `ushort`, `uint`, or `ulong`, provided the value of the constant expression is within the range of the destination type*." – jason Jul 15 '13 at 17:59
0

Arithmetic is always done on at least int. So when you do that addition (why add 0, if I may ask?) you're going to get an int back. You'd have to cast back to short afterwards again.

Joey
  • 344,408
  • 85
  • 689
  • 683
  • Eh crap. The list indices is used to store information into my index buffer, but I can only store a 16 bit value in the buffer so using an int isn't an option – Christian Frantz Jul 14 '13 at 13:08
-2

Using this:

using System;
using System.Collections.Generic;

public class Test
{
    public static void Main()
    {
        List<short> list = new List<short>();
        list.Add(1+1);
    }
}

In C#, the 1 will be int so you need to make sure you add variables of short

Jon
  • 38,814
  • 81
  • 233
  • 382