Reputation: 17
I am new to C#
I have a struct like
struct Package
{
Public int[] intvalues;
Public char[] charvalues;
Public string strvalue;
}
Now I have a string
string strquery;
I want to take the value of intvalues of the Package whose name is strquery.
As far as I tried, this didnt work
(Package)strquery.strvalue
Please help
Upvotes: 0
Views: 325
Reputation: 28917
It appears as though you might be trying to parse a String using your Package struct. If so then you might try something like this by converting your structure to encapsulate and manipulate a string.
struct Package {
public Package(string str) { //constructor
strvalue = str;
}
public int[] Get_intvalues() { // method version
// Convert stvalue into int array and return it.
return new[] {1,2,3}; //example
}
public char[] Get_charvalues() { // method version
return strvalue.ToCharArray();
}
public string strvalue; // string to work upon
}
This would allow you to use your Package struct with a string like so:
string str = "this is a test";
Package pkg = new Package(str);
pkg.Get_charvalues();
pkg.Get_intvalues(); //etc.
Upvotes: 0
Reputation: 1919
Well, you can't do anything like that. Because for a string isn't defined any explicit casting operator such as your struct Package.
Also, the strquery.strvalue is completely wrong, as the string doesn't have such a field.
You can try what Phaedrus said.
From what you've said until now I think you are doing something like this:
List<Package> listOfPackages =new List<Package>();
// You fill up here the list
foreach (Package pkg in listOfPackages)
{
if (pkg.strvalue == strquery)
{
Console.WriteLine("found a package with items:");
// write the pkg.intvalues
for(int i=0;i<pkg.intvalues.Length;i++)
Console.Write(pkg.intvalues[i].ToString());
Console.WriteLine(" ");
}
}
Upvotes: 2
Reputation: 7491
If I understand the question it appears you are need a dictionary of sorts where and int value would be associated with a key. If you changed your struct to look like this:
class Package // I would recommend using a class
{
Public Dictionary<string, int> intValues;
}
then you could "query" the int value that had the correct key like this...
var strQuery = "intA";
var pakage = new Package()
pakage.intValues = new Dictionary<string, int>()
{
new KeyValuePair<string, int>("intA", 0),
new KeyValuePair<string, int>("intB", 0),
new KeyValuePiar<string, int>("intC", 9),
}
var result = pakage.inValues[strQuery];
But this really is just enumerating a collection to find a value based on a key and your question could really use some clarification. Why don't you post what your trying to do without trying to suggest what code to use...
Upvotes: 0
Reputation: 8421
You cant cast a string type to a package type. To get the value of the strvalue you need to create a Package struct object first.
Package package = new Package();
...
string value = package.strvalue;
Upvotes: 0
Reputation: 16065
First, please clarify your code since it doesn't make any sense.
Second, ask yourself why are you using a struct? are you certain you need a struct? and not a class? look at other questions on StackOverflow that get into that topic.
Third
(Package)strquery.strvalue
Is not valid code. strquery is a string and cannot be casted into Package struct. These are not objects related to each other at any level. (except that they both derive from object class)
Fourth if I were to assume you have a collection of Package based on your "question"
I want to take the value of intvalues of the Package whose name is strquery.
You want something like this
int[] foundValues;
foreach(Package pack in packageCollection)
{
if( pack.strvalue == strquery)
{
foundValues = pack.intvalues;
break;
}
}
Upvotes: 0