Hi guys, quick question, my objective is to make class somehow hold all values that i'm using in the program. I'd like the class to act as a database, if that's possible. My only problem is that, i'm passing an array from the main form to the class, and that array contains random numbers that has it's respective meaning in the database. Each number has 3 properties that i want to get/select. It's form my thesis. Any answer will be much appreciated. Here's an example:

in Form1:

myClass callClass = new myClass() // class instantiation


string[] rndNum = new string[47] //array for random numbers


for (int i = 0; i < 47; i++)
{
while (true)
{
int index = rnd.Next(0, 47);
if (!ifSame[index])
{
ifSame[index] = true;
rndNum = index.ToString();
break;
}
}
}
callClass.database(rndNum);

myClass:

public void database (string [] numbers)
{
int prop1 = 0;
int prop2 = 0;
int prop3 = 0;
for (int i = 0; i < numbers.Length; i++)
{
//this part will read the array passed from Form1
//also in this part, as the numbers from the array are being read, there
//respective properties are given to them.
//Somewhat like this,
// if (numbers == "0")
// {
//     prop1 = 1;
//     prop2 = 2;
//     prop3 = 3;
// }
//also i'd like the 3 properties(prop1, prop2, prop3) to be passed to
//Form1 along with it's respective value from the numbers array (#0-47)
}
}

Recommended Answers

All 4 Replies

Yes, it's possible, but you will eventually mimmick the behavior of a Hash Table (or Dictionary) of of a DataSet and DataTable (the HARD way).

I would suggest to create a class for the numbers structure and properties like:

Partial Class NumberAndProperties : IComparer {
  public readonly int Key;
  public readonly int Property1;
  public readonly int Property2,
  public readonly int Property3;
  void New(int key, int property1, int property2, int property3){
    Key = key;
    Property1 = property1;
    Property2 = property2;
    Property3 = property3;
  }
  int IComparer.Compare(object a, object b){
    NumberAndProperties A = (NumberAndProperties) a;
    NumberAndProperties B = (NumberAndProperties) b;
    If (A.Key > B.Key) return 1;
    if (A.Key < B.Key) return -1;
   return 0;
  }
}

Then use this class for a List<NumberAndProperties> to hold the data. To do that
add

public List<NumberAndProperties> TheNumbers = new List<NumberAndProperties>();

inside the class, before the public void DataBase and change this piece to:

//this part will read the array passed from Form1
//also in this part, as the numbers from the array are being read, there 
//respective properties are given to them.
//Somewhat like this,
// if (numbers[i] == "0")
// {
// prop1 = 1;
// prop2 = 2;
// prop3 = 3;
NumberAndProperties newNumberAndProperties = new NumberAndProperties(numbers[i], property1Value, Property2Value, property3value);
TheNumbers.Add(newNumberAndProperties);
// }

Then, to retrieve any value, you can use the List<T> methods and properties, and you'll always get a NumberAndProperties class with the Key and the Property 1 to 3.

Hope this helps

I would suggest to create a class for the numbers structure and properties like:

Partial Class NumberAndProperties : IComparer {
  public readonly int Key;
  public readonly int Property1;
  public readonly int Property2,
  public readonly int Property3;
  void New(int key, int property1, int property2, int property3){
    Key = key;
    Property1 = property1;
    Property2 = property2;
    Property3 = property3;
  }
  int IComparer.Compare(object a, object b){
    NumberAndProperties A = (NumberAndProperties) a;
    NumberAndProperties B = (NumberAndProperties) b;
    If (A.Key > B.Key) return 1;
    if (A.Key < B.Key) return -1;
   return 0;
  }
}

Then use this class for a List<NumberAndProperties> to hold the data. To do that
add

public List<NumberAndProperties> TheNumbers = new List<NumberAndProperties>();

inside the class, before the public void DataBase and change this piece to:

//this part will read the array passed from Form1
//also in this part, as the numbers from the array are being read, there 
//respective properties are given to them.
//Somewhat like this,
// if (numbers[i] == "0")
// {
// prop1 = 1;
// prop2 = 2;
// prop3 = 3;
NumberAndProperties newNumberAndProperties = new NumberAndProperties(numbers[i], property1Value, Property2Value, property3value);
TheNumbers.Add(newNumberAndProperties);
// }

Then, to retrieve any value, you can use the List<T> methods and properties, and you'll always get a NumberAndProperties class with the Key and the Property 1 to 3.

Hope this helps

thanks, but i cnt quite understand where to put the class for the number structures, also
the list declaration..and what is this for?

int IComparer.Compare(object a, object b){
NumberAndProperties A = (NumberAndProperties) a;
NumberAndProperties B = (NumberAndProperties) b;
If (A.Key > B.Key) return 1;
if (A.Key < B.Key) return -1;
return 0;
}

i just want to capture the properties of the number that i stored at my array

I'll try to answer your questions:
1) Where to put the class? I normally create each class in a separate source for that class. Obviously you can add the class in the same source you already have.
2) The IComparer.Compare will permit to sort the list or to verify if a key already exists.
3) The List usage is based on specialized list that will know the class structure, allowing the direct access to any list item key or property.

Hope this helps

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.