We help IT Professionals succeed at work.

Help with concatenating VB.NET code

vcharles
vcharles asked
on
Medium Priority
382 Views
Last Modified: 2012-05-12
Hello,

I am trying to concatenate "Receiver" with ".xml" in the code below, but the syntax is incorrect, can you please help me with the correct syntax.

 Dim xyz As String
        xyz = "Receiver"
            Dim MyReceiver As XDocument = XDocument.Load(Server.MapPath("~/App_Data/+ " & xyz & "+ " & .xml & ""))

Thanks,

Victor
Comment
Watch Question

djon2003Senior software engineer
CERTIFIED EXPERT

Commented:
Well, this string is not correctly formed.
"~/App_Data/+ " & xyz & "+ " & .xml & ""

Are the + useful in the path ?
If yes :
"~/App_Data/+ " & xyz & "+ .xml "
If no :
"~/App_Data/" & xyz & ".xml "

Author

Commented:
Thanks for the code, would your code work for everywhere I want to replace "Receiver" with "xyz"? I have about 30 instances of the word Receiver.

 Dim MyReceiver As XDocument = XDocument.Load(Server.MapPath("~/App_Data/" & xyz & ".xml & "))
        Dim LargestReceiverID = (From el In MyReceiver.Descendants("ReceiverTable") Select CInt(el.<Receiver_ID>.Value)).ToList
        LargestReceiverID.Sort()
        Dim CheckForItemReceiver = (From el In MyReceiver.Descendants("ReceiverTable").Elements("Receiver") Select el.Value).ToList
        If Not CheckForItemReceiver.Contains(Me.C1Receiver.Text.ToString) Then
            Dim xNew As XElement = New XElement("ReceiverTable")
            xNew.Add(New XElement("Receiver_ID", (LargestReceiverID.Last() + 1)))
            xNew.Add(New XElement("Receiver", Me.C1Receiver.Text.ToString))
            MyReceiver.Root.Add(xNew)
            MyReceiver.Save(Server.MapPath("~/App_Data/Receiver.xml"))
        End If
        Dim searchReceiver As String = C1Receiver.Text
        Dim searchIdReceiver As String = Nothing
        Dim domReceiver As New XmlDocument()
        Dim listReceiver As XmlNodeList = domReceiver.SelectNodes("//ReceiverTable")
        If CheckForItemReceiver.Contains(Me.C1Receiver.Text.ToString) Then
            For Each node As XmlNode In listReceiver
                If node("Receiver").InnerText.Equals(searchReceiver) Then
                    searchIdReceiver = node("Receiver_ID").InnerText
                    Exit For
                End If
            Next
        End If

Author

Commented:
I tried using the same approach, but getting lots of syntax errors.

Dim xyz As String
        xyz = "Receiver"
        ' Adding New Data
        '1-Add data to Receiver.xml if doesn't already exist
        Dim My& xyz &  As XDocument = XDocument.Load(Server.MapPath("~/App_Data/" & xyz & ".xml"))
        Dim Largest& xyz & ID = (From el In My& xyz & .Descendants("& xyz & Table") Select CInt(el.<& xyz & _ID>.Value)).ToList
        Largest&(xyz & ID.Sort())
        Dim CheckForItem& xyz &  = (From el In My& xyz & .Descendants("& xyz & Table").Elements("& xyz & ") Select el.Value).ToList
        If Not CheckForItem& xyz & .Contains(Me.C1& xyz & .Text.ToString) Then
            Dim xNew As XElement = New XElement("& xyz & Table")
            xNew.Add(New XElement("& xyz & _ID", (Largest& xyz & ID.Last() + 1)))
            xNew.Add(New XElement("& xyz & ", Me.C1& xyz & .Text.ToString))
            My&(xyz & .Root.Add(xNew))
            My&(xyz & .Save(Server.MapPath("~/App_Data/& xyz & .xml")))
        End If
        Dim search & xyz &  As String = C1& xyz & .Text
        Dim searchId& xyz &  As String = Nothing
        Dim dom& xyz &  As New XmlDocument()
        Dim list& xyz &  As XmlNodeList = dom& xyz & .SelectNodes("//& xyz & Table")
        If CheckForItem& xyz & .Contains(Me.C1& xyz & .Text.ToString) Then
            For Each node As XmlNode In list& xyz & 
                If node("& xyz & ").InnerText.Equals(search& xyz & ) Then
                    searchId& xyz &  = node("& xyz & _ID").InnerText
                    Exit For
                End If
            Next
        End If
CERTIFIED EXPERT
Most Valuable Expert 2012
Top Expert 2014

Commented:
> Dim Largest& xyz & ID

You can not do that with declarations.

Author

Commented:
Do you have another approach?
CERTIFIED EXPERT
Most Valuable Expert 2012
Top Expert 2014

Commented:
>Dim search & xyz &  As String

Why do you need it that way? Can it not be

Dim search123 As String
djon2003Senior software engineer
CERTIFIED EXPERT

Commented:
Ehm.. You cannot apply a replace all from Receiver to & xyz &

Variable names cannot be dynamically create on the fly using concatenation. So set the variable names to something generic if the term receiver doesn't represent what you do in all the cases.

Let's take an example of modification (One the of the last line) :
searchIdReceiver = node("Receiver_ID").InnerText -->
searchId& xyz &  = node("& xyz & _ID").InnerText

Errors :
- Variable names don't accept spaces neither "&" char.
- This << "& xyz & _ID" >> is literally interpreted. So, the node as to have that name. The variable is not replaced because it is inside "xyz".

So the correction for that line should be (if we keep the variable name including the keyword intact) :
searchIdReceiver = node("Receiver_ID").InnerText -->
searchIdReceiver = node(xyz &"_ID").InnerText

Author

Commented:
I don't think I need to use the name of the xml file for all the variables, also I don't need
Dim searchId& xyz &  As String = Nothing and Dim searchId& xyz &  As String = Nothing


Can the following Dim  variables be fixed while they are being used with diffrenet xml files in For Next Loop?

 Dim MyFix  As XDocument = XDocument.Load(Server.MapPath("~/App_Data/" & xyz & ".xml"))
        Dim LargestFixID = (From el In My& xyz & .Descendants("& xyz & Table") Select CInt(el.<& xyz & _ID>.Value)).ToList
        LargestFixID.Sort()
        Dim CheckForItemFix = (From el In My& xyz & .Descendants("& xyz & Table").Elements("& xyz & ") Select el.Value).ToList


  Dim searchFix As String = C1& xyz & .Text
  Dim domFix As New XmlDocument()
  Dim listFix As XmlNodeList = domFix .SelectNodes("//& xyz & Table")
djon2003Senior software engineer
CERTIFIED EXPERT

Commented:
In think, from what I see, that you would greatly profit from reading some documents about the structure of VB.NET.

Here are two that can put you on the good track :
http://msdn.microsoft.com/en-us/library/7ee5a7s1(v=vs.80).aspx
http://msdn.microsoft.com/en-us/library/81ed9a62(v=vs.80).aspx

Author

Commented:
Thanks for the links, will get back to you.

Author

Commented:
Hi,

I read the links, but I still need help with the concatenating my statements. I reduced the number of xyz in my equation, but I neeed help with passing the values of xyz1...xyz3 to my statement.

Dim xyz1 As String
Dim xyz2 as String
Dim xyz3 as String
        xyz1 = "Receiver"
       xyz2 = "Donor"
       xyz3 = "Item"
        ' Adding New Data
        For x = 1 to 3
        Dim MyFix &  As XDocument = XDocument.Load(Server.MapPath("~/App_Data/" & xyz & & x &".xml"))
        Dim LargestFixID = (From el In My& xyz &  & x &".Descendants("& xyz &  & x &".Table") Select CInt(el.<& xyz &  & x &"_ID>.Value)).ToList
        LargestFixID.Sort())
        Dim CheckForItemFix  = (From el In My& xyz & & x &" .Descendants("& xyz &  & x &".Table").Elements("& xyz &  & x &".") Select el.Value).ToList
        If Not CheckForItemFix.Contains(Me.C1& xyz & & x &" .Text.ToString) Then
            Dim xNew As XElement = New XElement("& xyz &  & x &"Table")
            xNew.Add(New XElement("& xyz &  & x &" _ID", (LargestFixID.Last() + 1)))
            xNew.Add(New XElement("& xyz &  & x &", Me.C1& xyz &  & x &".Text.ToString))
            MyFix.Root.Add(xNew))
            MyFix .Save(Server.MapPath("~/App_Data/& xyz & & x &".xml")))
        End If
        Dim searchFix As String = C1& xyz & .Text
        Dim domFix  As New XmlDocument()
        Dim listFix As XmlNodeList = domFix.SelectNodes("//& xyz & & x &"Table")
        If CheckForItem& xyz & .Contains(Me.C1& xyz &  & x &".Text.ToString) Then
            For Each node As XmlNode In listFix
                If node("& xyz & & x &"").InnerText.Equals(search& xyz & & x &" ) Then
                    searchIdFix = node("& xyz & & x &" _ID").InnerText
                    Exit For
                End If
            Next
        End If
x = x+ 1
Next
Senior software engineer
CERTIFIED EXPERT
Commented:
Woo, there are a lot of errors in that. I really think that you should go back to basics. For now, I'll bold you the parts where I think it should be a variable. Which means that concatenation (using My& xyz) is not allowed.

Dim xyz1 As String
Dim xyz2 as String
Dim xyz3 as String
        xyz1 = "Receiver"
       xyz2 = "Donor"
       xyz3 = "Item"
        ' Adding New Data
        For x = 1 to 3
        Dim MyFix &  As XDocument = XDocument.Load(Server.MapPath("~/App_Data/" & xyz & & x &".xml"))
        Dim LargestFixID = (From el In My& xyz &  & x &".Descendants("& xyz &  & x &".Table") Select CInt(el.<& xyz &  & x &"_ID>.Value)).ToList
        LargestFixID.Sort())
        Dim CheckForItemFix  = (From el In My& xyz & & x &" .Descendants("& xyz &  & x &".Table").Elements("& xyz &  & x &".") Select el.Value).ToList
        If Not CheckForItemFix.Contains(Me.C1& xyz & & x &" .Text.ToString) Then
            Dim xNew As XElement = New XElement("& xyz &  & x &"Table")
            xNew.Add(New XElement("& xyz &  & x &" _ID", (LargestFixID.Last() + 1)))
            xNew.Add(New XElement("& xyz &  & x &", Me.C1& xyz &  & x &".Text.ToString))
            MyFix.Root.Add(xNew))
            MyFix .Save(Server.MapPath("~/App_Data/& xyz & & x &".xml")))
        End If
        Dim searchFix As String = C1& xyz & .Text
        Dim domFix  As New XmlDocument()
        Dim listFix As XmlNodeList = domFix.SelectNodes("//& xyz & & x &"Table")
        If CheckForItem& xyz & .Contains(Me.C1& xyz &  & x &".Text.ToString) Then
            For Each node As XmlNode In listFix
                If node("& xyz & & x &"").InnerText.Equals(search& xyz & & x &" ) Then
                    searchIdFix = node("& xyz & & x &" _ID").InnerText
                    Exit For
                End If
            Next
        End If
x = x+ 1
Next


These portions in bold doesn't represent all the problems. Though, I can suggest you that if you have a list of names to go through to either use a Generic.List(Of String) or an array of String().

Dim xyz() As String = New String() {"Receiver", "Donor", "Item"}
Then receiver can be accessed by xyz(0). So, you can iterate using a for loop like :
For x = 0 to 2
 Dim curXYZ = xyz(i)
Next

More over, when you want to access a Control by its name and not via it's declared variable. You shall use :
Me.Controls(controlName) to access it, where controlName is a String variable which value is the name of the control. So you can access C1Receiver by Me.Controls("C1" & xyz(0)).

These concepts are valid for about any programming language. I don't know for how long you've been programming, but I would guess you are just starting. Said that, I would suggest you to search on google for beginners guide in VB.NET. Learn basic concepts, try them, play with them, modify them.

Author

Commented:
Thank You very much for the tips and recommendations.

Explore More ContentExplore courses, solutions, and other research materials related to this topic.