Can I make a Common Array Class and create a instance of the Common Array Class with Type of a new c

P

Peri

Dear All,

I have written a common array class which has insert, search and other
functions.

For Example (Code in VB.NET):

' Client Class
Public Class Client
Public ClientCode As String 'Key Field
Public ClientName As String
...
End Class

'Client Array Class - Similar to a Vector Class
Public Class ClientArray

Private _ArrayIn() As Client

'Inserts the value into the _ArrayIn()
Public Function Insert(ByVal value As Client) As Long
....
End Function

'Searches the array
Public Function Search(ByVal value As Client) As Long
' GetSearchKey - Use this function to get the value in the "_ArrayIn()"
and "value" and compare
....
End Function

' Gets the search key.
Private Function GetSearchKey(ByVal value As Client) As String
Return value.ClientCode
End Function

End Class

Similary I have many classes like the Client Class, Say
' Scrip Class
Public Class Scrip
Public ScripCode As Integer 'Key Field
Public ScripName As String
Public Exchange As Short
Public Symbol As String
Public Market As String
...
End Class

Instead of creating a ScripArray Class with the same functionality as the
ClientArray Class, Can I make a Common Array Class and create a instance of
the Common Array Class with Type as Client or Scrip?

Please help me out
 
M

Marc Gravell

List<Client> and List<Scrip>
or
Collection<Client> and Collection<Script>

job done
(don't ask me what the VB.NET syntax is; you cross-posted to a C#
ng...)

Marc
 
P

Peri

Dear Marc,

If I use a list of a collection then I cannot write my own insert, search,
resize funtionalities. I would like to create my own insert/search and also
need to implement similar to a list or a collection.
 
P

Peri

Dear Marc,

If I use a list of a collection then I cannot write my own insert, search,
resize funtionalities. I would like to create my own insert/search and also
need to implement similar to a list or a collection.
 
C

Chris Shepherd

Peri said:
If I use a list of a collection then I cannot write my own insert, search,
resize funtionalities. I would like to create my own insert/search and also
need to implement similar to a list or a collection.

Why not?

What about List<T>/Collection<T> doesn't do what you're looking for?

Chris.
 
P

Peri

Dear Chris,

In My code, if you see in the common array which I have written I have
declared a variable called "Private _ArrayIn() As Client" and methods like
"Public Function Search(ByVal value As Client) As Long" which has "Client"
has a type. Instead of "Client", If I need to have it as "Scrip" then how do
I do this?

I hope what you are telling me is: In another class where we are
implementing this client/scrip array, you are asking me to put
"Dim clientArrayList As New List(Of Client)"

The Code:
----------
'Client Array Class - Similar to a Vector Class
Public Class ClientArray

Private _ArrayIn() As CLIENT

'Inserts the value into the _ArrayIn()
Public Function Insert(ByVal value As Client) As Long
....
End Function

'Searches the array
Public Function Search(ByVal value As CLIENT) As Long
' GetSearchKey - Use this function to get the value in the "_ArrayIn()"
and "value" and compare
....
End Function

' Gets the search key.
Private Function GetSearchKey(ByVal value As CLIENT) As STRING
Return value.CLIENTCODE
End Function

End Class



Thanks and Regards,

Peri
 
M

Marc Gravell

Public Function Search(ByVal value As Client) As Long
What does that return? The index of the item? In which case
..IndexOf(T) is already implemented in both List<T> and Collection<T>.

In the more general sense with generics, you simply have (using C#
syntax, but same is possible in VB.NET):

public class SomeClass<T> {
public int SomeMethod(T someArgument) {...}
}

Marc
 
J

Jon Skeet [C# MVP]

In My code, if you see in the common array which I have written I have
declared a variable called "Private _ArrayIn() As Client" and methods like
"Public Function Search(ByVal value As Client) As Long" which has "Client"
has a type. Instead of "Client", If I need to have it as "Scrip" then how do
I do this?

You need to use generics. However, if you're writing VB.NET code you'd
be a lot better asking in a VB group than a C# one.

Jon
 
C

Chris Dunaway

I hope what you are telling me is: In another class where we are
implementing this client/scrip array, you are asking me to put
"Dim clientArrayList As New List(Of Client)"

If a List(Of Client) does not suit your needs, you can try to make
your own class generic. Something like this:

'Client Array Class - Similar to a Vector Class
Public Class Array(Of T)

Private _ArrayIn() As T

'Inserts the value into the _ArrayIn()
Public Function Insert(ByVal value As T) As Long
....
End Function

'Searches the array
Public Function Search(ByVal value As T) As Long
' GetSearchKey - Use this function to get the value in the
"_ArrayIn()"
and "value" and compare
....
End Function

' Gets the search key.
Private Function GetSearchKey(ByVal value As T) As String
Return value.ClientCode
End Function

End Class

Then you can declare and instance of this class like this:

Dim MyClientArray as New Array(Of Client)

or

Dim MyScripArray As new Array(Of Scrip)

or whatever.

You see if this can suit your needs.

Chris
 
C

Chris Shepherd

Peri said:
Dear Chris,

In My code, if you see in the common array which I have written I have
declared a variable called "Private _ArrayIn() As Client" and methods like
"Public Function Search(ByVal value As Client) As Long" which has "Client"
has a type. Instead of "Client", If I need to have it as "Scrip" then how do
I do this?

You could use a generic list, but I still don't get how
List<T>/Collection<T> don't fit what you need.

Maybe breaking this down into simpler questions will help:
- Do you have common properties and/or methods between Client and Scrip?
- If so, are they defined in an interface?

You can't make a generic constraint reliably based off some common
properties without some kind of intermediary or parent that details
those properties (basically you need an interface or a base class that
contains whatever is common between the two classes).

I hope what you are telling me is: In another class where we are
implementing this client/scrip array, you are asking me to put
"Dim clientArrayList As New List(Of Client)"

In the examples you provided, they can be made to work generically:

Public Class Client
Public ClientCode As String 'Key Field
Public ClientName As String
...
End Class

Public Class Scrip
Public ScripCode As Integer 'Key Field
Public ScripName As String
Public Exchange As Short
Public Symbol As String
Public Market As String
...
End Class

can be rewritten (C#) as:

public interface IClientScrip
{
string Code { get; set; }
string Name {get; set;}
// Any other common fields
}

public class Client : IClientScrip
{
public string Code
{
get { return "some string here"; }
set { /* Set value in here */ }
}

public string Name
{
get { return "some string here"; }
set { /* Set value in here */ }
}
// ETC...
}

public class Scrip : IClientScrip
{
public string Code
{
get { return "some string here"; }
set { /* Set value in here */ }
}

public string Name
{
get { return "some string here"; }
set { /* Set value in here */ }
}

public short Exchange
{
get { return 0; /* Some value */ }
set { /* Set value in here */ }
}

// Etc...
}

public class ClientScripList<T> where T : IClientScrip
{
private List<IClientScrip> _ArrayIn;

public long Search(IClientScrip item)
{
foreach (IClientScrip listItem in _ArrayIn)
{
if (GetSearchKey(listItem) == GetSearchKey(item))
return _ArrayIn.IndexOf(listItem);
}
}

private string GetSearchKey(IClientScrip item)
{
return item.Code;
}
}

The thing is, IMO that's not a great idea since once it's an interface
there becomes less reason to use your own custom list. Compounding that,
you have ability to mix the two items into the same list. Maybe that's
not a bad thing, but it could be.

If they are truly separate, and yet you still need your own custom List
object, then they should logically have separate List custom objects.

Personally I like to make my stuff work with the generic objects that
already exist as much as possible, rather than rolling my own.


Chris.
 
P

Peri

Thanks Chris,

But the GetSearchKey is not working since it returns Return value.ClientCode
(This is only for Client), If scrip then it should be Return
value.ScripCode.

Is there a way to implement this?

Thanks and Regards,

Peri
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top