Browsing 7239 questions and answers with Jon Skeet

Can a C# Interface require or dictate a specific class?

Can I declare an Interface (i.e. IMySpecialControl) that requires classes implementing it to also inherit from some base class (i.e. System.Windows.Controls.UserControl)? My...
Jon Skeet
people
quotationmark

Can I declare an Interface (i.e. IMySpecialControl) that requires classes implementing it to also inherit from some base class (i.e. System.Windows.Controls.UserControl)? No, there's nothing in C# which defines that. It would... more 12/10/2013 6:00:01 PM

people

Store a string and a byte in a single int value

I'm having trouble finding a solution for this. I need a method to store a string and a byte in a single int value. A hash is not good for this since the method should allow en-...
Jon Skeet
people
quotationmark

An int has 32 bits. The byte will take 8 bits. That leaves you 24 bits to play with - enough for one and a half char values, uncompressed. Unless you really only need to store single character strings, or you have a restricted range of... more 12/10/2013 5:35:35 PM

people

How to set null to a GUID property

I have an object of type Employee which has a Guid property. I know if I want to set to null I must to define my type property as nullable Nullable<Guid> prop or Guid?...
Jon Skeet
people
quotationmark

Is there a way to set my property as null or string.empty in order to restablish the field in the database as null. No. Because it's non-nullable. If you want it to be nullable, you have to use Nullable<Guid> - if you didn't,... more 12/10/2013 5:24:41 PM

people

LINQ expression instead of nested foreach loops

I have these two clases: public class Client{ public List<Address> addressList{get;set;} } public class Address{ public string name { get; set; } } and I have...
Jon Skeet
people
quotationmark

Well I wouldn't put the Console.WriteLine in a lambda expression, but you can use SelectMany to avoid the nesting: foreach (var add in testList.SelectMany(x => x.addressList)) { Console.WriteLine(add.name); } I see little reason... more 12/10/2013 5:13:00 PM

people

Dispatcher.Invoke to update UI Control

I have a .Net 4.0 class that has a custom event handler (StatusChanged). I am trying to consume this class via a thread. I don't have the luxury of using BackgroundWorker because...
Jon Skeet
people
quotationmark

It's easiest just to use a lambda expression: private void doWork() { // Name changed to avoid it being a keyword MyClass clazz = new MyClass(); clazz.StatusChanged += (sender, args) => { string message = (string)... more 12/10/2013 4:30:35 PM

people

How can I use floor function

I can’t use floor function in my project. What is problem? int numAllSms = Math.Floor( (msg4SmsPart1.Count()) / 69) + Math.Floor((msg4SmsPart2.Count()) / 69) ; My string is...
Jon Skeet
people
quotationmark

You have at least two problems: Math.Floor will return a double or decimal; you're trying to assign it to an int variable Your divisions are being performed in integer arithmetic, which is presumably not what you were intending given... more 12/10/2013 4:11:52 PM

people

Return all matching XML nodes using LINQ

<?xml version='1.0' encoding='utf-8'?> <automationSettings> <VM name="DE-123" language="de" powerOn="true"> <vmClients> <vmClient...
Jon Skeet
people
quotationmark

I suspect you actually want: var query = xdoc.Root.Descendants("VM") .Where(vm => (string) vm.Attribute("name") == systemName) .Descendants("vmClient") .Select(vmClient => (string)... more 12/10/2013 2:55:49 PM

people

how can I return the child variable in an extended base method with Java oop

public class Buildings { public int getLevel(){ return level; } } public class MainBuilding extends Buildings{ public static final int cost = 200; public static int level =...
Jon Skeet
people
quotationmark

Either move the level variable (which should probably be an instance variable, rather than a static variable) to Buildings or make getLevel() abstract so that each subclass has to implement it separately (whether that's by returning a... more 12/10/2013 2:25:18 PM

people

Is there a quick way of zeroing a struct in C#?

This must have been answered already, but I can't find an answer: Is there a quick and provided way of zeroing a struct in C#, or do I have to provide someMagicalMethod...
Jon Skeet
people
quotationmark

Just use: myLunch = new ChocolateBar(); or myLunch = default(ChocolateBar); These are equivalent1, and will both end up assigning a new "all fields set to zero" value to myLunch. Also, ideally don't use mutable structs to start with... more 12/10/2013 1:38:35 PM

people

Null reference exception when filling an multidimensional image array

I create the Array cardImages in Class KartenClass public Image[][][] cardImages = new Image[9][][]; I wrote a method called arrbef() to fill it public void arrbef() { ...
Jon Skeet
people
quotationmark

This: public Image[][][] cardImages = new Image[9][][]; ... creates a top level array with 9 elements in. Every element value is null. You need: for (int i = 0; i < cardImages.Length; i++) { cardImages[i] = new Image[???][]; //... more 12/10/2013 1:28:35 PM

people