Well here is some code that will help explain the situation:
I first make a struct that actually will be used to save some server
informations:
struct Server {
public String IP;
public int CurrentBandwidthUsage;
public int MonthlyNetUsage;
public double Cost;
public Server(String ip, int CurrBandUse, int MonthNetUse,
double cost) {
this.IP = ip;
this.CurrentBandwidthUsage = CurrBandUse;
this.MonthlyNetUsage = MonthNetUse;
this.Cost = cost;
}
public object getValue(int ValueID) {
object _returnValue = new Object();
switch (ValueID) {
case 0:
_returnValue = MonthlyNetUsage;
break;
case 1:
_returnValue = CurrentBandwidthUsage;
break;
case 3:
_returnValue = Cost;
break;
}
return _returnValue;
}
}
Then I want to do some kind of sorting to an ArrayList witch contains
Server structs:
private static ArrayList partialSort(ArrayList a) {
object _min = ((Server)a[0]).getValue(0);
for (int i = 1; i < a.Count; i++) {
_min = ((Server)a[i]).getValue(0);
if (((Server)a[i]).MonthlyNetUsage <= _min) {
_min = ((Server)a[i]).getValue(0);
a.Insert(0, (Server)a[i]);
a.RemoveAt(i + 1);
}
}
return a;
}
Ofcourse the above does not work as I can't use the <= operator with
objects. The getValue method of the struct returns an object as I don't
know if the return type will be an int or a double type (in this
situation, or generally any other type of number).
I could of course create a getIntValue and a getDoubleValue methods to
solve this problem but I wouldn't like to use many if statements in the
for loop of the partialSort method, as I'll have to check every time
the method to use. I was woundering if I could use a String str witch
has the name of the field in order to do something like: _min =
((Server)a[i]).str
Any suggestions or workarounds?
Thanks in advance,
Jeoryos 4 1669
It sounds to me like you have a fundamental flaw in the logic of what
you are trying to do. Its really hard for me to imagine a situation
where you would want to get the value of a particular field based on
some int value... You might need to show the code that is creating the
server object and calling the GetValue(...) method to make any sense of
this.
Furthermore, in this situation I would say that using a class would be
better than a structure. I'd really only use a struct if I needed
strict sizes of my datastructures. If you're needing to save the
objects to a file you can just use serialization, xml or binary. Why on
earth are you needing to get a value based on an int? Why not just get
the value out of a property?
~justin
Jeoryos wrote: Well here is some code that will help explain the situation:
I first make a struct that actually will be used to save some server informations: struct Server { public String IP; public int CurrentBandwidthUsage; public int MonthlyNetUsage; public double Cost;
public Server(String ip, int CurrBandUse, int MonthNetUse, double cost) { this.IP = ip; this.CurrentBandwidthUsage = CurrBandUse; this.MonthlyNetUsage = MonthNetUse; this.Cost = cost; }
public object getValue(int ValueID) {
object _returnValue = new Object(); switch (ValueID) { case 0: _returnValue = MonthlyNetUsage; break; case 1: _returnValue = CurrentBandwidthUsage; break; case 3: _returnValue = Cost; break; } return _returnValue; }
}
Then I want to do some kind of sorting to an ArrayList witch contains Server structs:
private static ArrayList partialSort(ArrayList a) { object _min = ((Server)a[0]).getValue(0); for (int i = 1; i < a.Count; i++) { _min = ((Server)a[i]).getValue(0); if (((Server)a[i]).MonthlyNetUsage <= _min) { _min = ((Server)a[i]).getValue(0); a.Insert(0, (Server)a[i]); a.RemoveAt(i + 1); } } return a;
}
Ofcourse the above does not work as I can't use the <= operator with objects. The getValue method of the struct returns an object as I don't know if the return type will be an int or a double type (in this situation, or generally any other type of number).
I could of course create a getIntValue and a getDoubleValue methods to solve this problem but I wouldn't like to use many if statements in the for loop of the partialSort method, as I'll have to check every time the method to use. I was woundering if I could use a String str witch has the name of the field in order to do something like: _min = ((Server)a[i]).str
Any suggestions or workarounds?
Thanks in advance, Jeoryos
I do have the code that makes use of the getValue() method. See the
implementation of the partialSort witch actually get an arraylist of
server objects and makes some kind of sorting in order to have the
Servers with the min value of a property at the top of the arraylist.
I could of cource create CostBasedPartialSort and generaly a
partialSort method for every property that I would like the sort to be
based on but I'm looking for "global" sorting method (or let's say more
spicy)!
At the end I don't care about the size of the datastructures. I just
thought that there wasn't any need of using a class. Do really think
that I have to? Generaly what are the advantages - disadvantages of
the class vs struct in this case?
justncase80 wrote: It sounds to me like you have a fundamental flaw in the logic of what you are trying to do. Its really hard for me to imagine a situation where you would want to get the value of a particular field based on some int value... You might need to show the code that is creating the server object and calling the GetValue(...) method to make any sense of this.
Furthermore, in this situation I would say that using a class would be better than a structure. I'd really only use a struct if I needed strict sizes of my datastructures. If you're needing to save the objects to a file you can just use serialization, xml or binary. Why on earth are you needing to get a value based on an int? Why not just get the value out of a property?
~justin
Jeoryos wrote: Well here is some code that will help explain the situation:
I first make a struct that actually will be used to save some server informations: struct Server { public String IP; public int CurrentBandwidthUsage; public int MonthlyNetUsage; public double Cost;
public Server(String ip, int CurrBandUse, int MonthNetUse, double cost) { this.IP = ip; this.CurrentBandwidthUsage = CurrBandUse; this.MonthlyNetUsage = MonthNetUse; this.Cost = cost; }
public object getValue(int ValueID) {
object _returnValue = new Object(); switch (ValueID) { case 0: _returnValue = MonthlyNetUsage; break; case 1: _returnValue = CurrentBandwidthUsage; break; case 3: _returnValue = Cost; break; } return _returnValue; }
}
Then I want to do some kind of sorting to an ArrayList witch contains Server structs:
private static ArrayList partialSort(ArrayList a) { object _min = ((Server)a[0]).getValue(0); for (int i = 1; i < a.Count; i++) { _min = ((Server)a[i]).getValue(0); if (((Server)a[i]).MonthlyNetUsage <= _min) { _min = ((Server)a[i]).getValue(0); a.Insert(0, (Server)a[i]); a.RemoveAt(i + 1); } } return a;
}
Ofcourse the above does not work as I can't use the <= operator with objects. The getValue method of the struct returns an object as I don't know if the return type will be an int or a double type (in this situation, or generally any other type of number).
I could of course create a getIntValue and a getDoubleValue methods to solve this problem but I wouldn't like to use many if statements in the for loop of the partialSort method, as I'll have to check every time the method to use. I was woundering if I could use a String str witch has the name of the field in order to do something like: _min = ((Server)a[i]).str
Any suggestions or workarounds?
Thanks in advance, Jeoryos
Jeoryos <va*****@gmail.com> wrote: At the end I don't care about the size of the datastructures. I just thought that there wasn't any need of using a class. Do really think that I have to? Generaly what are the advantages - disadvantages of the class vs struct in this case?
Classes and structs behave very differently - classes are reference
types, and structs are value types. I haven't yet gotten around to
writing this difference up fully (it's tricky to do in an accurate but
readable way) but if you read http://www.pobox.com/~skeet/csharp/parameters.html
and http://www.pobox.com/~skeet/csharp/memory.html
that should help a bit.
Using a class should be your "default" position - it's rare to need to
define your own struct.
--
Jon Skeet - <sk***@pobox.com> http://www.pobox.com/~skeet Blog: http://www.msmvps.com/jon.skeet
If replying to the group, please do not mail me too
Thanks! I'll have a look at your links, and I'll make use of a class
instead of the struct, but my main problem here haven't been solved!
Jon wrote: Jeoryos <va*****@gmail.com> wrote: At the end I don't care about the size of the datastructures. I just thought that there wasn't any need of using a class. Do really think that I have to? Generaly what are the advantages - disadvantages of the class vs struct in this case?
Classes and structs behave very differently - classes are reference types, and structs are value types. I haven't yet gotten around to writing this difference up fully (it's tricky to do in an accurate but readable way) but if you read http://www.pobox.com/~skeet/csharp/parameters.html and http://www.pobox.com/~skeet/csharp/memory.html that should help a bit.
Using a class should be your "default" position - it's rare to need to define your own struct.
-- Jon Skeet - <sk***@pobox.com> http://www.pobox.com/~skeet Blog: http://www.msmvps.com/jon.skeet If replying to the group, please do not mail me too This thread has been closed and replies have been disabled. Please start a new discussion. Similar topics
by: John F Dutcher |
last post by:
I use code like the following to retrieve fields from a form:
recd =
recd.append(string.ljust(form.getfirst("lname",' '),15))
recd.append(string.ljust(form.getfirst("fname",' '),15))
etc.,...
|
by: Grasshopper |
last post by:
Hi,
I am automating Access reports to PDF using PDF Writer 6.0. I've
created a DTS package to run the reports and schedule a job to run this
DTS package. If I PC Anywhere into the server on...
|
by: Kamilche |
last post by:
I have looked at many object-oriented programming frameworks out there
for C. Though the ideas presented are intriguing, and I've used some
of them in my own work, they all suffered some drawback...
|
by: becte |
last post by:
I need to use three bytes to store four 6-bit integers (4 * 6 = 3 * 8)
like this
11111122|22223333|33444444
Suppose the input is, int c1, c2, c3, c4, range 0 .. 2^6 -1
and the output is int...
|
by: Charles Denny |
last post by:
I'm trying to call CertFindCertificateInStore to find all certificates in the
store that have the Code Signing enhanced key usage. I'm running into
problems marshalling the array of OIDs in...
|
by: Fraser Dickson |
last post by:
I am building a web based system using ASP.NET and VB.NET
which has to interact with a web service which uses XML
WDDX packets.
I have been given the XML Packet Specification by the Web
Service...
|
by: zoltan |
last post by:
Hi,
The scenario is like this :
struct ns_rr {
const u_char* rdata;
};
The rdata field contains some fields such as :
|
by: Kantha |
last post by:
Hi all,
I have declared an Union as follows
typedef union
{
struct interrupt_bits
{
unsigned char c_int_hs_fs_status : 1,
c_setup_intflag : 1,
|
by: Bill Grigg |
last post by:
All,
Can anyone supply an example or reference to an example of using
reflection to determine the data types and array lengths contained in a
nested stucture in C#? Actually, it is a structure...
|
by: DolphinDB |
last post by:
Tired of spending countless mintues downsampling your data? Look no further!
In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
|
by: ryjfgjl |
last post by:
ExcelToDatabase: batch import excel into database automatically...
|
by: isladogs |
last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM).
In this month's session, we are pleased to welcome back...
|
by: isladogs |
last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM).
In this month's session, we are pleased to welcome back...
|
by: Vimpel783 |
last post by:
Hello!
Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
|
by: jfyes |
last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
|
by: ArrayDB |
last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
|
by: CloudSolutions |
last post by:
Introduction:
For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
|
by: Shællîpôpï 09 |
last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
| |