C
C#7d ago
noname7777

Array as Public Property; Need to Set Value at Index

I come from C/Lua/VBA background, very new to C# or anything like it. Using .NET 9 and Onvif.core with VScodium/Omnisharp on Windows. Onvif.core has this class:
C#
public class GetCapabilitiesRequest
{
[MessageBodyMember(Namespace = "http://www.onvif.org/ver10/device/wsdl", Order = 0)]
[XmlElement("Category")]
public CapabilityCategory[] Category;

public GetCapabilitiesRequest();
public GetCapabilitiesRequest(CapabilityCategory[] Category);
}
C#
public class GetCapabilitiesRequest
{
[MessageBodyMember(Namespace = "http://www.onvif.org/ver10/device/wsdl", Order = 0)]
[XmlElement("Category")]
public CapabilityCategory[] Category;

public GetCapabilitiesRequest();
public GetCapabilitiesRequest(CapabilityCategory[] Category);
}
CapabilityCategory is an enum:
C#
public enum CapabilityCategory
{
All = 0,
Analytics = 1,
Device = 2,
Events = 3,
Imaging = 4,
Media = 5,
PTZ = 6
}
C#
public enum CapabilityCategory
{
All = 0,
Analytics = 1,
Device = 2,
Events = 3,
Imaging = 4,
Media = 5,
PTZ = 6
}
I am trying to set the value of Category[0] to CapabilityCategory.ALL This:
C#
GetCapabilitiesRequest caps = new() {
Category[0] = CapabilityCategory.All
}
C#
GetCapabilitiesRequest caps = new() {
Category[0] = CapabilityCategory.All
}
Gives me some errors:
Cannot initialize type 'GetCapabilitiesRequest' with a collection initializer because it does not implement 'System.Collections.IEnumerable' [proj_name]
The name 'Category' does not exist in the current context [proj_name]
Invalid initializer member declarator [proj_name]
Cannot initialize type 'GetCapabilitiesRequest' with a collection initializer because it does not implement 'System.Collections.IEnumerable' [proj_name]
The name 'Category' does not exist in the current context [proj_name]
Invalid initializer member declarator [proj_name]
My normal approach of shotgunning different syntax has yielded nothing useful. Tutorials point/W3 Schools and Microsoft documentation have also proven unhelpful. The library documentation is non-existent, and the ONVIF Application programmer's guide is far too general in this case. If more information is required I will be happy to provide it. Thanks,
18 Replies
Thinker
Thinker7d ago
You can't initialize elements in an array field like that, you'll have to assign it after creation.
GetCapabilitiesRequest caps = new();
caps.Category[0] = CapabilityCategory.All;
GetCapabilitiesRequest caps = new();
caps.Category[0] = CapabilityCategory.All;
noname7777
noname7777OP7d ago
I knew it would be something super simple lmao. thanks!
Thinker
Thinker7d ago
Actually you could also do this
GetCapabilitiesRequest caps = new()
{
Category = [CapabilityCategory.All]
};
GetCapabilitiesRequest caps = new()
{
Category = [CapabilityCategory.All]
};
(I'm dumb and this wouldn't work because you'd first have to initialize the array)
noname7777
noname7777OP7d ago
Oh I had assumed they were dynamic. Noted.
Angius
Angius7d ago
Lists are, arrays aren't
Thinker
Thinker7d ago
And in any case, accessing index 0 on an array or list without any elements will throw an exception.
MODiX
MODiX7d ago
Thinker
REPL Result: Failure
int[] xs = [];
xs[0] = 1;
int[] xs = [];
xs[0] = 1;
Exception: IndexOutOfRangeException
- Index was outside the bounds of the array.
- Index was outside the bounds of the array.
Compile: 210.594ms | Execution: 26.317ms | React with ❌ to remove this embed.
SleepWellPupper
Even more so since there is no initialization of the array field. So
GetCapabilitiesRequest caps = new();
caps.Category[0] = CapabilityCategory.All;
GetCapabilitiesRequest caps = new();
caps.Category[0] = CapabilityCategory.All;
would throw a NullReferenceException.
Unknown User
Unknown User6d ago
Message Not Public
Sign In & Join Server To View
noname7777
noname7777OP6d ago
I'm already compromising by being on windows and using something that isn't Neovim, but I appreciate the thought.
Angius
Angius6d ago
De-compromise by going back to Linux or Mac, and compromise by using Rider Net zero change in compromise lol
jcotton42
jcotton426d ago
Are you saying you’re on Windows just to learn C#? Because you don’t need to be.
noname7777
noname7777OP6d ago
I'm on windows for other reasons, I would absolutely be doing this on Linux if I could be. I'm aware .NET 9 is cross platform. I miss Sway every time I need to <Alt+tab> 😭
Unknown User
Unknown User6d ago
Message Not Public
Sign In & Join Server To View
noname7777
noname7777OP6d ago
It's entirely personal preference, I was just stating the fact that your suggestion maligns with mine.
Unknown User
Unknown User6d ago
Message Not Public
Sign In & Join Server To View
noname7777
noname7777OP6d ago
My preferences have more to do with the two years of muscle memory and BASH experience and less to do with the politics of it lmao
Unknown User
Unknown User6d ago
Message Not Public
Sign In & Join Server To View

Did you find this page helpful?