Learn how to a build a cloud-first strategyRegister Now

x
?
Solved

Unable to understand VBScript code

Posted on 2011-10-30
11
Medium Priority
?
387 Views
Last Modified: 2013-12-14
Hi Experts,

I have got some work, where I need to convert some VBScript code to C# 2.0, below is the VBScript code, which needs to be converted to c#.
' Component Template titles handled by the summary templates
FeaturedCT = "Featured Summary"
SummaryCT = "Summary"

' Set the looping variables on the first component presentation
If IsFirstComponent = 0 Then
	IsFirstComponent = 1
	
	' Start out left handed by default
	IsLeftHand = True

	matchCount = 0
	Dim components()
	ReDim components(Page.ComponentPresentations.Count)
	
	' Build a list of all the matching component presentations to be rendered out as summaries
	For Each objCP In Page.ComponentPresentations
		' Is this a Summary component template?
		If objCP.ComponentTemplateTitle = SummaryCT Or objCP.ComponentTemplateTitle = FeaturedCT Then
			' Check if this object should be included based on its approval status
			If staging Or getFieldValue(objCP.Component.MetadataFields("ApprovalStatus"), "") = "" Then
&#9;&#9;&#9;&#9;If getFieldValue(objCP.Component.MetadataFields("EndDate"), "") <> "" Then
&#9;&#9;&#9;&#9;&#9;If getDateSerial(objCP.Component.MetadataFields("EndDate").Value(1), False) > getDateSerial(Now, False) Then
&#9;&#9;&#9;&#9;&#9;&#9;Set components(matchCount) = objCP
&#9;&#9;&#9;&#9;&#9;&#9;matchCount = matchCount + 1
&#9;&#9;&#9;&#9;&#9;End If
&#9;&#9;&#9;&#9;Else
&#9;&#9;&#9;&#9;&#9;Set components(matchCount) = objCP
&#9;&#9;&#9;&#9;&#9;matchCount = matchCount + 1
&#9;&#9;&#9;&#9;End If
&#9;&#9;&#9;End If
&#9;&#9;End If
&#9;Next

&#9;' Resize the array to the amount of matches
&#9;ReDim Preserve components(matchCount)
End If

For i = 0 to UBound(components) - 1
&#9;' Determine which component to render from the pre-selected array
&#9;If components(i).ComponentID = Component.ID And components(i).OrdinalPosition = ComponentPresentation.OrdinalPosition Then
&#9;&#9;' Featured summary is always left aligned and causes all other items to be right-aligned
&#9;&#9;If ComponentPresentation.ComponentTemplateTitle = FeaturedCT Then
&#9;&#9;&#9;HasFeaturedSummary = 1
&#9;&#9;&#9;IsLeftHand = True&#9;&#9;&#9;
&#9;&#9;End If

&#9;&#9;Call RenderEntry(components, i)
&#9;&#9;
&#9;&#9;' If a featured summary was previously present all following items are right-aligned
&#9;&#9;If HasFeaturedSummary = 1 Then
&#9;&#9;&#9;IsLeftHand = False
&#9;&#9;Else
&#9;&#9;&#9;' Update the left-handed status
&#9;&#9;&#9;UpdatePositioning
&#9;&#9;End If

&#9;&#9;If i = UBound(Components) - 1 Then
&#9;&#9;&#9;WriteOut "<div class=""clearBoth""></div>"
&#9;&#9;End If
&#9;End If
Next

Open in new window


And below is the code which is attempted by me in c#.
public string RenderSummaryCT()
        {
            string FeaturedCT = "Featured Summary CT";
            string SummaryCT = "Summary CT";
            int IsFirstComponent = 0;
            string result = string.Empty;
            int hasFeaturedSummary = 0;
            Component comp = null;
            bool IsLeftHand = false;            
            List<tc.ComponentPresentation> cmp = new List<tc.ComponentPresentation>();

            if (IsFirstComponent == 0)
            {
                IsFirstComponent = 1;
                IsLeftHand = true;
                
                m_Logger.Info("CMP Array-" + cmp.Count);
                foreach (tc.ComponentPresentation objCMP in m_Page.ComponentPresentations)
                {
                    if ((objCMP.ComponentTemplate.Title == SummaryCT) || (objCMP.ComponentTemplate.Title == FeaturedCT))
                    {
                        comp = objCMP.Component;
                        string approvalStatus = string.Empty;
                        string endDate = string.Empty;

                        if (comp.Metadata != null)
                        {
                            ItemFields compItemfields = new ItemFields(comp.Metadata, comp.MetadataSchema);
                            approvalStatus = th.GetSingleStringValue("ApprovalStatus", compItemfields);
                            endDate = th.GetSingleStringValue("EndDate", compItemfields);
                        }
                        if ((baseutility.GetStagingConstantValue(m_Engine, m_Package)) || (string.IsNullOrEmpty(approvalStatus)))
                        {
                            if (!string.IsNullOrEmpty(endDate))
                            {
                                DateTime eDate = Convert.ToDateTime(baseutility.GetDateSerial(Convert.ToDateTime(endDate), false));
                                DateTime currentDate = Convert.ToDateTime(baseutility.GetDateSerial(DateTime.Now, false));
                                if ((eDate) > (currentDate))
                                {                                   
                                    cmp.Add(objCMP);                                                             
                                }
                            }
                            else
                            {
                                 cmp.Add(objCMP);
                                //m_Logger.Info("2. Adding cmp: " + maxCount.ToString() + "-- " + cmp[maxCount].Component.Title);                              
                            }
                        }
                    }                    
                }
            }

            foreach (tc.ComponentPresentation cm in cmp)
            {
                m_Logger.Info(cm.Component.Id +"--"+ m_Component.Id);
                if (cm.Component.Id == m_Component.Id)
                {
                    if (cm.ComponentTemplate == FeaturedCT)
                    {
                        hasFeaturedSummary = 1;
                    }


                    if (hasFeaturedSummary == 1)
                    {
                        IsLeftHand = false;
                    }
                    else
                    {

                    }
                    
                    m_Logger.Info("CMP maxcount -: " + cm.Component.Title);
                }
            }
            

            m_Logger.Info("Exiting for loop ");
            return result;
        }

Open in new window


I am sure there is something wrong, can you please suggest the suitable code using above VBScript logic.

Thanks
0
Comment
Question by:tia_kamakshi
9 Comments
 
LVL 10

Expert Comment

by:Jini Jose
ID: 37053749
0
 

Author Comment

by:tia_kamakshi
ID: 37054968
Hi Experts,

As you know this is the VBScript code and I need to convert to C#, but the link given by you is not for the vbscript code, its for VB6 to VB.net.

can you please convert this code as I am trying to do it.

Thanks.
0
 
LVL 27

Expert Comment

by:MikeToole
ID: 37056057

The VB code assigned the string variables for matching as:
            FeaturedCT = "Featured Summary"
            SummaryCT = "Summary"
In the C# code they're not the same:
            string FeaturedCT = "Featured Summary CT";
            string SummaryCT = "Summary CT";

Unless the format of the data has changed, the " CT" on the end of the string will cause all matches to fail.
0
Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 

Author Comment

by:tia_kamakshi
ID: 37056088
MikeToole,

I know these changes, this does not impact the logic, I intentially did this as this was going to be changed in .NET conversion.

Please suggest other part logic!!

Thanks.
0
 
LVL 27

Expert Comment

by:MikeToole
ID: 37056234
What is it that you don't understand about the VB code?
What are the problem symptoms of your current C# code?

0
 

Author Comment

by:tia_kamakshi
ID: 37056991
MikeToole,


Basically I am struggling with below for loop in vbscript, how can I write this in C#.

For i = 0 to UBound(components) - 1
	' Determine which component to render from the pre-selected array
	If components(i).ComponentID = Component.ID And components(i).OrdinalPosition = ComponentPresentation.OrdinalPosition Then
		' Featured summary is always left aligned and causes all other items to be right-aligned
		If ComponentPresentation.ComponentTemplateTitle = FeaturedCT Then
			HasFeaturedSummary = 1
			IsLeftHand = True			
		End If

		Call RenderEntry(components, i)
		
		' If a featured summary was previously present all following items are right-aligned
		If HasFeaturedSummary = 1 Then
			IsLeftHand = False
		Else
			' Update the left-handed status
			UpdatePositioning
		End If

		If i = UBound(Components) - 1 Then
			WriteOut "<div class=""clearBoth""></div>"
		End If
	End If
Next

Open in new window


Please suggest!!
0
 
LVL 27

Expert Comment

by:MikeToole
ID: 37058282
The first 'if' is missing the second part of the comparison
 if (cm.Component.Id == m_Component.Id && cm.OrdinalPosition == ComponentPresentation.OrdinalPosition)

>> Call RenderEntry(components, i)
This is a call to the routine RenderEntry(), it's not VB specific so the definintion must be somewhere else in the code

>> UpdatePositioning
>> WriteOut "<div class=""clearBoth""></div>"

Just an alternate way to call routines, the secon example passes a string parameter
Again these are not VB specific - look for them elsewhere in the code.

Hope this helps

>> 


0
 
LVL 32

Accepted Solution

by:
Robberbaron (robr) earned 2000 total points
ID: 37062079
I havent tried the logic but this is the code saved as VB6 file, then loaded into VB.NET, then run through the VB - c# convertor


you appear to have converted the Components array to a List, whuch just means some of the other code needs updating as well.  It may be easier to use ArrayList instead, which keeps the conversion a bit easier.


public void Test1()
{

	// Component Template titles handled by the summary templates
	FeaturedCT = "Featured Summary";
	SummaryCT = "Summary";

	// Set the looping variables on the first component presentation
	if (IsFirstComponent == 0) {
		IsFirstComponent = 1;

		// Start out left handed by default
		IsLeftHand = true;

		matchCount = 0;
		[] components = new[];

		// Build a list of all the matching component presentations to be rendered out as summaries
		foreach (object objCP_loopVariable in Page.ComponentPresentations) {
			objCP = objCP_loopVariable;
			// Is this a Summary component template?
			if (objCP.ComponentTemplateTitle == SummaryCT | objCP.ComponentTemplateTitle == FeaturedCT) {
				// Check if this object should be included based on its approval status
				if (staging | string.IsNullOrEmpty(getFieldValue(objCP.Component.MetadataFields("ApprovalStatus"), ""))) {

					if (!string.IsNullOrEmpty(getFieldValue(objCP.Component.MetadataFields("EndDate"), ""))) {

						if (getDateSerial(objCP.Component.MetadataFields("EndDate").Value(1), false) > getDateSerial(DateAndTime.Now, false)) {
							components(matchCount) = objCP;
							matchCount = matchCount + 1;
						}

					} else {
						components(matchCount) = objCP;
						matchCount = matchCount + 1;
					}
				}
			}
		}

		// Resize the array to the amount of matches
		Array.Resize(ref components, matchCount + 1);
	}

	for (i = 0; i <= Information.UBound(components) - 1; i++) {
		// Determine which component to render from the pre-selected array
		if (components(i).ComponentID == Component.ID & components(i).OrdinalPosition == ComponentPresentation.OrdinalPosition) {
			// Featured summary is always left aligned and causes all other items to be right-aligned
			if (ComponentPresentation.ComponentTemplateTitle == FeaturedCT) {
				HasFeaturedSummary = 1;
				IsLeftHand = true;
			}

			RenderEntry(components, i);

			// If a featured summary was previously present all following items are right-aligned
			if (HasFeaturedSummary == 1) {
				IsLeftHand = false;
			} else {
				// Update the left-handed status
				UpdatePositioning();
			}

			if (i == Information.UBound(components) - 1) {
				WriteOut("<div class='clearBoth'></div>");
			}
		}
	}
}

Open in new window

0
 

Author Closing Comment

by:tia_kamakshi
ID: 37105950
Provided solution was not appriate according to question asked
0

Featured Post

Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

This article will inform Clients about common and important expectations from the freelancers (Experts) who are looking at your Gig.
If you are a mobile app developer and especially develop hybrid mobile apps then these 4 mistakes you must avoid for hybrid app development to be the more genuine app developer.
THe viewer will learn how to use NetBeans IDE 8.0 for Windows to perform CRUD operations on a MySql database.
Six Sigma Control Plans

810 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question