|
brigates wrote: can I access day.mon.w_hr = 5; How do I accomplish this.
No, you can't access it that way.
The w_hr property can only be accessed using event.w_hr as it is a static member.
brigates wrote: I am new to C#
Then you should not use structs, as they are trickier to implement correctly. Use classes.
Despite everything, the person most likely to be fooling you next is yourself.
|
|
|
|
|
I have a struct that holds a value up to UInt64 . Depending on the value it could potentially be held in another Type .
I've got a function that whos return value is Type , which is the minimum required to hold the value.
From this returned Type , how do I create an object of that Type?
Dave
|
|
|
|
|
Have a look in the System.Reflection namespace for Activator (I think)
|
|
|
|
|
System.Activator Hmm... a little work to do to figure this one out!
Thanks Colin
Dave
|
|
|
|
|
The answer would be reflection. Whenever that's the answer, you are either doing something pretty complicated, or something wrong.
What is it that you are trying to accomplish, really? Usually there is an object oriented way to solve it.
Despite everything, the person most likely to be fooling you next is yourself.
|
|
|
|
|
A member variable in my struct is a UInt64. (I'm only using unsigned types here!)
1. Assume that the member variable's value is 8 : The smallest Type that could have that value is a Byte so my method returns Type Byte.
From this Type returned by the struct I want to create a variable of Type Byte.
2. Assume that the member variable's value is 256 : The smallest Type that could have that value is a UInt16 so my method returns Type UInt16.
From this Type returned by the struct I want to create a variable of Type UInt16.
3. UInt32...
4. UInt64...
Dave
|
|
|
|
|
Yes, I got it so far, but what is the point? What do you plan to do with the data?
If you create data of different types, the data has to be boxed in an object. To then use the data, you have to unbox it to the actual type.
Despite everything, the person most likely to be fooling you next is yourself.
|
|
|
|
|
Depending on the type, the data needs to be handled differently.
I suppose I could box the variable to object inside the method and make it return Object then something like
if (returnedObject is Byte)
{
Byte x = 0;
}
else if(returnedObject is UInt16)
{
UInt16 x = 0;
} but I was ideally wanting to return a Type as the value itself is not needed so there seems no point in returning it - only the type. Also, this way I have to stay within each if block or x is out of scope.
Dave
|
|
|
|
|
This works and will suffice if it can't be done by returning Type
object x = new object();
if (returnedObject is Byte)
{
x = (Byte)returnedObject;
}
else if(returnedObject is UInt16)
{
x = (UInt16)returnedObject;
}
Dave
|
|
|
|
|
DaveyM69 wrote: This works and will suffice if it can't be done by returning Type
object x = new object();
if (returnedObject is Byte)
{
x = (Byte)returnedObject;
}
else if(returnedObject is UInt16)
{
x = (UInt16)returnedObject;
}
That code only unboxes the value and boxes it again. You can just copy the reference and get the same result:
object x = returnedObject
Despite everything, the person most likely to be fooling you next is yourself.
|
|
|
|
|
Got it!
var x = Activator.CreateInstance(returnedType); It was simple after all
Dave
modified on Wednesday, July 16, 2008 4:21 AM
|
|
|
|
|
That's misuse of the var keyword. It should look like this:
object x = Activator.CreateInstance(returnedType);
It's still pretty useless. You can't really do anything with the data without checking the type and unboxing it. What is it that you are trying to accomplish?
[edit]
Actually, it's totally useless. You have created an instance of a value type without a value, and as it's boxed, you can't set the value of the instance.
Despite everything, the person most likely to be fooling you next is yourself.
modified on Wednesday, July 16, 2008 8:52 AM
|
|
|
|
|
Yeah - I figured that trying to apply it! Returning a new initialized instance of the correct type instead of just the type and checking using is on return is working, I've just had to restructure the code a little. Just seemed a little messy though but problem solved. Thanks for your help.
Dave
|
|
|
|
|
Hi All,
I'm looking to create a set of stats from a DataTable and I'm currently having a bit of trouble working out the best way to do it. There are a few columns in the table that I'm interested in with an unknown number of possible values in each of these columns. I have defined a Statistic type like:
public class Statistic
{
public string StatNameTag { get; set; }
public string Description { get; set; }
public int Value { get; set; }
public double Percentage { get; set; }
public Statistic()
{
}
}
Which are in turn held in:
public static ObservableCollection<statistic> StatsCollection = new ObservableCollection<statistic>();</statistic></statistic>
Currently I'm thinking that a foreach loop over the table is the way forward. Each time a new value in a given table column is found I'll create a new Statistic object to hold the information in and give it a StatTagName that includes the value in the name which will provide a way to find out if its a new value or not. If its not a new value increment the value on the current Statistic object already in the collection.
Am I going in the right direction with this, or would it make more sense to use the .Compute() method or something else altogether? Or should I skip this altogether and execute a series of ExecuteScalar() commands directly on the database after performing a distinct select on each column i'm interested in?
I'm struggling with the best way to go with this. Everything else seems a bit expensive compared to a foreach loop over an already loaded DataTable object.
Any input would be great. TIA,
Jammer
Going where everyone here has gone before!
My Blog
|
|
|
|
|
Hmmm - rather than using a DataTable, why not take a look at using Linq to do this? It would look something like:
var p1 = from p in dataContext
group p by p.Name
into MyGroup
select new { MyGroup.Description, MyGroup.Group.Sum(x=>x.Percentage), MyGroup.Group.Sum(y=>y.Value), MyGroup.Description }; I'm typing this blind, so it might not be 100% accurate, but you should get the idea.
|
|
|
|
|
That does look interesting indeed!! I'll have a play around with this method. Thanks Pete.
Jammer
Going where everyone here has gone before!
My Blog
|
|
|
|
|
I've gotten so far with this:
public void UpdateStats()
{
var p1 = from p in context.LocalSamples
group p by p.FileBitRate into MyGroup
select new {description = MyGroup.Key, percent = MyGroup.Group.Sum(x=>x.Percentage), value = MyGroup.Group.Sum(y=>y.Value) };
}
However, MyGroup doesn't appear to have a .Group method, only a .GroupBy<> but I've seen stacks of examples online using .Group.Sum ... so I'm a bit confused by that. I was also seeing errors saying:
"Invalid anonymous type member declarator"
Adding in description =, percent = solved that one, but I'm still unsure why I'm seeing a problem with the .Group.Sum ...
Tia,
Jammer
Going where everyone here has gone before!
My Blog
|
|
|
|
|
Sorry mate. Change this to:
var p1 = from p in context.LocalSamples group p by p.FileBitRate into MyGroup select new {description = MyGroup.Key, percent = MyGroup.Sum(x=>x.Percentage), value = MyGroup.Sum(y=>y.Value) };
|
|
|
|
|
Ahhh, thanks fella ... will have a look at this later tonight.
Jammer
Going where everyone here has gone before!
My Blog
|
|
|
|
|
Hi Again Pete,
On these two elements of the linq code:
percent = MyGroup.Sum(x=>x.Percentage), value = MyGroup.Sum(y=>y.Value
I get the following error:
DataLayer.LocalSample' does not contain a definition for 'Percentage'
DataLayer.LocalSample' does not contain a definition for 'Value'
Should the MyGroup (is this an anonymous type?) actually be an instance of my Statistic class? Or should I be creating the Statistic objects in a foreach loop over the p1 variable once this code has been executed?
the x. / y. is expecting a field from my LocalSample table and Percentage/Value are the things I'm trying to calculate. Bits of this are clearer to me now, but I'm still way off really understanding this.
Thanks chap.
Jammer
Going where everyone here has gone before!
My Blog
|
|
|
|
|
Hi,
Can anyone tell me the C# equivalent ofthe following java code?
address = InetAddress.getLocalHost().getAddress();
I've not done anything with networking in C# before but from an admittedly quick inspection it does appear that the java implementation makes more intuitive sense. I'd say it's more feature rich too but I haven't ben able to make complete sense yet of what C# has to offer.
Regards,
Dave
Regards,
Dave
|
|
|
|
|
DwR wrote: Can anyone tell me the C# equivalent ofthe following java code?
address = InetAddress.getLocalHost().getAddress();
IPAddress address = Dns.Resolve("localhost");
|
|
|
|
|
Hi,
Thanks for that. A couple of questions / observations:
1. The C# documentation refers to the resolve method as being obsolete.
2. The java type of "address" is byte[]. The method returns a "raw" ip address. The C# equivalents, including Dns.resolve return an IPAddress[] type which i assume isn't raw. Can C# return raw addresses? I assume the type to use is c# byte[]...
Additionally, I'm unclear as to what the java documentation means by a raw ip address. Can you clarify? I'm expecting that it means that I the IPAddress[] type is not the correct type for this operation.
Regards,
Dave
|
|
|
|
|
DwR wrote: I'm unclear as to what the java documentation means by a raw ip address
One would presume that the raw IP Address is just the bytes that make up the IP Address - as it would normally be with anything described as raw.
DwR wrote: I'm expecting that it means that I the IPAddress[] type is not the correct type for this operation
What operation?
|
|
|
|
|
Hi,
I think I've resolved this. By setting the type as IPAddress the member "GetAddressBytes" can be used to convert the address to bytes matching the type of the variable "address". Thanks for your tip, it pointed me in the right directions.
That said, I've hit another problem which seems to be a bit trickier. I need to determine the IP addresses associated with the network interfaces of the multi-homed host the code is running on. I am capturing the available interfaces as follows:
NetworkInterface[] interfaces = NetworkInterface.GetAllNetworkInterfaces();
I then want to loop through the entries and for each one load it's associated IP Addresses into an IPHostEntry instance (or similar). However, I don't see any way to determine the list of ip addresses associated with a particular network interface returned by the foreach loop as it iterates through "interfaces". How is this done?
Regards,
Dave
|
|
|
|