Code to blotted

Converting lazy code to an object

Page 1 of 1

3 Replies - 2339 Views - Last Post: 24 January 2006 - 07:43 PM Rate Topic: -----

#1 cphalen  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 4
  • Joined: 17-January 06

Code to blotted

Posted 17 January 2006 - 02:43 PM

I am working on a site that querys info from many servers, up to 10 sometimes. The info that I am querying is Virtual Server VM states, I currently have to have to declare up to 10 Public Properties that have the servers IP address (see code below). Is there a way to just store the IP's in a sql DB and get them from there?

Imports MSDN.VirtualServerWrapper 'VSWVirtualServer pulled from here

Private _vsw158 As VSWVirtualServer
Private _vsw159 As VSWVirtualServer

Public ReadOnly Property vsw158() As VSWVirtualServer
   Get
      If _vsw158 Is Nothing Then
         _vsw158 = New VSWVirtualServer("192.168.10.158")
      End If
      Return _vsw158
   End Get
End Property 

Public ReadOnly Property vsw159() As VSWVirtualServer
   Get
      If _vsw159 Is Nothing Then
         _vsw159 = New VSWVirtualServer("192.168.10.159")
      End If
      Return _vsw159
   End Get
End Property 

etc....

On page Load:

For Each entry In vsw158.VMCollection
   'Do some stuff here
Next 


For Each entry In vsw159.VMCollection
   'Do some stuff here
Next 

etc...



Thanks, this will help me soooooo much :)

Craig

Is This A Good Question/Topic? 0
  • +

Replies To: Code to blotted

#2 sontek  Icon User is offline

  • D.I.C Regular
  • member icon

Reputation: 8
  • View blog
  • Posts: 283
  • Joined: 13-September 01

Re: Code to blotted

Posted 19 January 2006 - 11:05 AM

An easy way would be to create a Generic List of strings

List<string> listOfIps = new List<string>();

and save them into an XML file, You can actually serialize the generic list array (listOfIps.ToArray()) into an XML file and deserialize it into the object when you load it. Heres an example I whipped up in C#, I'm not to familiar with vb.


 public T Load<T>(string XMLFilePathName)
 {
        T toReturn;
        TextReader reader = null;
        try
        {
           XmlSerializer serializer = new XmlSerializer(typeof(T));
           if (File.Exists(XMLFilePathName))
           reader = new StreamReader(XMLFilePathName);
           toReturn = (T)serializer.Deserialize(reader);
         }
         finally
         {
           if (reader != null)
           reader.Close();
         }
         return toReturn;
}
public void Save(Object objectToSave, string XMLFilePathName)
{
   TextWriter writer = null;

    try
    {
       XmlSerializer serializer = new     XmlSerializer(objectToSave.GetType());

        string path = XMLFilePathName.Substring(0, XMLFilePathName.LastIndexOf('\\'));
        if (!Directory.Exists(path))
            Directory.CreateDirectory(path);

         if (File.Exists(XMLFilePathName))
            File.Delete(XMLFilePathName);

         writer = new StreamWriter(XMLFilePathName);
         serializer.Serialize(writer, objectToSave);
      }
      finally
      {
         if (writer != null)
         writer.Close();
      }
}


Was This Post Helpful? 0
  • +
  • -

#3 cphalen  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 4
  • Joined: 17-January 06

Re: Code to blotted

Posted 24 January 2006 - 06:42 PM

OKay.... Well this tells me how to pull the Servers from a list, I got that. But how do I declare the Properties dynamically?

Craig
Was This Post Helpful? 0
  • +
  • -

#4 sontek  Icon User is offline

  • D.I.C Regular
  • member icon

Reputation: 8
  • View blog
  • Posts: 283
  • Joined: 13-September 01

Re: Code to blotted

Posted 24 January 2006 - 07:43 PM

I'm not sure I understand exactly what you mean but if you're trying to dynamically create a property create an IP class that has 3 properties

PropertyName
Value
IPAddress


so then you'll have an array of IPClasses

IPClass[] ips = new IpClass[]
{
new IpClass("First Property", "Value", "192.168.0.1"),
new IpClass("Second Property", "Value", "192.168.0.2"),
new IpClass("Third Property", "Value", "192.168.0.3")
};

foreach(IpClass ip in ips)
{
Console.WriteLine(ip.Property + " " + ip.Value + "\n" + ip.IpAddress);
}




is that kind of what you mean? Sorry if I misunderstood!
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1